app.yaml変更点
go1.11になって少し記述が変わっている。
変更前
runtime: go api_version: go1 handlers: - url: /.* script: _go_app
変更後
runtime: go111 handlers: - url: /.* script: auto
api_versionは廃止。
runtimeは1.21の場合はgo112を指定する。
ローカルで動かすときとGAE上で動かすときとで静的ファイルの配置を同じにする
VueCLIからビルドするとjsやcssなどdist以下に配置されるがindex.htmlのパスが/js/...になるので表示されない。
VueとGAE/Goが絶妙に噛み合っていない。
以下で紹介する方法が一番手間がないはず。
Google App Engine
app.yamlに静的ファイルの配置場所を指定するだけでいい。
Vueのデフォルトの出力先がdist以下なので少しトリックが必要。
# アクセスがあったときの挙動 handlers: # 静的ファイル - url: /js static_dir: dist/js - url: /css static_dir: dist/css - url: /dist/img static_dir: img - url: /dist/manifest.json static_dir: dist/manifest.json - url: /.* script: auto
Go
当然だがローカルから起動した場合はapp.yamlを参照してくれない。
静的ファイルの指定をしないといけないがGoogle App Engineにアップロードしても挙動は変わらないようにしたい。
// [START func_root]
func root(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
if os.Args[1] == "local" {
// local
log.Printf("url %s", r.URL.Path[1:])
http.ServeFile(w, r, "dist"+r.URL.Path)
} else {
http.NotFound(w, r)
}
return
}
// VueCLIから出力したhtmlファイル
var indexTmpl, err = template.ParseFiles("dist/index.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err = indexTmpl.Execute(w, "test"); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// [END func_root]
func main() {
// go run main.go local でlocalが入る
// log.Printf(os.Args[1])
http.HandleFunc("/", root)
// ....
}
ローカルから確認したいときはgo run main.go local
go1.11あたりから普通のGoを使う必要がある。]
参考ソース
最小構成をGitHubにアップロードしている。
https://github.com/ninomae-makoto/googleappengine-go-vue
app.yamlについては以下にまとめている。
https://trueman-developer.blogspot.com/2018/02/google-app-engine-appyaml.html
Visual Stadio CodeでGo開発環境。
https://trueman-developer.blogspot.com/2017/05/visual-studio-codegowindows.html