From 3a2069e19d97d805db5a15cb4b6195c7528c0d54 Mon Sep 17 00:00:00 2001 From: DanielEstebanRengifo Date: Sat, 9 May 2026 18:33:45 -0500 Subject: [PATCH] feat: add attack on titan api service --- internal/context/context.go | 31 ++++++++----- internal/sbi/api_attackontitan.go | 54 +++++++++++++++++++++++ internal/sbi/processor/attack_on_titan.go | 33 ++++++++++++++ internal/sbi/router.go | 6 ++- 4 files changed, 111 insertions(+), 13 deletions(-) create mode 100644 internal/sbi/api_attackontitan.go create mode 100644 internal/sbi/processor/attack_on_titan.go diff --git a/internal/context/context.go b/internal/context/context.go index 2bfe8b2..0c4f3c3 100644 --- a/internal/context/context.go +++ b/internal/context/context.go @@ -5,29 +5,26 @@ import ( "github.com/Alonza0314/nf-example/internal/logger" "github.com/Alonza0314/nf-example/pkg/factory" - "github.com/google/uuid" - "github.com/free5gc/openapi/models" + "github.com/google/uuid" ) type NFContext struct { - NfId string - Name string - UriScheme models.UriScheme - BindingIPv4 string - SBIPort int - - SpyFamilyData map[string]string + NfId string + Name string + UriScheme models.UriScheme + BindingIPv4 string + SBIPort int + SpyFamilyData map[string]string + AttackOnTitanData map[string]string } var nfContext = NFContext{} func InitNfContext() { cfg := factory.NfConfig - nfContext.NfId = uuid.New().String() nfContext.Name = "ANYA" - nfContext.UriScheme = cfg.Configuration.Sbi.Scheme nfContext.SBIPort = cfg.Configuration.Sbi.Port nfContext.BindingIPv4 = os.Getenv(cfg.Configuration.Sbi.BindingIPv4) @@ -57,6 +54,18 @@ func InitNfContext() { "Henry": "Henderson", "Martha": "Marriott", } + nfContext.AttackOnTitanData = map[string]string{ + "Eren": "Yeager", + "Mikasa": "Ackerman", + "Armin": "Arlert", + "Levi": "Ackerman", + "Hange": "Zoe", + "Erwin": "Smith", + "Reiner": "Braun", + "Bertolt": "Hoover", + "Annie": "Leonhart", + "Zeke": "Yeager", + } } func GetSelf() *NFContext { diff --git a/internal/sbi/api_attackontitan.go b/internal/sbi/api_attackontitan.go new file mode 100644 index 0000000..9751aab --- /dev/null +++ b/internal/sbi/api_attackontitan.go @@ -0,0 +1,54 @@ +package sbi + +import ( + "net/http" + + "github.com/Alonza0314/nf-example/internal/logger" + "github.com/gin-gonic/gin" +) + +func (s *Server) getAttackOnTitanRoute() []Route { + return []Route{ + { + Name: "Hello Attack on Titan!", + Method: http.MethodGet, + Pattern: "/", + APIFunc: func(c *gin.Context) { + c.JSON(http.StatusOK, "Hello Attack on Titan!") + }, + // Use + // curl -X GET http://127.0.0.163:8000/attackontitan/ -w "\n" + }, + { + Name: "Attack on Titan Character", + Method: http.MethodGet, + Pattern: "/character/:Name", + APIFunc: s.HTTPSearchAttackOnTitanCharacter, + // Use + // curl -X GET http://127.0.0.163:8000/attackontitan/character/Eren -w "\n" + }, + { + Name: "Add Attack on Titan Character", + Method: http.MethodPost, + Pattern: "/character", + APIFunc: s.HTTPAddAttackOnTitanCharacter, + // Use + // curl -X POST http://127.0.0.163:8000/attackontitan/character -H "Content-Type: application/json" -d '{"name":"Zeke","lastName":"Yeager"}' -w "\n" + }, + } +} + +func (s *Server) HTTPSearchAttackOnTitanCharacter(c *gin.Context) { + logger.SBILog.Infof("In HTTPSearchAttackOnTitanCharacter") + targetName := c.Param("Name") + if targetName == "" { + c.String(http.StatusBadRequest, "No name provided") + return + } + s.Processor().FindAttackOnTitanCharacterName(c, targetName) +} + +func (s *Server) HTTPAddAttackOnTitanCharacter(c *gin.Context) { + logger.SBILog.Infof("In HTTPAddAttackOnTitanCharacter") + s.Processor().AddAttackOnTitanCharacter(c) +} \ No newline at end of file diff --git a/internal/sbi/processor/attack_on_titan.go b/internal/sbi/processor/attack_on_titan.go new file mode 100644 index 0000000..5bcc70f --- /dev/null +++ b/internal/sbi/processor/attack_on_titan.go @@ -0,0 +1,33 @@ +package processor + +import ( + "fmt" + "net/http" + + "github.com/gin-gonic/gin" +) + +func (p *Processor) FindAttackOnTitanCharacterName(c *gin.Context, targetName string) { + if lastName, ok := p.Context().AttackOnTitanData[targetName]; ok { + c.String(http.StatusOK, fmt.Sprintf("Character: %s %s", targetName, lastName)) + return + } + c.String(http.StatusNotFound, fmt.Sprintf("[%s] not found in Attack on Titan", targetName)) +} + +func (p *Processor) AddAttackOnTitanCharacter(c *gin.Context) { + var body struct { + Name string `json:"name"` + LastName string `json:"lastName"` + } + if err := c.ShouldBindJSON(&body); err != nil { + c.String(http.StatusBadRequest, "Invalid request body") + return + } + if body.Name == "" || body.LastName == "" { + c.String(http.StatusBadRequest, "Name and LastName are required") + return + } + p.Context().AttackOnTitanData[body.Name] = body.LastName + c.String(http.StatusOK, fmt.Sprintf("Character %s %s added successfully", body.Name, body.LastName)) +} \ No newline at end of file diff --git a/internal/sbi/router.go b/internal/sbi/router.go index b62139f..bb04047 100644 --- a/internal/sbi/router.go +++ b/internal/sbi/router.go @@ -6,10 +6,9 @@ import ( "github.com/Alonza0314/nf-example/internal/logger" "github.com/Alonza0314/nf-example/pkg/app" - "github.com/gin-gonic/gin" - "github.com/free5gc/util/httpwrapper" logger_util "github.com/free5gc/util/logger" + "github.com/gin-gonic/gin" ) type Route struct { @@ -45,6 +44,9 @@ func newRouter(s *Server) *gin.Engine { spyFamilyGroup := router.Group("/spyfamily") applyRoutes(spyFamilyGroup, s.getSpyFamilyRoute()) + attackOnTitanGroup := router.Group("/attackontitan") + applyRoutes(attackOnTitanGroup, s.getAttackOnTitanRoute()) + return router }