HugoをGAEで公開時に苦戦した話

# はじめに

最近、Hugoでブログを作って、GCPのGAE(Google App Engine)で公開した。 
[公開したブログ](https://sakojun.work/2020/02/hugo%E3%81%A7blog%E4%BD%9C%E3%81%A3%E3%81%A6gae%E3%81%A7%E5%85%AC%E9%96%8B%E3%81%97%E3%81%9F/)

選定理由などはブログに書いているが、GAEに公開する所でちょっと苦戦した部分を記載する。

# 参考にしたサイト

Hugoでブログつくるまで 
https://qiita.com/bake0937/items/e0914efbd9434be474a4

GAEでの公開 
https://blog.youyo.info/post/2015/10/23/publish-hugo-in-gae/

# GAEへの公開でエラー

## deployでエラー１

```bash
$ gcloud app deploy
 ERROR: (gcloud.app.deploy) The [application] field is specified in file [/home/hoge/dir/app.yaml]. This field is not used by gcloud and must be removed. Project name should instead be specified either by `gcloud config set project MY_PROJECT` or by setting the `--project` flag on individual command executions.
 ```

- yamlの様式が変わった模様 
  http://hamarimakuri.blogspot.com/2018/03/gae.html

- [ドキュメント](https://cloud.google.com/appengine/docs/standard/go111/config/appref?hl=ja)みて修正  

## deployでエラー2

```bash
$ gcloud app deploy
 The component [app-engine-go] is required for staging this 
 application.
 ERROR: (gcloud.app.deploy) You cannot perform this action because this Cloud SDK installation is managed by an external package manager.
 Please consider using a separate installation of the Cloud SDK created through the default mechanism described at: https://cloud.google.com/sdk/
```

- gcloudコマンドをaptで入れたため、古かったのが原因っぽい 
  https://qrunch.net/@regonn/entries/sIescQ9qdgJZ0qWa

- [インストーラ使って再インストール](https://cloud.google.com/sdk/docs/downloads-interactive)した  
  
## deployでエラー3

```bash
$ gcloud app deploy
 ERROR: (gcloud.app.deploy) Staging command [/home/hoge/google-cloud-sdk/platform/google_appengine/go-app-stager /home/hoge/dir/app.yaml /home/hoge/dir /tmp/tmprq6eNx/tmpSQWQij] failed with return code [1].
 
 ------------------------------------ STDOUT ------------------------------------
 ------------------------------------ STDERR ------------------------------------
 2020/01/26 18:44:00 staging for go1.11
 2020/01/26 18:44:00 GO111MODULE=auto, but no go.mod found, so building with dependencies from GOPATH
 2020/01/26 18:44:00 Staging second-gen Standard app (GOPATH mode): failed analyzing /home/hoge/dir: could not get package for dir "/home/hoge/dir": no buildable Go source files in /home/hoge/dir
 GOPATH: /home/hoge/dir/.go
 --------------------------------------------------------------------------------
```

- goのコードが存在しない事が原因のよう
- [ドキュメント](https://cloud.google.com/appengine/docs/standard/go111/go-differences?hl=ja)と[GitHubのサンプル](https://github.com/GoogleCloudPlatform/golang-samples/blob/master/appengine/go11x/helloworld/helloworld.go
)を見つつ、`main.go`を作成


デプロイできた！
  

# 出来上がった設定ファイル

```yaml:app.yaml
runtime: go111

handlers:
- url: /(.*\.css)
  mime_type: text/css
  static_files: public/\1
  upload: public/(.*\.css)

- url: /(.*\.html)
  mime_type: text/html
  static_files: public/\1
  upload: public/(.*\.html)
  secure: always

- url: /(.*\.ico)
  mime_type: image/x-icon
  static_files: public/\1
  upload: public/(.*\.ico)

- url: /(.*\.js)
  mime_type: text/javascript
  static_files: public/\1
  upload: public/(.*\.js)

- url: /(.*\.ttf)
  mime_type: font/truetype
  static_files: public/\1
  upload: public/(.*\.ttf)

- url: /(.*\.woff)
  mime_type: application/x-font-woff
  static_files: public/\1
  upload: public/(.*\.woff)

- url: /(.*\.xml)
  mime_type: application/xml
  static_files: public/\1
  upload: public/(.*\.xml)

- url: /(.*\.(bmp|gif|ico|jpeg|jpg|png))
  static_files: public/\1
  upload: public/(.*\.(bmp|gif|ico|jpeg|jpg|png))

- url: /(.+)/
  static_files: public/\1/index.html
  upload: public/(.+)/index.html
  secure: always

- url: /(.+)
  static_files: public/\1/index.html
  upload: public/(.+)/index.html
  secure: always

- url: /
  static_files: public/index.html
  upload: public/index.html
  secure: always
```
※元の設定ファイルだとhttpでアクセスできてしまうので「secure: always」を追加しました。
[Google App Engineにデプロイしたアプリへの接続を常時https化する](https://qiita.com/Shin-27/items/876de3162ce0fd616555)

```go:main.go
package main

import (
//	"fmt"
	"log"
	"net/http"
	"os"
)

func main() {
//	http.HandleFunc("/", indexHandler)

	// [START setting_port]
	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)
	}
	// [END setting_port]
}
```

# 最後に

ちょっと苦戦はしたけど、Hugoは楽ちんです！