-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
213 lines (200 loc) · 5.59 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"flag"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
)
const (
kFaviconFile = "./static/favicon.ico"
kFaviconPath = "/favicon.ico"
kStaticDir = "./static"
kStaticPath = "/static/"
kRootPath = "/"
kTemplatesDir = "./templates"
kCommonTemplate = "common.tmpl"
kSiteTemplate = "site.tmpl"
kSiteTableTemplate = "site_table.tmpl"
kSiteSubmissionTemplate = "site_submission.tmpl"
kSiteTemplateName = "site"
kDirIndex = "index.html"
kShelvesPage = "/shelves.html"
kShelvesPath = "/shelves/"
kCategoryQuery = "category"
kTagQuery = "tag"
)
var (
certFileFlag = flag.String("cert", "tls.crt", "Path to certificate public file")
keyFileFlag = flag.String("key", "tls.key", "Path to certificate private key file")
httpPortFlag = flag.String("http_port", ":http", "Server http port")
httpsPortFlag = flag.String("https_port", ":https", "Server https port")
httpsFlag = flag.Bool("https", false, "Whether to use https")
)
func main() {
flag.Parse()
if len(*httpPortFlag) == 0 {
log.Println("must provide --http_port")
return
}
if *httpsFlag {
if len(*certFileFlag) == 0 {
log.Println("must provide --cert with --https flag")
return
} else if len(*keyFileFlag) == 0 {
log.Println("must provide --key with --https flag")
return
} else if len(*httpsPortFlag) == 0 {
log.Println("must provide --https_port with --https flag")
return
}
}
p, hf := handleFavicon()
http.HandleFunc(p, hf)
p, h := handleStatic()
http.Handle(p, h)
p, hf = kRootPath, handleTemplate(kSiteTemplate, nil)
http.HandleFunc(p, hf)
// Problems
p, hf = kShelvesPage, handleTemplate(kSiteTableTemplate, allData)
http.HandleFunc(p, hf)
p, hf = kShelvesPath, handleTemplate(kSiteSubmissionTemplate, allData)
http.HandleFunc(p, hf)
if *httpsFlag {
log.Printf("Https & http-redirect: Listening on %s and %s\n", *httpPortFlag, *httpsPortFlag)
go func() {
redir := http.NewServeMux()
redir.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Connection", "close")
http.Redirect(w, req, fmt.Sprintf("https://%s%s", req.Host, req.URL), http.StatusFound)
})
err := http.ListenAndServe(*httpPortFlag, redir)
if err != nil {
log.Fatal(err)
}
}()
err := http.ListenAndServeTLS(*httpsPortFlag, *certFileFlag, *keyFileFlag, nil)
if err != nil {
log.Fatal(err)
}
} else {
log.Printf("Http-only: Listening on %s\n", *httpPortFlag)
err := http.ListenAndServe(*httpPortFlag, nil)
if err != nil {
log.Fatal(err)
}
}
}
func handleFavicon() (string, http.HandlerFunc) {
return kFaviconPath, func(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, kFaviconFile)
}
}
func handleStatic() (string, http.Handler) {
fs := http.FileServer(http.Dir(kStaticDir))
return kStaticPath, http.StripPrefix(kStaticPath, fs)
}
func handleTemplate(siteTemplate string, data tableData) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
cp := filepath.Join(kTemplatesDir, kCommonTemplate)
lp := filepath.Join(kTemplatesDir, siteTemplate)
fp := filepath.Join(kTemplatesDir, filepath.Clean(req.URL.Path))
info, err := os.Stat(fp)
if err != nil {
if os.IsNotExist(err) {
http.NotFound(w, req)
return
}
} else if info.IsDir() {
fp = filepath.Join(fp, kDirIndex)
info, err = os.Stat(fp)
if err != nil {
if os.IsNotExist(err) {
http.NotFound(w, req)
return
}
} else if info.IsDir() {
http.NotFound(w, req)
return
}
}
// Process any query filtering
query := req.URL.Query()
categories, _ := query[kCategoryQuery]
tags, _ := query[kTagQuery]
u := req.URL
addQueryToU := func(k, v string) string {
c := *u
q := c.Query()
q.Add(k, v)
c.RawQuery = q.Encode()
return (&c).RequestURI()
}
removeQueryFromU := func(k, v string) string {
c := *u
q := c.Query()
vals, ok := q[k]
if ok {
di := -1
for i := 0; i < len(vals); i++ {
if vals[i] == v {
di = i
break
}
}
if di >= 0 {
copy(vals[di:], vals[di+1:])
vals[len(vals)-1] = ""
vals = vals[:len(vals)-1]
q[k] = vals
}
}
c.RawQuery = q.Encode()
return (&c).RequestURI()
}
fm := template.FuncMap{
"RequestURIWithQueryCategory": func(v category) string {
return addQueryToU(kCategoryQuery, string(v))
},
"RequestURIWithQueryTag": func(t tag) string {
return addQueryToU(kTagQuery, string(t))
},
"RequestURIWithoutQueryCategory": func(v string) string {
return removeQueryFromU(kCategoryQuery, v)
},
"RequestURIWithoutQueryTag": func(t string) string {
return removeQueryFromU(kTagQuery, t)
},
}
tmpl := template.New("")
tmpl = tmpl.Funcs(fm)
tmpl, err = tmpl.ParseFiles(cp, lp, fp)
if err != nil {
log.Println(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
templateData := struct {
Data tableData
Path string
SiteLicense licenseType
CategoryFilter []string
TagFilter []string
}{
Data: data.FilterByCategoriesAndTags(categories, tags),
Path: req.URL.Path,
SiteLicense: kSiteLicense,
CategoryFilter: categories,
TagFilter: tags,
}
err = tmpl.ExecuteTemplate(w, kSiteTemplateName, templateData)
if err != nil {
log.Println(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
return
}
}