2014年10月26日日曜日

GAE Go で static なサイトをホストする

0. python 2.7 が必要

1. https://cloud.google.com/appengine/downloads#Google_App_Engine_SDK_for_Go から SDK をダウンロードして解凍する

2. PATH に解凍したディレクトリを設定する

export PATH=/path/to/go_appengine:$PATH

3. フォルダ構成

myapp/ app.yaml index.html img/ css/ js/ src/ main.go

4. app.yaml

application: myapp-application-id version: 1 runtime: go api_version: go1 handlers: - url: /(.*\.html)$ static_files: \1 upload: .*\.html$ - url: /img static_dir: img - url: /css static_dir: css - url: /js static_dir: js - url: /.* script: _go_app

5. main.go

static dir / static files 以外の全ての URL を /index.html にリダイレクトする package myapp import ( "net/http" ) func init() { http.HandleFunc("/", handler) } func handler(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "index.html", http.StatusFound) }

6. ローカルでテスト

$ cd myapp $ goapp serve localhost:8080 で実行される

7. Deploy

$ cd myapp $ goapp deploy --oauth