自分の参照用、備忘録、実験用に作成 テンプレートとしても活用
.
├── adapter
│ ├── controller
│ │ └── user.go
│ └── repository
│ └── user.go
├── db
│ └── migrations # dbスキーママイグレーション
├── doc # ドキュメントなど
├── docker-compose.yml
├── Dockerfile
├── domain
│ └── user.go
├── ent
│ └── schema # DBスキーマはここに格納
├── go.mod
├── go.sum
├── go.test.sh
├── infrastructure
│ ├── httphandler.go
│ ├── postgres.go
│ ├── router.go
│ └── server.go
├── main.go
├── Makefile
├── middleware
├── README.md
├── reference
│ └── user
│ └── schema.yml
├── testdata
│ └── http # 手動テスト
├── testutil
│ └── testutil.go
├── tmp
│ ├── build-errors.log
│ └── main
└── usecase
├── request.go
└── user.go
testdata/http 内の File を Rest Client で試す
# 実行
make run
# Automatic テスト
make test
# Database 接続
make db-in
# PostgreSQL 使用
psql template -U test
- go-playground/validator/10 を使用
- validator package · pkg.go.dev カスタムする場合は参照
docker-compose.yml
のPOSTGRES_DB
で指定したデータベースを作成
golang-migrate/migrate をインストール
# Use on Mac
brew install golang-migrate
# Use on Windows
scoop install migrate
make mig-cr name={分かりやすい名前}
以下のような File が db/migrations
以下に作成
000001_create_users_table.down.sql
000001_create_users_table.up.sql
DDL を up.sql に書き、それを打ち消す SQL を down.sql に書く
注:IF (NOT) EXISTS をつけること。
まずデータベースの接続情報を環境変数に登録しておく
dbdriver://username:password@host:port/dbname?option1=true&option2=false
export POSTGRESQL_URL='postgres://test:test@localhost:5432/test?sslmode=disable'
# migration
make mig-up
# rollback
make mig-down
# migration version
make mig-v
CloudSQL はデフォルトのタイムゾーンがロンドンのため、
ALTER DATABASE {DB名} SET timezone TO 'Asia/Tokyo';
を最初に実行する必要がある。
詳細は
go.mod
を確認。
目的 | ライブラリ | ドキュメントリンク |
---|---|---|
WAF | Chi | go-chi/chi: lightweight, idiomatic and composable router for building Go HTTP services |
ORM | facebook/ent | ent |
Logging | ZAP | go-chi/httplog |
Errors | errors | errors - Go 言語 |