Open2

Air+Echo+docker-composeのAPIサーバの環境構築を調べる

み

やりたいこと

  • Air+Echo+docker-composeを使ったAPIサーバの環境構築

使用技術・環境など

  • Golang: v1.13
  • Docker
  • docker-compose
  • Air
    • ホットリロードライブラリで、realizeの代替として期待できそうらしい

記事メモ

基本的に参考にする記事はこちら↓

他の参考文献としては公式Docs

み

詰まったポイントの備忘録

ちなみに、解決後に見返すと全体的に基本的な内容な気がして悲しい。。。

その1:Goモジュールまわり

問題点

my-api             |   __    _   ___  
my-api             |  / /\  | | | |_) 
my-api             | /_/--\ |_| |_| \_ , built with Go 
my-api             |
my-api             | mkdir /go/src/tmp
my-api             | watching .
my-api             | watching app
my-api             | building...
my-api             | go: cannot find main module, but found .git/config in /go
my-api             | 	to create a module there, run:
my-api             | 	cd .. && go mod init
my-api             | failed to build, error: exit status 1

解決策

go.modファイルを作成すれば解決した

具体的には、APIのコンテナに入って以下のコマンドで作成

go mod init [任意のpackage名]

その2:build pathまわり

問題点

my-api             |   __    _   ___  
my-api             |  / /\  | | | |_) 
my-api             | /_/--\ |_| |_| \_ , built with Go 
my-api             | 
my-api             | mkdir /go/src/tmp
my-api             | watching .
my-api             | watching app
my-api             | building...
my-api             | build .: cannot find module for path .
my-api             | failed to build, error: exit status 1

解決策

今回採用したホットリロードライブラリAirの設定ファイル.air.tomlを修正すれば解決した

.air.toml
[build]
# Just plain old shell command. You could use `make` as well.
cmd = "go build -o ./tmp/main ./app/main.go"

公式サンプルair/air_example.toml at master · cosmtrek/airをそのままコピペしていたのがよくなかった
公式サンプルと異なり、buildコマンド部分に、./app/main.goを追記する形にしたところ上手く動くようになった

ちなみに、自分の環境のファイル構造は以下(今回関係ある点のみ抜き出し)

src 
┣ app
    ┗ main.go
┗ .air.toml

参考