diff --git a/cmd/webserver/routes/config.go b/cmd/webserver/routes/config.go index afb95dd..3f59921 100644 --- a/cmd/webserver/routes/config.go +++ b/cmd/webserver/routes/config.go @@ -8,11 +8,15 @@ import ( // Add routes to our web server func AddRoutes(httpServer *httpserver.HTTPServer, queries *sqlc.Queries) { - ctrl := user.NewHandlerController(queries) // TODO + userRouterGroup := httpServer.NewRouterGroup("/user") + ctrl := user.NewHandlerController( + userRouterGroup, + queries, + ) // User related functionality - httpServer.GET("/user/", ctrl.IndexHandler) - httpServer.GET("/user/:name", ctrl.GetHandler) - httpServer.POST("/user", ctrl.PostHandler) - httpServer.DELETE("/user", ctrl.DeleteHandler) + userRouterGroup.GET("/", ctrl.IndexHandler) + userRouterGroup.GET(":name", ctrl.GetHandler) + userRouterGroup.POST("", ctrl.PostHandler) + userRouterGroup.DELETE("", ctrl.DeleteHandler) } diff --git a/cmd/webserver/routes/user/controller.go b/cmd/webserver/routes/user/controller.go index 2a7f303..20849ef 100644 --- a/cmd/webserver/routes/user/controller.go +++ b/cmd/webserver/routes/user/controller.go @@ -4,14 +4,20 @@ import ( "context" "weezel/example-gin/pkg/generated/sqlc" + + "github.com/gin-gonic/gin" ) type HandlerController struct { - querier sqlc.Querier + querier sqlc.Querier + userRouterGroup *gin.RouterGroup } -func NewHandlerController(db sqlc.Querier) *HandlerController { - return &HandlerController{querier: db} +func NewHandlerController(userRouterGroup *gin.RouterGroup, db sqlc.Querier) *HandlerController { + return &HandlerController{ + userRouterGroup: userRouterGroup, + querier: db, + } } // MockHandlerController introduces method calls that can be implemented on test case basis diff --git a/pkg/httpserver/server.go b/pkg/httpserver/server.go index 16577fa..218b737 100644 --- a/pkg/httpserver/server.go +++ b/pkg/httpserver/server.go @@ -135,6 +135,7 @@ func WithCustomHealthCheckHandler(healthCheckHandler gin.HandlerFunc) Option { // Initializing the server in a goroutine so that it won't block func (h *HTTPServer) Start() { + l.Logger.Info().Msgf("Starting web server on %s", h.httpServer.Addr) go func() { if err := h.httpServer.ListenAndServe(); err != nil && errors.Is(err, http.ErrServerClosed) { @@ -157,6 +158,10 @@ func (h *HTTPServer) Shutdown(ctx context.Context) { } } +func (h *HTTPServer) NewRouterGroup(path string, handlers ...gin.HandlerFunc) *gin.RouterGroup { + return h.ginEngine.Group(path, handlers...) +} + func (h *HTTPServer) Handle(httpMethod, relativePath string, handlers ...gin.HandlerFunc) { h.ginEngine.Handle(httpMethod, relativePath, handlers...) }