Skip to content

Commit

Permalink
🚜 refactor: Update server directory structure and implement user crea…
Browse files Browse the repository at this point in the history
…tion.
  • Loading branch information
taiseidev committed Nov 20, 2024
1 parent 0fa4d61 commit 704e256
Show file tree
Hide file tree
Showing 25 changed files with 256 additions and 114 deletions.
1 change: 1 addition & 0 deletions .cspell/framework-words.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gorm
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
"addWords": true,
"scope": "workspace"
},
"framework-words": {
"name": "framework-words",
"path": "${workspaceRoot}/.cspell/framework-words.txt",
"description": "Words specific to this framework",
"addWords": true,
"scope": "workspace"
},
},
"cSpell.ignorePaths": [
"dart_defines",
Expand Down
10 changes: 10 additions & 0 deletions server/.air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# プロジェクトのルートディレクトリ
root = "."
tmp_dir = "tmp"

[build]
cmd = "go build -o ./tmp/main ./cmd/api"
bin = "./tmp/main"
include_ext = ["go"]
exclude_dir = ["tmp", "vendor"]
log = "air.log"
47 changes: 0 additions & 47 deletions server/app/.air.toml

This file was deleted.

26 changes: 0 additions & 26 deletions server/app/Dockerfile

This file was deleted.

17 changes: 0 additions & 17 deletions server/app/main.go

This file was deleted.

Binary file removed server/app/tmp/main
Binary file not shown.
38 changes: 38 additions & 0 deletions server/cmd/api/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"camly-api/cmd/api/routes"
"camly-api/internal/user/handler"
"camly-api/internal/user/repository"
"camly-api/internal/user/service"
"log"

"github.com/labstack/echo/v4"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)

func main() {
e := echo.New()

dsn := "user:password@tcp(camly-db:3306)/camly-db?charset=utf8mb4&parseTime=True&loc=Local"

// データベースに接続
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatalf("failed to connect database: %v", err)
}

// リポジトリ、サービス、ハンドラーの初期化
userRepo := repository.NewUserRepository(db)
userService := service.NewUserService(userRepo)
userHandler := handler.NewUserHandler(userService)

// ルートを登録
routes.RegisterRoutes(e, userHandler)

// サーバーを起動
if err := e.Start(":8080"); err != nil {
log.Fatalf("failed to start server: %v", err)
}
}
15 changes: 15 additions & 0 deletions server/cmd/api/routes/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package routes

import (
"camly-api/internal/user/handler"

"github.com/labstack/echo/v4"
)

// 全てのルートを定義
func RegisterRoutes(e *echo.Echo, userHandler *handler.UserHandler) {
// ユーザー関連のルートを定義
userRoutes := e.Group("/users")
userRoutes.POST("", userHandler.CreateUser)

}
1 change: 1 addition & 0 deletions server/cmd/api/tmp/build-errors.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1
Binary file added server/cmd/api/tmp/main
Binary file not shown.
18 changes: 9 additions & 9 deletions server/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ version: "3.9"
services:
app:
build:
context: ./app
dockerfile: Dockerfile
context: .
dockerfile: ./docker/api/Dockerfile
container_name: camly-app
volumes:
- ./app:/app
- .:/app
- .air.toml:/.air.toml
working_dir: /app
ports:
- "8080:8080"
networks:
- golang_test_network

db:
container_name: camly-db
platform: linux/x86_64
build:
context: ./mysql
dockerfile: Dockerfile
image: mysql:8.0
tty: true
ports:
- "3306:3306"
env_file:
- ./mysql/.env_mysql
- ./docker/db/.env_mysql
volumes:
- mysql_data:/var/lib/mysql
- ./mysql/init:/docker-entrypoint-initdb.d
- ./docker/db/init:/docker-entrypoint-initdb.d
networks:
- golang_test_network

Expand Down
26 changes: 26 additions & 0 deletions server/docker/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# ベースイメージ
FROM golang:1.21-alpine

# 必要なツールをインストール
RUN apk update && apk add --no-cache git

# 作業ディレクトリを設定
WORKDIR /app/cmd/api

# go.mod と go.sum をコンテナにコピー
COPY go.mod go.sum ./

# 依存関係をダウンロード
RUN go mod download

# ソースコードをコピー
COPY ./cmd/api/ .

# アプリケーションで使用するポートを公開
EXPOSE 8080

# Airをインストール
RUN go install github.com/cosmtrek/[email protected]

# Airを使ってAPIサーバーを実行
CMD ["air", "-c", "../.air.toml"]
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions server/docker/db/init/create_table.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh

CMD_MYSQL="mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DATABASE}"

# articles テーブルの作成と初期データ挿入
$CMD_MYSQL -e "CREATE TABLE IF NOT EXISTS article (
id INT(10) AUTO_INCREMENT NOT NULL PRIMARY KEY,
title VARCHAR(50) NOT NULL,
body VARCHAR(1000)
);"
$CMD_MYSQL -e "INSERT INTO article (id, title, body) VALUES
(1, '記事1', '記事1です。'),
(2, '記事2', '記事2です。');"

# users テーブルの作成と初期データ挿入
$CMD_MYSQL -e "CREATE TABLE IF NOT EXISTS users (
id INT(10) AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE
);"
$CMD_MYSQL -e "INSERT INTO users (id, name, email) VALUES
(1, 'John Doe', '[email protected]'),
(2, 'Jane Smith', '[email protected]');"

echo "Database setup complete."
14 changes: 11 additions & 3 deletions server/app/go.mod → server/go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
module camly-app
module camly-api

go 1.19

require github.com/labstack/echo/v4 v4.12.0
require (
github.com/labstack/echo/v4 v4.12.0
gorm.io/driver/mysql v1.5.7
gorm.io/gorm v1.25.12
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand All @@ -13,5 +21,5 @@ require (
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/text v0.20.0 // indirect
)
18 changes: 16 additions & 2 deletions server/app/go.sum → server/go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0=
github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
Expand All @@ -22,6 +31,11 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo=
gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
36 changes: 36 additions & 0 deletions server/internal/user/handler/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package handler

import (
"camly-api/internal/user/model"
"camly-api/internal/user/service"
"net/http"

"github.com/labstack/echo/v4"
)

type UserHandler struct {
userService *service.UserService
}

func NewUserHandler(userService *service.UserService) *UserHandler {
return &UserHandler{
userService: userService,
}
}

func (h *UserHandler) CreateUser(c echo.Context) error {
// リクエストからデータをバインド
var req model.User
if err := c.Bind(&req); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request"})
}

// Serviceを呼び出し
user, err := h.userService.CreateUser(req.Name, req.Email)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to create user"})
}

// レスポンスとしてユーザーを返す
return c.JSON(http.StatusOK, user)
}
7 changes: 7 additions & 0 deletions server/internal/user/model/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package model

type User struct {
ID uint `gorm:"primaryKey"`
Name string `gorm:"size:255"`
Email string `gorm:"uniqueIndex;size:255"`
}
21 changes: 21 additions & 0 deletions server/internal/user/repository/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package repository

import (
"camly-api/internal/user/model"

"gorm.io/gorm"
)

type UserRepository struct {
db *gorm.DB
}

func NewUserRepository(db *gorm.DB) *UserRepository {
return &UserRepository{
db: db,
}
}

func (r *UserRepository) SaveUser(user *model.User) error {
return r.db.Create(user).Error
}
Loading

0 comments on commit 704e256

Please sign in to comment.