Initial commit

This commit is contained in:
2025-10-27 14:22:19 +03:00
commit c6e7f13800
23 changed files with 1164 additions and 0 deletions

View 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})
})
}

View 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
}

View 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)
}),
)