aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md3
-rw-r--r--aws/main.go31
-rw-r--r--gcp/main.go35
-rw-r--r--lib/lib.go26
4 files changed, 70 insertions, 25 deletions
diff --git a/README.md b/README.md
index d94e0f3..1fe7219 100644
--- a/README.md
+++ b/README.md
@@ -10,3 +10,6 @@ You need to set up the following environment variables for it to work:
* `PREFIX` for the prefix that must be removed from your package name,
e.g. `golang.org/x/` for `golang.org/x/image`, and
* `VCS` for the type of VCS you are using, e.g. `git`.
+
+Additionally, the version under `gcp/` will bind to the port passed in
+the `PORT` environment variable, or 8080 if that variable is not set.
diff --git a/aws/main.go b/aws/main.go
index b1dcc28..528f9b3 100644
--- a/aws/main.go
+++ b/aws/main.go
@@ -9,45 +9,26 @@ package main
import (
"context"
- "fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
- "os"
+ "go.awhk.org/go-import-redirect/lib"
+ "net/http"
"path"
- "strings"
)
-func getDest(dest, repo string) string {
- dest = strings.TrimRight(dest, "/")
- return fmt.Sprintf("%s/%s", dest, repo)
-}
-
-func getRepo(pkg, prefix string) string {
- prefix = strings.TrimRight(prefix, "/")
- path := strings.TrimLeft(strings.TrimPrefix(pkg, prefix), "/")
- return strings.Split(path, "/")[0]
-}
-
func redirect(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
var (
pkg = path.Join(req.Headers["Host"], req.Path)
- dest = getDest(os.Getenv("DEST"), getRepo(pkg, os.Getenv("PREFIX")))
- doc = fmt.Sprintf("https://godoc.org/%s", pkg)
- vcs = os.Getenv("VCS")
- body = fmt.Sprintf(`<!doctype html>
-<meta name="go-import" content="%s %s %s">
-<title>go-import-redirect</title>
-`, pkg, vcs, dest)
resp = events.APIGatewayProxyResponse{
- Body: body,
+ 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 = 200
+ resp.StatusCode = http.StatusOK
} else {
- resp.Headers["Location"] = doc
- resp.StatusCode = 302
+ resp.Headers["Location"] = "https://godoc.org/" + pkg
+ resp.StatusCode = http.StatusFound
}
return resp, nil
}
diff --git a/gcp/main.go b/gcp/main.go
new file mode 100644
index 0000000..c5c8466
--- /dev/null
+++ b/gcp/main.go
@@ -0,0 +1,35 @@
+// Copyright (c) 2019, Grégoire Duchêne <gduchene@awhk.org>
+//
+// 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(":"+port, nil))
+}
diff --git a/lib/lib.go b/lib/lib.go
new file mode 100644
index 0000000..b066e69
--- /dev/null
+++ b/lib/lib.go
@@ -0,0 +1,26 @@
+// Copyright (c) 2019, Grégoire Duchêne <gduchene@awhk.org>
+//
+// 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 := strings.TrimRight(os.Getenv("DEST"), "/") + "/" + getRepo(pkg)
+ return fmt.Sprintf(`<!doctype html>
+<meta name="go-import" content="%s %s %s">
+<title>go-import-redirect</title>
+`, pkg, os.Getenv("VCS"), dest)
+}
+
+func getRepo(pkg string) string {
+ prefix := strings.TrimRight(os.Getenv("PREFIX"), "/")
+ path := strings.TrimLeft(strings.TrimPrefix(pkg, prefix), "/")
+ return strings.Split(path, "/")[0]
+}