Skip to content
12 changes: 3 additions & 9 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/techbloghub/server/config"
_ "github.com/techbloghub/server/ent/runtime"
"github.com/techbloghub/server/internal/database"
"github.com/techbloghub/server/internal/http/router"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[질문]
go/gin에서는 기본적으로 깃허브 레포 통해서 패키지를 관리하나요?! 아니면 따로 설정을 해둔건가요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

server/go.mod

Lines 1 to 4 in 77db124

module github.com/techbloghub/server
go 1.23.4

요기 보면 처음에 프로젝트 초기화할때 패키지 모듈명을 이런식으로 원격저장소 주소로 해서 초기화를 보통 합니당.

공식문서에 적혀있는 내용

In actual development, the module path will typically be the repository location where your source code will be kept. For example, the module path might be github.com/mymodule. If you plan to publish your module for others to use, the module path must be a location from which Go tools can download your module. For more about naming a module with a module path, see Managing dependencies.

링크

요거 물어본거가 맞는거겠죠..?ㅎㅎㅎ

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 맞습니다 ㅎㅎㅎㅎ


"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
Expand All @@ -26,18 +27,11 @@ func main() {
defer client.Close()

// 서버 실행
r := setRouter()
r := gin.Default()
router.InitRouter(r)
routerErr := r.Run(":" + cfg.ServerConfig.Port)
if routerErr != nil {
fmt.Println("Error while running server: ", routerErr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return
}
}

func setRouter() *gin.Engine {
r := gin.Default()
r.GET("/ping", func(context *gin.Context) {
context.String(200, "pong")
})
return r
}
10 changes: 6 additions & 4 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/techbloghub/server/internal/http/router"
)

func TestPingEndpoint(t *testing.T) {
// Set Gin to Test Mode
gin.SetMode(gin.TestMode)

// Create a new router instance using the private setRouter function
router := setRouter()
// Create a new r instance using the private setRouter function
r := gin.Default()
router.InitRouter(r)

// Create a test HTTP recorder
w := httptest.NewRecorder()
Expand All @@ -24,11 +26,11 @@ func TestPingEndpoint(t *testing.T) {
assert.NoError(t, err)

// Serve the request using the router
router.ServeHTTP(w, req)
r.ServeHTTP(w, req)

// Assert the response code is 200
assert.Equal(t, http.StatusOK, w.Code)

// Assert the response body is "pong"
assert.Equal(t, "pong", w.Body.String())
assert.Equal(t, "pong\n", w.Body.String())
}
7 changes: 7 additions & 0 deletions internal/http/handler/pinpong.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package handler

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

func PingPong(context *gin.Context) {
context.String(200, "pong\n")
}
10 changes: 10 additions & 0 deletions internal/http/router/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package router

import (
"github.com/gin-gonic/gin"
"github.com/techbloghub/server/internal/http/handler"
)

func InitRouter(r *gin.Engine) {
r.GET("/ping", handler.PingPong)
}