Skip to content
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
28 changes: 28 additions & 0 deletions cmd/jirigo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import (
"log"
"time"

"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/jslee/JiRigo/pkg/infra/config"
"github.com/jslee/JiRigo/pkg/infra/db"
"github.com/jslee/JiRigo/pkg/infra/postgres"
"github.com/jslee/JiRigo/pkg/infra/routing"
"github.com/jslee/JiRigo/pkg/services/migrations"
"github.com/jslee/JiRigo/pkg/services/signin/signinapi"
"github.com/jslee/JiRigo/pkg/services/signin/signinimpl"
_ "github.com/lib/pq" // PostgreSQL 드라이버 임포트 추가
)

Expand Down Expand Up @@ -88,6 +92,21 @@ func initDatabase(dbConfig *config.DatabaseConfig) (db.DB, error) {
return gormDB, nil
}

func setupServer(db db.DB) *gin.Engine {
engine := gin.Default()

// 라우터 등록기 생성
routeRegister := routing.NewGinRouteRegister(engine)

// 서비스 제공
signinService, _ := signinimpl.ProvideService(db)

// API 컨트롤러 초기화
signinapi.ProvideSigninAPI(routeRegister, signinService)

return engine
}

func main() {
// 환경 변수 로드
loadEnv()
Expand All @@ -108,5 +127,14 @@ func main() {
// API 서버 초기화 및 실행
// TODO: 서버 시작 로직 추가

// 서버 설정 (setupServer 함수 호출 및 서버 실행)
engine := setupServer(gormDB)

// TODO_JS : 포트 넘버 env 파일에 정의 후 불러와서 서버 실행하도록 수정 필요 (25-03-11)
// 서버 실행 (포트 8080으로 설정)
if err := engine.Run(":8080"); err != nil {
log.Fatalf("서버 실행 실패: %v", err)
}

log.Println("서버 시작됨")
}
38 changes: 32 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
module github.com/jslee/JiRigo

go 1.22
go 1.23

toolchain go1.23.7

require (
github.com/gin-gonic/gin v1.10.0
github.com/google/uuid v1.6.0
github.com/joho/godotenv v1.5.1
github.com/lib/pq v1.10.9
gocloud.dev v0.40.0
gorm.io/driver/postgres v1.5.2
gorm.io/gorm v1.25.2
)

require (
contrib.go.opencensus.io/integrations/ocsql v0.1.7 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
go.opencensus.io v0.24.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
159 changes: 72 additions & 87 deletions go.sum

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions pkg/infra/response/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package response

import (
"github.com/gin-gonic/gin"
)

// Response 구조체 정의
type Response struct {
Success bool `json:"success"`
Message string `json:"message,omitempty"`
Data interface{} `json:"data,omitempty"`
Error string `json:"error,omitempty"`
}

// Success 성공 응답을 위한 헬퍼 함수
func Success(c *gin.Context, statusCode int, message string, data interface{}) Response {
c.JSON(statusCode, Response{
Success: true,
Message: message,
Data: data,
})
return Response{
Success: true,
Message: message,
Data: data,
}
}

// Error 에러 응답을 위한 헬퍼 함수
func Error(c *gin.Context, statusCode int, message string, err error) Response {
c.JSON(statusCode, Response{
Success: false,
Message: message,
Error: err.Error(),
})
return Response{
Success: false,
Message: message,
Error: err.Error(),
}
}
65 changes: 65 additions & 0 deletions pkg/infra/routing/gin_router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package routing

import (
"net/http"

"github.com/gin-gonic/gin"
)

type GinRouteRegister struct {
engine *gin.Engine
group *gin.RouterGroup
basePath string
}

func NewGinRouteRegister(engine *gin.Engine) *GinRouteRegister {
return &GinRouteRegister{
engine: engine,
group: &engine.RouterGroup,
}
}

func (r *GinRouteRegister) Get(path string, handler http.HandlerFunc) {
r.group.GET(path, wrapHandler(handler))
}

func (r *GinRouteRegister) Post(path string, handler http.HandlerFunc) {
r.group.POST(path, wrapHandler(handler))
}

func (r *GinRouteRegister) Put(path string, handler http.HandlerFunc) {
r.group.PUT(path, wrapHandler(handler))
}

func (r *GinRouteRegister) Delete(path string, handler http.HandlerFunc) {
r.group.DELETE(path, wrapHandler(handler))
}

// 지정된 경로 접두사를 가진 새 라우트 그룹을 생성
func (r *GinRouteRegister) Group(path string, fn func(RouteRegister)) {
group := r.group.Group(path)
subRouter := &GinRouteRegister{
engine: r.engine,
group: group,
basePath: r.basePath + path,
}
fn(subRouter)
}

// 라우터에 미들웨어를 추가
func (r *GinRouteRegister) UseMiddleware(middleware ...func(http.Handler) http.Handler) {
for _, m := range middleware {
r.group.Use(func(c *gin.Context) {
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.Next()
})).ServeHTTP(c.Writer, c.Request)
})
}
}

// http.HandlerFunc를 gin.HandlerFunc으로 변환
func wrapHandler(h http.HandlerFunc) gin.HandlerFunc {
return func(c *gin.Context) {
h(c.Writer, c.Request)
}
}
13 changes: 13 additions & 0 deletions pkg/infra/routing/routing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package routing

import "net/http"

// HTTP 라우트를 등록하기 위한 인터페이스
type RouteRegister interface {
Get(path string, handler http.HandlerFunc)
Post(path string, handler http.HandlerFunc)
Put(path string, handler http.HandlerFunc)
Delete(path string, handler http.HandlerFunc)
Group(path string, fn func(RouteRegister))
UseMiddleware(middleware ...func(http.Handler) http.Handler)
}
Empty file removed pkg/services/signin/.gitkeep
Empty file.
14 changes: 13 additions & 1 deletion pkg/services/signin/signin.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
package signin

type Service interface{}
import (
"context"

"github.com/jslee/JiRigo/pkg/services/signin/signinmodel"
)

type Service interface {
GetUsers(ctx context.Context) ([]signinmodel.Users, error)
GetUserByUID(ctx context.Context, userUID string) (*signinmodel.Users, error)
CreateUser(ctx context.Context, cmd signinmodel.CreateUserCmd) error
UpdateUser(ctx context.Context, userUID string, cmd signinmodel.UpdateUserCmd) error
DeleteUser(ctx context.Context, userUID string) error
}
38 changes: 38 additions & 0 deletions pkg/services/signin/signinapi/api.go
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
package signinapi

import (
"github.com/jslee/JiRigo/pkg/infra/routing"
"github.com/jslee/JiRigo/pkg/services/signin"
)

type SigninAPI struct {
signinService signin.Service
}

func ProvideSigninAPI(
routeRegister routing.RouteRegister,
signinService signin.Service,
) *SigninAPI {
sapi := &SigninAPI{
signinService: signinService,
}

sapi.registerRoutes(routeRegister)
return sapi
}

// 라우트 등록 메서드 수정
func (sapi *SigninAPI) registerRoutes(router routing.RouteRegister) {
router.Group("/user", func(signinRoute routing.RouteRegister) {
// 단일 사용자 조회
signinRoute.Post("/detail", sapi.getUserByUID)

// 사용자 생성
signinRoute.Post("/", sapi.CreateUser)

// 사용자 정보 수정
// signinRoute.Patch("/edit", sapi.UpdateUser)

// 사용자 삭제
signinRoute.Post("/delete", sapi.DeleteUser)
})
}
97 changes: 97 additions & 0 deletions pkg/services/signin/signinapi/signin.go
Original file line number Diff line number Diff line change
@@ -1 +1,98 @@
package signinapi

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/jslee/JiRigo/pkg/infra/response"
"github.com/jslee/JiRigo/pkg/services/signin/signinmodel"
)

// GET: /user/detail
// 사용자 정보 상세 조회
func (sapi *SigninAPI) getUserByUID(w http.ResponseWriter, r *http.Request) {
// Gin Context 생성
c, _ := gin.CreateTestContext(w)
c.Request = r

var query signinmodel.GetUserByUIDQuery
if err := c.ShouldBindJSON(&query); err != nil {
response.Error(c, http.StatusBadRequest, "잘못된 요청 데이터", err)
return
}

// 사용자 목록 조회 서비스 호출
users, err := sapi.signinService.GetUserByUID(c, query.UID)
if err != nil {
response.Error(c, http.StatusInternalServerError, "사용자 조회 실패", err)
return
}

response.Success(c, http.StatusOK, "사용자 조회 성공", users)
}

// POST: /user/sign-up
// 사용자 생성
func (sapi *SigninAPI) CreateUser(w http.ResponseWriter, r *http.Request) {
// Gin Context 생성
c, _ := gin.CreateTestContext(w)
c.Request = r

var cmd signinmodel.CreateUserCmd
if err := c.ShouldBindJSON(&cmd); err != nil {
response.Error(c, http.StatusBadRequest, "bad request data", err)
return
}

// 사용자 생성 서비스 호출
if err := sapi.signinService.CreateUser(c, cmd); err != nil {
response.Error(c, http.StatusInternalServerError, "사용자 생성 실패", err)
return
}

response.Success(c, http.StatusOK, "사용자 생성 성공", nil)
}

// PATCH: /user/edit
// 사용자 정보 수정
// func (sapi *SigninAPI) UpdateUser(w http.ResponseWriter, r *http.Request) {
// // Gin Context 생성
// c, _ := gin.CreateTestContext(w)
// c.Request = r

// var cmd signinmodel.UpdateUserCmd
// if err := c.ShouldBindJSON(&cmd); err != nil {
// response.Error(c, http.StatusBadRequest, "bad request data", err)
// return
// }

// // 사용자 정보 수정 서비스 호출
// if err := sapi.signinService.UpdateUser(c, cmd); err != nil {
// response.Error(c, http.StatusInternalServerError, "사용자 정보 수정 실패", err)
// return
// }

// response.Success(c, http.StatusOK, "사용자 정보 수정 성공", nil)
// }

// DELETE: /user/delete(USER)
// 사용자 삭제
func (sapi *SigninAPI) DeleteUser(w http.ResponseWriter, r *http.Request) {
// Gin Context 생성
c, _ := gin.CreateTestContext(w)
c.Request = r

var query signinmodel.GetUserByUIDQuery
if err := c.ShouldBindJSON(&query); err != nil {
response.Error(c, http.StatusBadRequest, "잘못된 요청 데이터", err)
return
}

// 사용자 삭제 서비스 호출
if err := sapi.signinService.DeleteUser(c, query.UID); err != nil {
response.Error(c, http.StatusInternalServerError, "사용자 삭제 실패", err)
return
}

response.Success(c, http.StatusOK, "사용자 삭제 성공", nil)
}
Loading