From de494ca3a302eb323a4ba45acfd4051c862652a7 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 4 May 2025 16:02:48 -0500 Subject: [PATCH] Added new API service with GET and POST methods --- config/nfcfg.yaml | 2 +- internal/sbi/api_myservice.go | 28 ++++++++++++++++++++++++++++ internal/sbi/processor/my_service.go | 9 +++++++++ internal/sbi/router.go | 23 +++++++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 internal/sbi/api_myservice.go create mode 100644 internal/sbi/processor/my_service.go diff --git a/config/nfcfg.yaml b/config/nfcfg.yaml index 45001e3..39e1236 100644 --- a/config/nfcfg.yaml +++ b/config/nfcfg.yaml @@ -6,7 +6,7 @@ configuration: nfName: NF # the name of this NF sbi: # Service-based interface information scheme: http # the protocol for sbi (http or https) - bindingIPv4: 127.0.0.163 # IP used to bind the service + bindingIPv4: 127.0.0.1 # IP used to bind the service port: 8000 # Port used to bind the service tls: # the local path of TLS key pem: cert/nf.pem # NF TLS Certificate diff --git a/internal/sbi/api_myservice.go b/internal/sbi/api_myservice.go new file mode 100644 index 0000000..095eff8 --- /dev/null +++ b/internal/sbi/api_myservice.go @@ -0,0 +1,28 @@ +package sbi + +import ( + "net/http" + "github.com/gin-gonic/gin" +) + +// GET /myservice/hello +func GetHello(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "Hello from MyService!", + }) +} + +// POST /myservice/data +func PostData(c *gin.Context) { + var jsonData map[string]interface{} + if err := c.BindJSON(&jsonData); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON"}) + return + } + + // You could add logic here (or call a processor function) + c.JSON(http.StatusOK, gin.H{ + "received": jsonData, + "status": "Data processed successfully", + }) +} diff --git a/internal/sbi/processor/my_service.go b/internal/sbi/processor/my_service.go new file mode 100644 index 0000000..f336a5e --- /dev/null +++ b/internal/sbi/processor/my_service.go @@ -0,0 +1,9 @@ +package processor + +import "fmt" + +func ProcessData(data map[string]interface{}) error { + fmt.Println("Processing data:", data) + // Do something with data here + return nil +} diff --git a/internal/sbi/router.go b/internal/sbi/router.go index 6fa35e1..8471bc9 100644 --- a/internal/sbi/router.go +++ b/internal/sbi/router.go @@ -39,12 +39,17 @@ func applyRoutes(group *gin.RouterGroup, routes []Route) { func newRouter(s *Server) *gin.Engine { router := logger_util.NewGinWithLogrus(logger.GinLog) + // Existing route groups defaultGroup := router.Group("/default") applyRoutes(defaultGroup, s.getDefaultRoute()) spyFamilyGroup := router.Group("/spyfamily") applyRoutes(spyFamilyGroup, s.getSpyFamilyRoute()) + // New route group: /myservice + myServiceGroup := router.Group("/myservice") + applyRoutes(myServiceGroup, s.getMyServiceRoute()) + return router } @@ -53,3 +58,21 @@ func bindRouter(nf app.App, router *gin.Engine, tlsKeyLogPath string) (*http.Ser bindAddr := fmt.Sprintf("%s:%d", sbiConfig.BindingIPv4, sbiConfig.Port) return httpwrapper.NewHttp2Server(bindAddr, tlsKeyLogPath, router) } + +// NEW: Routes for /myservice +func (s *Server) getMyServiceRoute() []Route { + return []Route{ + { + Name: "GetHello", + Method: "GET", + Pattern: "/hello", + APIFunc: GetHello, + }, + { + Name: "PostData", + Method: "POST", + Pattern: "/data", + APIFunc: PostData, + }, + } +}