Last active
December 20, 2021 12:45
-
-
Save syossan27/b360b55ee758bdad4a6e97b3bcf8aca5 to your computer and use it in GitHub Desktop.
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
// 指定した開発環境にデプロイしたPR/Commitへ遷移 | |
package p | |
import ( | |
"context" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"cloud.google.com/go/storage" | |
) | |
func redirectUrl(w http.ResponseWriter, r *http.Request) { | |
// 指定したアプリ/開発環境を取得 | |
envParam, ok := r.URL.Query()["env"] | |
if !ok || len(envParam) == 0 { | |
log.Fatal("Invalid env param") | |
} | |
numParam, ok := r.URL.Query()["num"] | |
if !ok || len(numParam) == 0 { | |
log.Fatal("Invalid num param") | |
} | |
env := envParam[0] | |
num := numParam[0] | |
// Cloud Storageにある遷移先テキストファイルを参照 | |
ctx := context.Background() | |
client, err := storage.NewClient(ctx) | |
if err != nil { | |
log.Fatal(err) | |
} | |
obj := client.Bucket("example-bucket").Object(env + "/" + num + ".txt") | |
reader, err := obj.NewReader(ctx) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer reader.Close() | |
content, err := ioutil.ReadAll(reader) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// 遷移先テキストファイルに記述しているURLにリダイレクト | |
w.Header().Set("Content-Type", "text/html") | |
w.Header().Set("Cache-Control", "no-store") | |
w.Header().Set("location", string(content)) | |
w.WriteHeader(http.StatusMovedPermanently) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment