From 611dc33366fa2f588999013e19236831523b5aaa Mon Sep 17 00:00:00 2001 From: gabbyrz024 Date: Sun, 23 Nov 2025 14:52:57 +0000 Subject: [PATCH] feat: add new API service with GET and POST endpoints --- internal/newapi/handler.go | 29 +++++++++++++++++++++++++++++ internal/sbi/api_secondapi.go | 22 ++++++++++++++++++++++ internal/sbi/router.go | 5 +++++ internal/secondapi/handler.go | 31 +++++++++++++++++++++++++++++++ secondapi/handler.go | 31 +++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+) create mode 100644 internal/newapi/handler.go create mode 100644 internal/sbi/api_secondapi.go create mode 100644 internal/secondapi/handler.go create mode 100644 secondapi/handler.go diff --git a/internal/newapi/handler.go b/internal/newapi/handler.go new file mode 100644 index 0000000..24a770c --- /dev/null +++ b/internal/newapi/handler.go @@ -0,0 +1,29 @@ +package newapi + +import ( + "net/http" + "github.com/gin-gonic/gin" +) + +func GetMessage(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "Hola desde el nuevo GET!", + }) +} + +type CreateRequest struct { + Name string `json:"name"` +} + +func CreateItem(c *gin.Context) { + var req CreateRequest + if err := c.BindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid input"}) + return + } + + c.JSON(http.StatusCreated, gin.H{ + "status": "created", + "name": req.Name, + }) +} diff --git a/internal/sbi/api_secondapi.go b/internal/sbi/api_secondapi.go new file mode 100644 index 0000000..31710bd --- /dev/null +++ b/internal/sbi/api_secondapi.go @@ -0,0 +1,22 @@ +package sbi + +import ( + "github.com/gabbyrz024/nf-example/internal/secondapi" +) + +func (s *Server) getSecondAPIRoute() []Route { + return []Route{ + { + Name: "secondapi-status", + Method: "GET", + Pattern: "/status", + APIFunc: secondapi.GetStatus, + }, + { + Name: "secondapi-message", + Method: "POST", + Pattern: "/message", + APIFunc: secondapi.PostMessage, + }, + } +} diff --git a/internal/sbi/router.go b/internal/sbi/router.go index b62139f..e30f5f3 100644 --- a/internal/sbi/router.go +++ b/internal/sbi/router.go @@ -10,6 +10,8 @@ import ( "github.com/free5gc/util/httpwrapper" logger_util "github.com/free5gc/util/logger" + + "github.com/gabbyrz024/nf-example/internal/secondapi" ) type Route struct { @@ -45,6 +47,9 @@ func newRouter(s *Server) *gin.Engine { spyFamilyGroup := router.Group("/spyfamily") applyRoutes(spyFamilyGroup, s.getSpyFamilyRoute()) + secondAPIGroup := router.Group("/secondapi") + applyRoutes(secondAPIGroup, s.getSecondAPIRoute()) + return router } diff --git a/internal/secondapi/handler.go b/internal/secondapi/handler.go new file mode 100644 index 0000000..87c8f26 --- /dev/null +++ b/internal/secondapi/handler.go @@ -0,0 +1,31 @@ +package secondapi + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +func GetStatus(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "status": "second api running", + }) +} + +type MessageRequest struct { + Message string `json:"message"` +} + +func PostMessage(c *gin.Context) { + var req MessageRequest + if err := c.BindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "error": err.Error(), + }) + return + } + + c.JSON(http.StatusOK, gin.H{ + "received": req.Message, + }) +} diff --git a/secondapi/handler.go b/secondapi/handler.go new file mode 100644 index 0000000..87c8f26 --- /dev/null +++ b/secondapi/handler.go @@ -0,0 +1,31 @@ +package secondapi + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +func GetStatus(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "status": "second api running", + }) +} + +type MessageRequest struct { + Message string `json:"message"` +} + +func PostMessage(c *gin.Context) { + var req MessageRequest + if err := c.BindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "error": err.Error(), + }) + return + } + + c.JSON(http.StatusOK, gin.H{ + "received": req.Message, + }) +}