Skip to content

Commit

Permalink
Merge pull request YJU-OKURA#6 from yuminn-k/init/set-project
Browse files Browse the repository at this point in the history
  • Loading branch information
yuminn-k authored Feb 11, 2024
2 parents a82c286 + 1549766 commit eaff054
Show file tree
Hide file tree
Showing 23 changed files with 208 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENV=prod
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
vendor/*
!vendor/vendor.json
coverage.out
count.out
test
profile.out
tmp.out
.env
3 changes: 3 additions & 0 deletions constants/error_messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package constants

// 에러 메시지를 상수로 정의한 파일
3 changes: 3 additions & 0 deletions constants/http_status_codes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package constants

// HTTP 상태 코드를 상수로 정의한 파일
3 changes: 3 additions & 0 deletions constants/roles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package constants

// 사용자 역할(예: 관리자, 일반 사용자 등)을 상수로 정의한 파일
1 change: 1 addition & 0 deletions controllers/user_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package controllers
1 change: 1 addition & 0 deletions dto/user_dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package dto
82 changes: 82 additions & 0 deletions infra/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package infra

import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"os"
"time"
)

type SQLHandler struct {
DB *gorm.DB
Err error
}

var dbConn *SQLHandler

func DBOpen() {
dbConn = SetupDB()
}

func DBClose() {
sqlDB, _ := dbConn.DB.DB()
sqlDB.Close()
}

func SetupDB() *SQLHandler {
user := os.Getenv("DB_USERNAME")
password := os.Getenv("DB_PASSWORD")
host := os.Getenv("DB_HOST")
port := os.Getenv("DB_PORT")
dbName := os.Getenv("DB_DATABASE")
fmt.Println(user, password, host, port)

var db *gorm.DB
var err error
// Todo: USE_HEROKU = 1のときと場合分け
if os.Getenv("USE_HEROKU") != "1" {
dsn := user + ":" + password + "@tcp(" + host + ":" + port + ")/" + dbName + "?parseTime=true&loc=Asia%2FTokyo"
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic(err)
}
} /*else {
var (
instanceConnectionName = os.Getenv("DB_CONNECTION_NAME") // e.g. 'project:region:instance'
)
dbURI := fmt.Sprintf("%s:%s@unix(/cloudsql/%s)/%s?parseTime=true", user, password, instanceConnectionName, database)
// dbPool is the pool of database connections.
db, err = gorm.Open(mysql.Open(dbURI), &gorm.Config{})
if err != nil {
panic(err)
}
}*/

sqlDB, _ := db.DB()
//コネクションプールの最大接続数を設定。
sqlDB.SetMaxIdleConns(100)
//接続の最大数を設定。 nに0以下の値を設定で、接続数は無制限。
sqlDB.SetMaxOpenConns(100)
//接続の再利用が可能な時間を設定。dに0以下の値を設定で、ずっと再利用可能。
sqlDB.SetConnMaxLifetime(100 * time.Second)

sqlHandler := new(SQLHandler)
db.Logger.LogMode(4)
sqlHandler.DB = db

return sqlHandler
}

func GetDBConn() *SQLHandler {
return dbConn
}

func BeginTransaction() *gorm.DB {
dbConn.DB = dbConn.DB.Begin()
return dbConn.DB
}

func RollBack() {
dbConn.DB.Rollback()
}
13 changes: 13 additions & 0 deletions infra/initializer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package infra

import (
"github.com/joho/godotenv"
"log"
)

func Initialize() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading env file")
}
}
49 changes: 49 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"net/http"
)

// @title APIドキュメントのタイトル
// @version バージョン(1.0)
// @description 仕様書に関する内容説明
// @termsOfService 仕様書使用する際の注意事項

// @contact.name APIサポーター
// @contact.url http://www.swagger.io/support
// @contact.email [email protected]

// @license.name ライセンス(必須)
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @host localhost:8080
// @BasePath /
func main() {
//infra.Initialize()
r := gin.New()

url := ginSwagger.URL("http://localhost:8080/swagger/doc.json")
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))

r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})

r.GET("/test", test)
_ = r.Run("localhost:8080")
}

// @description テスト用APIの詳細
// @version 1.0
// @accept application/x-json-stream
// @param none query string false "必須ではありません。"
// @Success 200 {object} gin.H {"code":200,"msg":"ok"}
// @router /test/ [get]
func test(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"msg": "ok"})
}
Empty file added makeFile
Empty file.
26 changes: 26 additions & 0 deletions middlewares/authentication_middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package middlewares

import (
"github.com/gin-gonic/gin"
"net/http"
//"github.com/swaggo/gin-swagger"
//"github.com/swaggo/files"
//docs "github.com/YJU-OKURA/project_minori-gin-deployment-repo/backend/docs"
)

// 사용자의 인증 상태를 확인하고 관리하는 미들웨어를 포함

// @BasePath /api/v1

// PingExample godoc
// @Summary ping example
// @Schemes
// @Description do ping
// @Tags example
// @Accept json
// @Produce json
// @Success 200 {string} Helloworld
// @Router /example/helloworld [get]
func Helloworld(g *gin.Context) {
g.JSON(http.StatusOK, "Hello World")
}
3 changes: 3 additions & 0 deletions middlewares/error_handling_middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package middlewares

// 에러 발생 시 적절하게 처리하는 미들웨어를 포함
1 change: 1 addition & 0 deletions middlewares/logging_middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package middlewares
1 change: 1 addition & 0 deletions migration/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
1 change: 1 addition & 0 deletions migration/migrations/1_users.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
Empty file.
1 change: 1 addition & 0 deletions models/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package models
1 change: 1 addition & 0 deletions repositories/user_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package repositories
1 change: 1 addition & 0 deletions services/user_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package services
3 changes: 3 additions & 0 deletions utils/date_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package utils

// 날짜와 시간 관련 유틸리티 함수를 포함
3 changes: 3 additions & 0 deletions utils/string_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package utils

// 문자열 처리 관련 유틸리티 함수를 포함
3 changes: 3 additions & 0 deletions utils/validation_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package utils

// 데이터 유효성 검사 관련 유틸리티 함수를 포함

0 comments on commit eaff054

Please sign in to comment.