Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
54 changes: 54 additions & 0 deletions internal/sbi/api_attackontitan.go
Original file line number Diff line number Diff line change
@@ -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)
}
33 changes: 33 additions & 0 deletions internal/sbi/processor/attack_on_titan.go
Original file line number Diff line number Diff line change
@@ -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))
}
6 changes: 4 additions & 2 deletions internal/sbi/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down