Categorygithub.com/taaaaakahiro/golang-rest-example
module
0.0.0-20240725074859-e908baaa3d66
Repository: https://github.com/taaaaakahiro/golang-rest-example.git
Documentation: pkg.go.dev

# README

golang-rest-example

my version

Docker version 20.10.12, build e91ed57
docker-compose version 1.29.2, build 5becea4c

setup

install

run DB/DBコンテナを起動

$ docker-compose up -d #DBコンテナ起動
$ docker-compose down #DBコンテナ停止

run app/api serverを起動

  1. setup environment/環境変数を設定(内容は.env.sampleファイル参照)
    $ export PORT=<PORT>
    $ export MYSQL_DSN=<MYSQL_DSN>
    $ export ALLOW_CORS_ORIGIN=<ALLOW_CORS_ORIGIN>
    
  2. command/ローカルでapi serverを起動
    $ make run #go run ./cmd/api/main.go
    
  3. check endpoint/ブラウザで動作確認
    $ localhost:<PORT>/version #ex  localhost:8080/version
    $ localhost:<PORT>/healthz #ex localhost:8080/healthz
    

test with linux/goでテストを実行

$ make test #go test ./...

curl/http request/HTTPリクエスト(動作確認結果のキャプチャあり)

$ curl -X GET localhost:8080/version #バージョン確認(動作確認も兼ねています)
$ curl -X GET localhost:8080/v1/user/{id} -H "Content-Type: application/json" #idを指定して該当のuserを取得
$ curl -X GET localhost:8080/v1/user/all -H "Content-Type: application/json" #userテーブルの一覧(全件)を取得
$ curl -X POST localhost:8080/v1/user -H "Content-Type: application/json" --data-raw '{"name": "user"}' #usersテーブルに指定したnameのuserレコードを追加
$ curl -X DELETE localhost:8080/v1/user/{id} -H "Content-Type: application/json" #idを指定して該当のuserをテーブルから削除
$ curl -X POST localhost:8080/v1/review -H "Content-Type: application/json" --data-raw '{"user_id": 1,"text":"xyz"}' #reviewsテーブルへ登録 ※指定のuser_idがusersテーブルにあること、user_idは数値(文字列だと型が合わない&リクエストするデータは空白なし)

Architecture

DDD/Clean Architecture

pkg

  • command: 各pkgを初期化
  • config: 設定を管理
  • domain: 目的の明確化
  • handler: ルーティング
  • infrastructure
    • persistence: クエリ
  • io: DB接続
  • middleware: リクエストの前処理等
  • service: 複数のUseCaseを纏める
  • server: API Serverの設定
  • version: toolバージョン管理

追加予定の機能

  • テンプレートエンジン 2023/2/6 up
  • csvインポート
  • csvエクスポート
  • ファイルアップロード
  • メール送信
  • gorilla → chi 2023/2/11 up

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author