From feff62b8fcff9709dc6c7f0b56ce80a288367f29 Mon Sep 17 00:00:00 2001 From: Grégoire Duchêne Date: Thu, 26 Mar 2020 22:03:15 +0000 Subject: Standardize the project layout https://github.com/golang-standards/project-layout seems sensible. --- aws/main.go | 38 -------------------------------------- cmd/aws/main.go | 38 ++++++++++++++++++++++++++++++++++++++ cmd/gcp/main.go | 35 +++++++++++++++++++++++++++++++++++ gcp/main.go | 35 ----------------------------------- internal/lib.go | 27 +++++++++++++++++++++++++++ internal/lib_test.go | 23 +++++++++++++++++++++++ lib/lib.go | 27 --------------------------- lib/lib_test.go | 23 ----------------------- 8 files changed, 123 insertions(+), 123 deletions(-) delete mode 100644 aws/main.go create mode 100644 cmd/aws/main.go create mode 100644 cmd/gcp/main.go delete mode 100644 gcp/main.go create mode 100644 internal/lib.go create mode 100644 internal/lib_test.go delete mode 100644 lib/lib.go delete mode 100644 lib/lib_test.go diff --git a/aws/main.go b/aws/main.go deleted file mode 100644 index 528f9b3..0000000 --- a/aws/main.go +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) 2019, Grégoire Duchêne -// -// Use of this source code is governed by the ISC license that can be -// found in the LICENSE file. - -// +build linux - -package main - -import ( - "context" - "github.com/aws/aws-lambda-go/events" - "github.com/aws/aws-lambda-go/lambda" - "go.awhk.org/go-import-redirect/lib" - "net/http" - "path" -) - -func redirect(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { - var ( - pkg = path.Join(req.Headers["Host"], req.Path) - resp = events.APIGatewayProxyResponse{ - Body: lib.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://godoc.org/" + pkg - resp.StatusCode = http.StatusFound - } - return resp, nil -} - -func main() { - lambda.Start(redirect) -} diff --git a/cmd/aws/main.go b/cmd/aws/main.go new file mode 100644 index 0000000..7954af3 --- /dev/null +++ b/cmd/aws/main.go @@ -0,0 +1,38 @@ +// Copyright (c) 2019, Grégoire Duchêne +// +// Use of this source code is governed by the ISC license that can be +// found in the LICENSE file. + +// +build linux + +package main + +import ( + "context" + "github.com/aws/aws-lambda-go/events" + "github.com/aws/aws-lambda-go/lambda" + "go.awhk.org/go-import-redirect/internal" + "net/http" + "path" +) + +func redirect(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { + var ( + pkg = path.Join(req.Headers["Host"], req.Path) + resp = events.APIGatewayProxyResponse{ + Body: internal.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://godoc.org/" + pkg + resp.StatusCode = http.StatusFound + } + return resp, nil +} + +func main() { + lambda.Start(redirect) +} diff --git a/cmd/gcp/main.go b/cmd/gcp/main.go new file mode 100644 index 0000000..e8ad6ba --- /dev/null +++ b/cmd/gcp/main.go @@ -0,0 +1,35 @@ +// Copyright (c) 2019, Grégoire Duchêne +// +// Use of this source code is governed by the ISC license that can be +// found in the LICENSE file. + +package main + +import ( + "go.awhk.org/go-import-redirect/internal" + "log" + "net/http" + "os" + "path" +) + +func redirect(resp http.ResponseWriter, req *http.Request) { + pkg := path.Join(req.Host, req.URL.Path) + resp.Header()["Content-Type"] = []string{"text/html; charset=utf-8"} + if v, ok := req.URL.Query()["go-get"]; ok && len(v) > 0 && v[0] == "1" { + resp.WriteHeader(http.StatusOK) + } else { + resp.Header()["Location"] = []string{"https://godoc.org/" + pkg} + resp.WriteHeader(http.StatusFound) + } + resp.Write([]byte(internal.GetBody(pkg))) +} + +func main() { + port := os.Getenv("PORT") + if port == "" { + port = "8080" + } + http.HandleFunc("/", redirect) + log.Fatal(http.ListenAndServe(os.Getenv("ADDR")+":"+port, nil)) +} diff --git a/gcp/main.go b/gcp/main.go deleted file mode 100644 index bb792cc..0000000 --- a/gcp/main.go +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) 2019, Grégoire Duchêne -// -// Use of this source code is governed by the ISC license that can be -// found in the LICENSE file. - -package main - -import ( - "go.awhk.org/go-import-redirect/lib" - "log" - "net/http" - "os" - "path" -) - -func redirect(resp http.ResponseWriter, req *http.Request) { - pkg := path.Join(req.Host, req.URL.Path) - resp.Header()["Content-Type"] = []string{"text/html; charset=utf-8"} - if v, ok := req.URL.Query()["go-get"]; ok && len(v) > 0 && v[0] == "1" { - resp.WriteHeader(http.StatusOK) - } else { - resp.Header()["Location"] = []string{"https://godoc.org/" + pkg} - resp.WriteHeader(http.StatusFound) - } - resp.Write([]byte(lib.GetBody(pkg))) -} - -func main() { - port := os.Getenv("PORT") - if port == "" { - port = "8080" - } - http.HandleFunc("/", redirect) - log.Fatal(http.ListenAndServe(os.Getenv("ADDR")+":"+port, nil)) -} diff --git a/internal/lib.go b/internal/lib.go new file mode 100644 index 0000000..742a9c7 --- /dev/null +++ b/internal/lib.go @@ -0,0 +1,27 @@ +// Copyright (c) 2019, Grégoire Duchêne +// +// Use of this source code is governed by the ISC license that can be +// found in the LICENSE file. + +package internal + +import ( + "fmt" + "os" + "strings" +) + +func GetBody(pkg string) string { + dest := GetDest(os.Getenv("PREFIX"), os.Getenv("DEST"), pkg) + return fmt.Sprintf(` + +go-import-redirect +`, pkg, os.Getenv("VCS"), dest) +} + +func GetDest(srcPrefix, destPrefix, pkg string) string { + srcPrefix = strings.TrimRight(srcPrefix, "/") + destPrefix = strings.TrimRight(destPrefix, "/") + path := strings.TrimLeft(strings.TrimPrefix(pkg, srcPrefix), "/") + return destPrefix + "/" + strings.Split(path, "/")[0] +} diff --git a/internal/lib_test.go b/internal/lib_test.go new file mode 100644 index 0000000..19a880d --- /dev/null +++ b/internal/lib_test.go @@ -0,0 +1,23 @@ +// Copyright (c) 2019, Grégoire Duchêne +// +// Use of this source code is governed by the ISC license that can be +// found in the LICENSE file. + +package internal + +import "testing" + +func TestGetDest(t *testing.T) { + cs := []struct{ srcPrefix, destPrefix, pkg, expected string }{ + {"src.example.com/x/", "https://example.com/git/", "src.example.com/x/foo", "https://example.com/git/foo"}, + {"src.example.com/x/", "https://example.com/git/", "src.example.com/x/foo/bar", "https://example.com/git/foo"}, + {"src.example.com/x", "https://example.com/git", "src.example.com/x/foo", "https://example.com/git/foo"}, + {"src.example.com/x", "https://example.com/git", "src.example.com/x/foo/bar", "https://example.com/git/foo"}, + } + for _, c := range cs { + actual := GetDest(c.srcPrefix, c.destPrefix, c.pkg) + if actual != c.expected { + t.Errorf("expected %s, got %s", c.expected, actual) + } + } +} diff --git a/lib/lib.go b/lib/lib.go deleted file mode 100644 index a35cb13..0000000 --- a/lib/lib.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2019, Grégoire Duchêne -// -// Use of this source code is governed by the ISC license that can be -// found in the LICENSE file. - -package lib - -import ( - "fmt" - "os" - "strings" -) - -func GetBody(pkg string) string { - dest := GetDest(os.Getenv("PREFIX"), os.Getenv("DEST"), pkg) - return fmt.Sprintf(` - -go-import-redirect -`, pkg, os.Getenv("VCS"), dest) -} - -func GetDest(srcPrefix, destPrefix, pkg string) string { - srcPrefix = strings.TrimRight(srcPrefix, "/") - destPrefix = strings.TrimRight(destPrefix, "/") - path := strings.TrimLeft(strings.TrimPrefix(pkg, srcPrefix), "/") - return destPrefix + "/" + strings.Split(path, "/")[0] -} diff --git a/lib/lib_test.go b/lib/lib_test.go deleted file mode 100644 index 7b46bfb..0000000 --- a/lib/lib_test.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) 2019, Grégoire Duchêne -// -// Use of this source code is governed by the ISC license that can be -// found in the LICENSE file. - -package lib - -import "testing" - -func TestGetDest(t *testing.T) { - cs := []struct{ srcPrefix, destPrefix, pkg, expected string }{ - {"src.example.com/x/", "https://example.com/git/", "src.example.com/x/foo", "https://example.com/git/foo"}, - {"src.example.com/x/", "https://example.com/git/", "src.example.com/x/foo/bar", "https://example.com/git/foo"}, - {"src.example.com/x", "https://example.com/git", "src.example.com/x/foo", "https://example.com/git/foo"}, - {"src.example.com/x", "https://example.com/git", "src.example.com/x/foo/bar", "https://example.com/git/foo"}, - } - for _, c := range cs { - actual := GetDest(c.srcPrefix, c.destPrefix, c.pkg) - if actual != c.expected { - t.Errorf("expected %s, got %s", c.expected, actual) - } - } -} -- cgit v1.2.3-70-g09d2