forked from YJU-OKURA/project_minori-gin-deployment-repo
-
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.
Merge pull request YJU-OKURA#6 from yuminn-k/init/set-project
- Loading branch information
Showing
23 changed files
with
208 additions
and
0 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 @@ | ||
ENV=prod |
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,8 @@ | ||
vendor/* | ||
!vendor/vendor.json | ||
coverage.out | ||
count.out | ||
test | ||
profile.out | ||
tmp.out | ||
.env |
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,3 @@ | ||
package constants | ||
|
||
// 에러 메시지를 상수로 정의한 파일 |
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,3 @@ | ||
package constants | ||
|
||
// HTTP 상태 코드를 상수로 정의한 파일 |
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,3 @@ | ||
package constants | ||
|
||
// 사용자 역할(예: 관리자, 일반 사용자 등)을 상수로 정의한 파일 |
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 @@ | ||
package controllers |
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 @@ | ||
package dto |
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,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() | ||
} |
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,13 @@ | ||
package infra | ||
|
||
import ( | ||
"github.com/joho/godotenv" | ||
"log" | ||
) | ||
|
||
func Initialize() { | ||
err := godotenv.Load() | ||
if err != nil { | ||
log.Fatal("Error loading env file") | ||
} | ||
} |
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,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"}) | ||
} |
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 @@ | ||
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") | ||
} |
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,3 @@ | ||
package middlewares | ||
|
||
// 에러 발생 시 적절하게 처리하는 미들웨어를 포함 |
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 @@ | ||
package middlewares |
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 @@ | ||
package main |
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 @@ | ||
package main |
Empty file.
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 @@ | ||
package models |
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 @@ | ||
package repositories |
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 @@ | ||
package services |
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,3 @@ | ||
package utils | ||
|
||
// 날짜와 시간 관련 유틸리티 함수를 포함 |
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,3 @@ | ||
package utils | ||
|
||
// 문자열 처리 관련 유틸리티 함수를 포함 |
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,3 @@ | ||
package utils | ||
|
||
// 데이터 유효성 검사 관련 유틸리티 함수를 포함 |