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
|
// SPDX-FileCopyrightText: © 2019 Grégoire Duchêne <gduchene@awhk.org>
// SPDX-License-Identifier: ISC
//go:build aws && linux
package main
import (
"context"
"net/http"
"os"
"path"
"strings"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
var (
from = os.Getenv("FROM")
to = os.Getenv("TO")
vcs = os.Getenv("VCS")
redir = &redirector{from, to, vcs}
)
func redirect(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
pkg := path.Join(req.Headers["Host"], req.Path)
if v, ok := req.QueryStringParameters["go-get"]; !ok || v != "1" {
return events.APIGatewayProxyResponse{
Headers: map[string]string{"Location": "https://pkg.go.dev/" + pkg},
StatusCode: http.StatusFound,
}, nil
}
var buf strings.Builder
if err := body.Execute(&buf, bodyData{pkg, redir.getRepo(pkg), vcs}); err != nil {
return events.APIGatewayProxyResponse{}, err
}
return events.APIGatewayProxyResponse{
Body: buf.String(),
Headers: map[string]string{"Content-Type": "text/html; charset=utf-8"},
StatusCode: http.StatusOK,
}, nil
}
func main() {
lambda.Start(redirect)
}
|