From dffb0141f37a910d5ceaa818481f944b1e268ba7 Mon Sep 17 00:00:00 2001 From: JohanPaladinez Date: Fri, 14 Nov 2025 04:04:33 +0000 Subject: [PATCH] feat: add lab6 API service with GET and POST --- internal/sbi/api_lab6.go | 44 ++++++++++++++++++++++++++++++++++++++++ internal/sbi/router.go | 16 +++++++++------ 2 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 internal/sbi/api_lab6.go diff --git a/internal/sbi/api_lab6.go b/internal/sbi/api_lab6.go new file mode 100644 index 0000000..7939a7e --- /dev/null +++ b/internal/sbi/api_lab6.go @@ -0,0 +1,44 @@ +package sbi + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +// Rutas del nuevo servicio /lab6 +func (s *Server) getLab6Route() []Route { + return []Route{ + { + Name: "Lab6 Info", + Method: http.MethodGet, + Pattern: "/", // -> /lab6/ + APIFunc: func(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "Lab 6 OK", + "author": "Paladinez", + }) + }, + }, + { + Name: "Lab6 Echo", + Method: http.MethodPost, + Pattern: "/echo", // -> /lab6/echo + APIFunc: func(c *gin.Context) { + var body map[string]interface{} + + if err := c.BindJSON(&body); err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "error": "invalid JSON body", + }) + return + } + + c.JSON(http.StatusOK, gin.H{ + "message": "Lab 6 echo", + "received": body, + }) + }, + }, + } +} diff --git a/internal/sbi/router.go b/internal/sbi/router.go index 6fa35e1..a8ecb50 100644 --- a/internal/sbi/router.go +++ b/internal/sbi/router.go @@ -37,15 +37,19 @@ func applyRoutes(group *gin.RouterGroup, routes []Route) { } func newRouter(s *Server) *gin.Engine { - router := logger_util.NewGinWithLogrus(logger.GinLog) + router := logger_util.NewGinWithLogrus(logger.GinLog) - defaultGroup := router.Group("/default") - applyRoutes(defaultGroup, s.getDefaultRoute()) + defaultGroup := router.Group("/default") + applyRoutes(defaultGroup, s.getDefaultRoute()) - spyFamilyGroup := router.Group("/spyfamily") - applyRoutes(spyFamilyGroup, s.getSpyFamilyRoute()) + spyFamilyGroup := router.Group("/spyfamily") + applyRoutes(spyFamilyGroup, s.getSpyFamilyRoute()) - return router + // Nuevo servicio para el Lab 6 + lab6Group := router.Group("/lab6") + applyRoutes(lab6Group, s.getLab6Route()) + + return router } func bindRouter(nf app.App, router *gin.Engine, tlsKeyLogPath string) (*http.Server, error) {