-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
135 lines (120 loc) · 3.95 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
// Package main contains the code that redirects go modules requests to the correct
// DEDIS repositiory.
package main
import (
"bytes"
"context"
"fmt"
"html/template"
"regexp"
"strings"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
// prefix is the base URL
const prefix = "/"
// repositiory contains the property of a repository, namely the URL and
// the VCS.
type repository struct {
URL, VCS string
}
// repoMap contains the list of repositories distributed by DEDIS
var repoMap = map[string]repository{
"cothority": {"https://github.com/dedis/cothority", "git"},
"kyber": {"https://github.com/dedis/kyber", "git"},
"onet": {"https://github.com/dedis/onet", "git"},
"protobuf": {"https://github.com/dedis/protobuf", "git"},
"fixbuf": {"https://github.com/dedis/fixbuf", "git"},
"simnet": {"https://github.com/dedis/simnet", "git"},
"purbs": {"https://github.com/dedis/purbs", "git"},
"indyclient": {"https://github.com/dedis/indyclient", "git"},
"dela": {"https://github.com/dedis/dela", "git"},
"dela-apps": {"https://github.com/dedis/dela-apps", "git"},
"cs438": {"https://github.com/dedis/cs438", "git"},
"polypus-go": {"https://github.com/dedis/polypus-go", "git"},
"libpurb": {"https://github.com/dedis/libpurb", "git"},
"purb-db": {"https://github.com/dedis/purb-db", "git"},
"debugtools": {"https://github.com/dedis/debugtools", "git"},
}
var xTemplate = template.Must(template.New("x").Parse(`<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="go.dedis.ch/{{.Head}} {{.Repo.VCS}} {{.Repo.URL}}">
<meta name="go-source" content="go.dedis.ch/{{.Head}} https://github.com/dedis/{{.Head}}/ https://github.com/dedis/{{.Head}}/tree/master{/dir} https://github.com/dedis/{{.Head}}/blob/master{/dir}/{file}#L{line}">
<meta http-equiv="refresh" content="0; url=https://godoc.org/go.dedis.ch/{{.Head}}{{.Tail}}">
</head>
<body>
Nothing to see here; <a href="https://godoc.org/go.dedis.ch/{{.Head}}{{.Tail}}">move along</a>.
</body>
</html>
`))
var templateRedirection = `<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="refresh" content="0; url=https://dedis.epfl.ch">
</head>
<body>
Redirecting...
</body>
</html>`
func makeRedirection() events.APIGatewayProxyResponse {
return events.APIGatewayProxyResponse{
Headers: map[string]string{"Content-Type": "text/html"},
StatusCode: 301,
Body: templateRedirection,
}
}
func makeError(code int, body string) events.APIGatewayProxyResponse {
return events.APIGatewayProxyResponse{
Headers: map[string]string{"Content-Type": "text/html"},
StatusCode: code,
Body: fmt.Sprintf(`<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>%s</body>
</html>
`, body),
}
}
func handleRequest(
ctx context.Context,
req events.APIGatewayProxyRequest,
) (events.APIGatewayProxyResponse, error) {
head, tail := strings.TrimPrefix(req.Path, prefix), ""
if i := strings.Index(head, "/"); i != -1 {
head, tail = head[:i], head[i:]
// remove the versioning as it is not supported by godoc
re := regexp.MustCompile("/v[0-9]{1,}")
tail = re.ReplaceAllString(tail, "")
}
// The base route redirects to the DEDIS website
if head == "" {
return makeRedirection(), nil
}
repo, ok := repoMap[head]
if !ok {
return makeError(404, "<h1>404 Page Not Found</h1>"), nil
}
data := struct {
Head, Tail string
Repo repository
}{head, tail, repo}
buf := bytes.NewBufferString("")
if err := xTemplate.Execute(buf, data); err != nil {
return makeError(500, "<h1>500 Bad Request</h1>"), err
}
return events.APIGatewayProxyResponse{
Body: buf.String(),
StatusCode: 200,
Headers: map[string]string{
"Content-Type": "text/html",
},
}, nil
}
func main() {
lambda.Start(handleRequest)
}