Last active
August 1, 2022 06:26
-
-
Save tatsuyasusukida/99b67b327f836fd531bee7e8211e702d to your computer and use it in GitHub Desktop.
Cloud Run golang redirect server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module loremipsum.co.jp/redirect | |
go 1.18 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net/http" | |
"os" | |
) | |
func main () { | |
location := os.Getenv("LOCATION") | |
if location == "" { | |
log.Fatal("location == \"\"") | |
} | |
http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Location", location) | |
w.WriteHeader(http.StatusMovedPermanently) | |
}) | |
port := os.Getenv("PORT") | |
if port == "" { | |
port = "3000" | |
} | |
log.Printf("Listening on %s\n", port) | |
log.Fatal(http.ListenAndServe(":" + port, nil)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: | |
deploy: | |
source .env && gcloud run deploy $$SERVICE \ | |
--source . \ | |
--region asia-northeast1 \ | |
--platform managed \ | |
--allow-unauthenticated | |
gist: | |
source .env && gist -u $$GIST_ID \ | |
-od "Cloud Run golang redirect server" \ | |
go.mod main.go makefile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment