[A-00136]App EngineでGoアプリを動かしてみる
Google CloudのApp Engineの使い方についてまとめ
golangで作成したappを動かしてみます。
下記のディレクトリ構造でプロジェクトを作成します。

.gcloudignoreはgcloudコマンドを実行した時に自動で作成されるファイルのため、作る必要はありません。
他2ファイルを下記のように作成します。
runtime: go115
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
http.HandleFunc("/", indexHandler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
log.Printf("Listening on port %s", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
fmt.Fprint(w, "Hello, World!")
}
上記を作成したら下記のコマンドを実行します。
gcloud app create
gcloud app deploy
上記が完了したら下記のコマンドでWebブラウザより確認します。
gcloud app browse
下記のような画面が表示されるはずです。

上記のアプリケーションはデプロイしたままにすると課金されるので下記の手順で無効にします。
[設定]タブを押下して、[アプリケーションを無効にする]を押下します。

・Nodejsでも動かしてみる
・Appendix
公式ドキュメントはこちら
https://cloud.google.com/appengine/docs/standard/go/building-app?hl=ja#cloud-shell
https://cloud.google.com/appengine/docs/standard/python3/building-app/cleaning-up?hl=ja
コメントを残す