-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
232 lines (209 loc) · 5.75 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright 2015 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"html/template"
"log"
"net/http"
"path/filepath"
"strings"
"time"
"golang.org/x/crypto/acme/autocert"
)
var (
dirFlag = flag.String("dir", "./public", "directory of files to serve")
distFlag = flag.String("dist-dir", "./dist", "directory of distribution files to serve")
goHepPkgs = []string{
"hep",
"hep/brio",
"hep/csvutil",
"hep/fads",
"hep/fastjet",
"hep/fit",
"hep/fmom",
"hep/fwk",
"hep/hbook",
"hep/hepevt",
"hep/hepmc",
"hep/heppdt",
"hep/hplot",
"hep/lhef",
"hep/lhef2hepmc",
"hep/pawgo",
"hep/rio",
"hep/rootio",
"hep/sio",
"hep/slha",
"hep/xrootd",
}
expPkgs = []string{
"exp",
"exp/vgshiny",
}
cgoPkgs = []string{
"cgo",
"cgo/croot",
}
)
func main() {
flag.Parse()
dir, err := filepath.Abs(*dirFlag)
if err != nil {
log.Fatal(err)
}
distDir, err := filepath.Abs(*distFlag)
if err != nil {
log.Fatal(err)
}
m := autocert.Manager{
Prompt: autocert.AcceptTOS,
Email: "[email protected]",
HostPolicy: autocert.HostWhitelist("go-hep.org", "www.go-hep.org"),
Cache: autocert.DirCache("cert-cache"),
}
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir(dir)))
mux.Handle("/dist/", http.StripPrefix("/dist/", http.FileServer(http.Dir(distDir))))
mux.HandleFunc("/x/", goGetHandle)
mux.HandleFunc("/commit/", commitHandle)
go http.ListenAndServe(":http", m.HTTPHandler(http.HandlerFunc(redirectToHttps)))
srv := http.Server{
Addr: ":https",
Handler: m.HTTPHandler(mux),
TLSConfig: m.TLSConfig(),
ReadTimeout: time.Second * 15,
WriteTimeout: time.Second * 30,
IdleTimeout: time.Minute * 5,
MaxHeaderBytes: 1 << 20,
}
err = srv.ListenAndServeTLS("", "")
if err != nil {
log.Fatal(err)
}
}
func redirectToHttps(w http.ResponseWriter, req *http.Request) {
if strings.HasPrefix(req.URL.Path, "/x/") {
goGetHandle(w, req)
return
}
http.Redirect(w, req,
"https://"+req.Host+req.URL.String(),
http.StatusMovedPermanently,
)
}
func commitHandle(w http.ResponseWriter, r *http.Request) {
url := r.URL.Path
sha := url[len("/commit/"):]
data := struct {
Commit string
Repo string
}{sha, "hep"}
commitTemplate.Execute(w, data)
}
func goGetHandle(w http.ResponseWriter, r *http.Request) {
url := r.URL.Path
url = url[len("/x/"):]
repo := url
var data = struct {
Pkg string
Repo string
}{
Pkg: repo,
Repo: repo,
}
switch {
case goGetHepPkg(repo):
data.Repo = "hep"
goGetHepTemplate.Execute(w, data)
return
case goGetExpPkg(repo):
data.Repo = "exp"
goGetHepTemplate.Execute(w, data)
return
case goGetCgoPkg(repo):
repo = repo[len("cgo/"):]
data.Pkg = repo
data.Repo = repo
goGetCgoTemplate.Execute(w, data)
return
default:
http.NotFound(w, r)
return
}
}
func goGetHepPkg(pkg string) bool {
for _, v := range goHepPkgs {
if strings.HasPrefix(pkg, v) {
return true
}
}
return false
}
func goGetExpPkg(pkg string) bool {
for _, v := range expPkgs {
if strings.HasPrefix(pkg, v) {
return true
}
}
return false
}
func goGetCgoPkg(pkg string) bool {
for _, v := range cgoPkgs {
if pkg == v {
return true
}
}
return false
}
var goGetHepTemplate = template.Must(template.New("x/hep").Parse(`<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="go-hep.org/x/{{.Repo}} git https://github.com/go-hep/{{.Repo}}"/>
<meta name="go-source" content="go-hep.org/x/{{.Repo}} https://github.com/go-hep/{{.Repo}}/ https://github.com/go-hep/{{.Repo}}/tree/main{/dir} https://github.com/go-hep/{{.Repo}}/blob/main{/dir}/{file}#L{line}"/>
<meta http-equiv="refresh" content="0; url=https://pkg.go.dev/go-hep.org/x/{{.Pkg}}"/>
</head>
<body>
Nothing to see here; <a href="https://pkg.go.dev/go-hep.org/x/{{.Pkg}}">move along</a>.
</body>
</html>
`))
var goGetExpTemplate = template.Must(template.New("x/exp").Parse(`<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="go-hep.org/x/{{.Repo}} git https://github.com/go-hep/{{.Repo}}"/>
<meta name="go-source" content="go-hep.org/x/{{.Repo}} https://github.com/go-hep/{{.Repo}}/ https://github.com/go-hep/{{.Repo}}/tree/main{/dir} https://github.com/go-hep/{{.Repo}}/blob/main{/dir}/{file}#L{line}"/>
<meta http-equiv="refresh" content="0; url=https://pkg.go.dev/go-hep.org/x/{{.Pkg}}"/>
</head>
<body>
Nothing to see here; <a href="https://pkg.go.dev/go-hep.org/x/{{.Pkg}}">move along</a>.
</body>
</html>
`))
var goGetCgoTemplate = template.Must(template.New("x/cgo").Parse(`<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="go-hep.org/x/cgo/{{.Repo}} git https://github.com/go-hep/{{.Repo}}"/>
<meta name="go-source" content="go-hep.org/x/cgo/{{.Repo}} https://github.com/go-hep/{{.Repo}}/ https://github.com/go-hep/{{.Repo}}/tree/main{/dir} https://github.com/go-hep/{{.Repo}}/blob/main{/dir}/{file}#L{line}"/>
<meta http-equiv="refresh" content="0; url=https://pkg.go.dev/go-hep.org/x/cgo/{{.Pkg}}"/>
</head>
<body>
Nothing to see here; <a href="https://pkg.go.dev/go-hep.org/x/cgo/{{.Pkg}}">move along</a>.
</body>
</html>
`))
var commitTemplate = template.Must(template.New("commit").Parse(`<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta http-equiv="refresh" content="0; url=https://github.com/go-hep/{{.Repo}}/commit/{{.Commit}}"/>
</head>
<body>
Nothing to see here; <a href="https://github.com/go-hep/{{.Repo}}/commit/{{.Commit}}">move along</a>.
</body>
</html>
`))