Initial commit
This commit is contained in:
27
backend/internal/routes/food.go
Normal file
27
backend/internal/routes/food.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sqlc_example/internal/services"
|
||||
"sqlc_example/models/dto"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func setupFoodRoutes(router *gin.Engine, foodService services.FoodService) {
|
||||
router.GET("/food", func(ctx *gin.Context) {
|
||||
var newItemFoodDTO dto.NewFoodItemDTO
|
||||
|
||||
if err := ctx.ShouldBindJSON(newItemFoodDTO); err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"err": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
guid, serviceErr := foodService.AddFoodItem(newItemFoodDTO); if serviceErr != nil {
|
||||
ctx.AbortWithStatusJSON(*&serviceErr.HttpCode, gin.H{"error": serviceErr.Error})
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{"guid": guid})
|
||||
})
|
||||
}
|
||||
49
backend/internal/routes/router.go
Normal file
49
backend/internal/routes/router.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
func NewGinRouter(lc fx.Lifecycle) *gin.Engine {
|
||||
|
||||
router := gin.Default()
|
||||
|
||||
server := &http.Server{
|
||||
Addr: fmt.Sprintf(":%s", "8080"),
|
||||
Handler: router,
|
||||
}
|
||||
|
||||
lc.Append(fx.Hook{
|
||||
OnStart: func(ctx context.Context) error {
|
||||
|
||||
go func() {
|
||||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
// TODO: log
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
},
|
||||
OnStop: func(ctx context.Context) error {
|
||||
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := server.Shutdown(shutdownCtx); err != nil {
|
||||
// TODO: log
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
})
|
||||
|
||||
return router
|
||||
}
|
||||
17
backend/internal/routes/setup.go
Normal file
17
backend/internal/routes/setup.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"sqlc_example/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
var Module = fx.Module("routes",
|
||||
fx.Invoke(func (
|
||||
router *gin.Engine,
|
||||
foodService services.FoodService,
|
||||
) {
|
||||
setupFoodRoutes(router, foodService)
|
||||
}),
|
||||
)
|
||||
Reference in New Issue
Block a user