Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

バックエンドのディレクトリ整備 #44

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions backend/controllers/friends_controller.go
Original file line number Diff line number Diff line change
@@ -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)
}
38 changes: 38 additions & 0 deletions backend/controllers/login_controller.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion backend/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module sample
module gomoku

go 1.22.6

Expand Down
14 changes: 3 additions & 11 deletions backend/main.go
Original file line number Diff line number Diff line change
@@ -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")
}
10 changes: 10 additions & 0 deletions backend/models/database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package database

import (
"gorm.io/gorm"
)

// Database struct
type Database struct {
*gorm.DB
}
11 changes: 11 additions & 0 deletions backend/models/friend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package models

import (
"gorm.io/gorm"
)

// Friend struct
type Friend struct {
gorm.Model
}

10 changes: 10 additions & 0 deletions backend/models/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

import (
"gorm.io/gorm"
)

// User struct
type User struct {
gorm.Model
}
27 changes: 27 additions & 0 deletions backend/routes/router.go
Original file line number Diff line number Diff line change
@@ -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
}
33 changes: 33 additions & 0 deletions docs/02.Backend/00.DirStructure/README.md
Original file line number Diff line number Diff line change
@@ -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は存在しない。