Skip to content

spilebull/bull

Repository files navigation

Clean architecture with Go language templates

Build Test

目的

自分の参照用、備忘録、実験用に作成 テンプレートとしても活用

ディレクトリ構成

.
├── 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

Request Data Validation

Database

  • docker-compose.ymlPOSTGRES_DBで指定したデータベースを作成

Database Migration

以下方法

golang-migrate/migrate をインストール

# Use on Mac
brew install golang-migrate

# Use on Windows
scoop install migrate

Database Migration File

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 をつけること。

Database Migration Execution

まずデータベースの接続情報を環境変数に登録しておく

接続情報は以下のような形式

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 のタイムゾーンについてに注意点

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 言語

詳細なドキュメントへのリンク

参考 URL

About

Clean Architecture + Microservice + Golang

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published