From 068609111dc98380acc975b8d928ac138df9dbf5 Mon Sep 17 00:00:00 2001 From: Luisa Date: Fri, 14 Nov 2025 11:58:49 -0500 Subject: [PATCH] feat(api): add MyAPI service with GET and POST endpoints --- internal/sbi/api_myapi.go | 42 +++++++++++++++++++++++++++++++++++++++ internal/sbi/router.go | 3 +++ 2 files changed, 45 insertions(+) create mode 100644 internal/sbi/api_myapi.go diff --git a/internal/sbi/api_myapi.go b/internal/sbi/api_myapi.go new file mode 100644 index 0000000..7912203 --- /dev/null +++ b/internal/sbi/api_myapi.go @@ -0,0 +1,42 @@ +package sbi + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +// getMyAPIRoute define las rutas de nuestro nuevo servicio "myapi". +// TendrĂ¡ al menos un GET y un POST, como pide el lab. +func (s *Server) getMyAPIRoute() []Route { + return []Route{ + { + Name: "MyAPI GET info", + Method: http.MethodGet, + Pattern: "/", + APIFunc: func(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "service": "myapi", + "status": "ok", + }) + }, + }, + { + Name: "MyAPI POST echo", + Method: http.MethodPost, + Pattern: "/echo", + APIFunc: func(c *gin.Context) { + var req struct { + Message string `json:"message" binding:"required"` + } + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"}) + return + } + c.JSON(http.StatusOK, gin.H{ + "echo": req.Message, + }) + }, + }, + } +} diff --git a/internal/sbi/router.go b/internal/sbi/router.go index 6fa35e1..1128b89 100644 --- a/internal/sbi/router.go +++ b/internal/sbi/router.go @@ -45,6 +45,9 @@ func newRouter(s *Server) *gin.Engine { spyFamilyGroup := router.Group("/spyfamily") applyRoutes(spyFamilyGroup, s.getSpyFamilyRoute()) + myapiGroup := router.Group("/myapi") + applyRoutes(myapiGroup, s.getMyAPIRoute()) + return router }