diff options
| author | Grégoire Duchêne <gduchene@awhk.org> | 2022-01-04 14:16:32 +0000 |
|---|---|---|
| committer | Grégoire Duchêne <gduchene@awhk.org> | 2022-01-04 14:16:32 +0000 |
| commit | 02d777a1675d12f0965e974ae8b0e2b1265210ea (patch) | |
| tree | e9abe62d2ddc317edfd3f6a27d10906364fa8c49 /main_aws.go | |
| parent | 39c8e0eeb9ad1662d665f3727664a7b2138d7cf0 (diff) | |
Move redirection logic into its own HTTP handler
Also, have the non-Lambda version take flags instead of environment
variables and add tests for the HTTP handler.
Diffstat (limited to 'main_aws.go')
| -rw-r--r-- | main_aws.go | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/main_aws.go b/main_aws.go index 120d6de..4f6f16e 100644 --- a/main_aws.go +++ b/main_aws.go @@ -8,27 +8,38 @@ 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) { - var ( - pkg = path.Join(req.Headers["Host"], req.Path) - resp = events.APIGatewayProxyResponse{ - Body: GetBody(pkg), - Headers: map[string]string{"Content-Type": "text/html; charset=utf-8"}, - } - ) - if v, ok := req.QueryStringParameters["go-get"]; ok && v == "1" { - resp.StatusCode = http.StatusOK - } else { - resp.Headers["Location"] = "https://pkg.go.dev/" + pkg - resp.StatusCode = http.StatusFound + 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 resp, nil + return events.APIGatewayProxyResponse{ + Body: buf.String(), + Headers: map[string]string{"Content-Type": "text/html; charset=utf-8"}, + StatusCode: http.StatusOK, + }, nil } func main() { |
