diff options
| author | Grégoire Duchêne <gduchene@awhk.org> | 2019-09-09 21:18:59 +0100 |
|---|---|---|
| committer | Grégoire Duchêne <gduchene@awhk.org> | 2019-09-09 21:55:09 +0100 |
| commit | 0c050372cc95f231f68164ff2d8164d085ea3ccc (patch) | |
| tree | c4c6e338896516a8a3c18e7a2682dd1f80a3dbc1 /gcp | |
| parent | fc1a6692348673e9703b09ae22e7cdd93c67e4ff (diff) | |
Add a version for GCP Cloud Run
Diffstat (limited to 'gcp')
| -rw-r--r-- | gcp/main.go | 35 |
1 files changed, 35 insertions, 0 deletions
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)) +} |
