generated from taiseidev/flutter-base
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚜 refactor: Update server directory structure and implement user crea…
…tion.
- Loading branch information
Showing
25 changed files
with
256 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
gorm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.