diff --git a/backend/controllers/friends_controller.go b/backend/controllers/friends_controller.go new file mode 100644 index 0000000..fa685c1 --- /dev/null +++ b/backend/controllers/friends_controller.go @@ -0,0 +1,29 @@ +package controllers + +import ( + "github.com/gin-gonic/gin" + "net/http" +) + +// FriendsController is a struct to define the friends controller +type FriendsController struct { +} + +// NewFriendsController is a function to create a new friends controller +func NewFriendsController() *FriendsController { + return &FriendsController{} +} + + +// GetFriendsResponse is a struct to define the get friends response +type GetFriendsResponse struct { + Message string `json:"message"` +} + +// GetFriends is a function to handle the get friends request +func (controller *FriendsController) GetFriends(c *gin.Context) { + + var response GetFriendsResponse + response.Message = "Get friends successful" + c.JSON(http.StatusOK, response) +} diff --git a/backend/controllers/login_controller.go b/backend/controllers/login_controller.go new file mode 100644 index 0000000..8447600 --- /dev/null +++ b/backend/controllers/login_controller.go @@ -0,0 +1,38 @@ +package controllers + +import ( + "github.com/gin-gonic/gin" + "net/http" +) + +// LoginController is a struct to define the login controller +type LoginController struct { +} + +// NewLoginController is a function to create a new login controller +func NewLoginController() *LoginController { + return &LoginController{} +} + +// LoginRequest is a struct to define the login request +type LoginRequest struct { + Message string `json:"message" binding:"required"` +} + +// LoginResponse is a struct to define the login response +type LoginResponse struct { + Message string `json:"message"` +} + +// Login is a function to handle the login request +func (controller *LoginController) Login(c *gin.Context) { + var request LoginRequest + if err := c.ShouldBindJSON(&request); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + var response LoginResponse + response.Message = "Login successful" + c.JSON(http.StatusOK, response) +} \ No newline at end of file diff --git a/backend/go.mod b/backend/go.mod index a37ae39..ca200dd 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -1,4 +1,4 @@ -module sample +module gomoku go 1.22.6 diff --git a/backend/main.go b/backend/main.go index 003efd7..16c260d 100644 --- a/backend/main.go +++ b/backend/main.go @@ -1,18 +1,10 @@ package main import ( - "net/http" - "github.com/gin-gonic/gin" - _ "github.com/lib/pq" + "gomoku/routes" ) func main() { - r := gin.Default() - - r.GET("/", func(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{ - "message": "Hello World", - }) - }) - r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") + router := routes.GetRouter() + router.Run(":8080") } diff --git a/backend/models/database/database.go b/backend/models/database/database.go new file mode 100644 index 0000000..9ff732a --- /dev/null +++ b/backend/models/database/database.go @@ -0,0 +1,10 @@ +package database + +import ( + "gorm.io/gorm" +) + +// Database struct +type Database struct { + *gorm.DB +} diff --git a/backend/models/friend.go b/backend/models/friend.go new file mode 100644 index 0000000..8459720 --- /dev/null +++ b/backend/models/friend.go @@ -0,0 +1,11 @@ +package models + +import ( + "gorm.io/gorm" +) + +// Friend struct +type Friend struct { + gorm.Model +} + diff --git a/backend/models/user.go b/backend/models/user.go new file mode 100644 index 0000000..a40a8ca --- /dev/null +++ b/backend/models/user.go @@ -0,0 +1,10 @@ +package models + +import ( + "gorm.io/gorm" +) + +// User struct +type User struct { + gorm.Model +} \ No newline at end of file diff --git a/backend/routes/router.go b/backend/routes/router.go new file mode 100644 index 0000000..f611646 --- /dev/null +++ b/backend/routes/router.go @@ -0,0 +1,27 @@ +package routes + +import ( + "github.com/gin-gonic/gin" + "gomoku/controllers" +) + +func GetRouter() *gin.Engine { + router := gin.Default() + + api := router.Group("/api/v1") + { + users := api.Group("/users") + { + loginController := controllers.NewLoginController() + users.POST("/login", loginController.Login) + } + + friends := api.Group("/friends") + { + friendsController := controllers.NewFriendsController() + friends.GET("/", friendsController.GetFriends) + } + } + + return router +} \ No newline at end of file diff --git a/docs/02.Backend/00.DirStructure/README.md b/docs/02.Backend/00.DirStructure/README.md index 151b177..ed63b9f 100644 --- a/docs/02.Backend/00.DirStructure/README.md +++ b/docs/02.Backend/00.DirStructure/README.md @@ -1 +1,34 @@ # 00.DirStructure + +バックエンドのディレクトリ構造は以下のようになっている。 + +```bash +backend/ +├── Dockerfile +├── README.md +├── controllers +├── go.mod +├── go.sum +├── main.go +├── middleware +├── models +│ └── database +└── routes +``` + +1. **Dockerfile**: バックエンドイメージをビルドするためのDockerfile +2. **README.md**: このファイル +3. **controllers**: バックエンドのコントローラー +4. **go.mod**: Goモジュールファイル +5. **go.sum**: Goのサムファイル +6. **main.go**: バックエンドのエントリーポイント +7. **middleware**: バックエンドのミドルウェア(現状空の為、git未追跡) +8. **models**: バックエンドのモデル + - **database**: +9. **routes**: バックエンドのルート +10. **tmp**: Airによって作成されたバックエンドの一時ディレクトリ + + +基本的にMVC(Model-View-Controller)パターンに従ってディレクトリ構造を設計している。 +開発者のレベルを考慮し、分かりやすいアーキテクチャを選択した。 +現状バックエンドは単にAPIサーバーとしての機能しか持たない予定のため、Viewは存在しない。