From 0a7bdadf0f464f97c06e5a7f8c35ec64a091e65b Mon Sep 17 00:00:00 2001 From: "CTFang@WireLab" Date: Fri, 5 Jul 2024 03:40:56 +0000 Subject: [PATCH] feat: add SPYxFamily service --- internal/context/context.go | 8 +++++ internal/sbi/api_spyfamily.go | 44 ++++++++++++++++++++++++++++ internal/sbi/processor/processor.go | 20 +++++++++++++ internal/sbi/processor/spy_family.go | 16 ++++++++++ internal/sbi/router.go | 3 ++ internal/sbi/server.go | 2 ++ pkg/service/init.go | 12 ++++++++ 7 files changed, 105 insertions(+) create mode 100644 internal/sbi/api_spyfamily.go create mode 100644 internal/sbi/processor/processor.go create mode 100644 internal/sbi/processor/spy_family.go diff --git a/internal/context/context.go b/internal/context/context.go index f5d2514..a8a2fb6 100644 --- a/internal/context/context.go +++ b/internal/context/context.go @@ -16,6 +16,8 @@ type NFContext struct { UriScheme models.UriScheme BindingIPv4 string SBIPort int + + SpyFamilyData map[string]string } var nfContext = NFContext{} @@ -38,6 +40,12 @@ func InitNfContext() { nfContext.BindingIPv4 = "0.0.0.0" } } + nfContext.SpyFamilyData = map[string]string{ + "Loid": "Forger", + "Anya": "Forger", + "Yor": "Forger", + "Bond": "Forger", + } } func GetSelf() *NFContext { diff --git a/internal/sbi/api_spyfamily.go b/internal/sbi/api_spyfamily.go new file mode 100644 index 0000000..46117e8 --- /dev/null +++ b/internal/sbi/api_spyfamily.go @@ -0,0 +1,44 @@ +package sbi + +import ( + "net/http" + + "github.com/andy89923/nf-example/internal/logger" + "github.com/gin-gonic/gin" +) + +func (s *Server) getSpyFamilyRoute() []Route { + return []Route{ + { + Name: "Hello SPYxFAMILY!", + Method: http.MethodGet, + Pattern: "/", + APIFunc: func(c *gin.Context) { + c.JSON(http.StatusOK, "Hello SPYxFAMILY!") + }, + // Use + // curl -X GET http://127.0.0.163:8000/spyfamily/ -w "\n" + }, + { + Name: "SPYxFAMILY Character", + Method: http.MethodGet, + Pattern: "/character/:Name", + APIFunc: s.HTTPSerchSpyFamilyCharacter, + // Use + // curl -X GET http://127.0.0.163:8000/spyfamily/Anya -w "\n" + // "Character: Anya Forger" + }, + } +} + +func (s *Server) HTTPSerchSpyFamilyCharacter(c *gin.Context) { + logger.SBILog.Infof("In HTTPSerchCharacter") + + targetName := c.Param("Name") + if targetName == "" { + c.JSON(http.StatusBadRequest, "No name provided") + return + } + + s.Processor().FindSpyFamilyCharacterName(c, targetName) +} diff --git a/internal/sbi/processor/processor.go b/internal/sbi/processor/processor.go new file mode 100644 index 0000000..c717df3 --- /dev/null +++ b/internal/sbi/processor/processor.go @@ -0,0 +1,20 @@ +package processor + +import "github.com/andy89923/nf-example/pkg/app" + +type ProcessorNf interface { + app.App + + Processor() *Processor +} + +type Processor struct { + ProcessorNf +} + +func NewProcessor(nf ProcessorNf) (*Processor, error) { + p := &Processor{ + ProcessorNf: nf, + } + return p, nil +} diff --git a/internal/sbi/processor/spy_family.go b/internal/sbi/processor/spy_family.go new file mode 100644 index 0000000..64d98d7 --- /dev/null +++ b/internal/sbi/processor/spy_family.go @@ -0,0 +1,16 @@ +package processor + +import ( + "fmt" + "net/http" + + "github.com/gin-gonic/gin" +) + +func (p *Processor) FindSpyFamilyCharacterName(c *gin.Context, targetName string) { + if lastName, ok := p.Context().SpyFamilyData[targetName]; ok { + c.JSON(http.StatusOK, fmt.Sprintf("Character: %s %s", targetName, lastName)) + return + } + c.JSON(http.StatusNotFound, fmt.Sprintf("[%s] not found in SPYxFAMILY", targetName)) +} diff --git a/internal/sbi/router.go b/internal/sbi/router.go index dc318c6..6fa35e1 100644 --- a/internal/sbi/router.go +++ b/internal/sbi/router.go @@ -42,6 +42,9 @@ func newRouter(s *Server) *gin.Engine { defaultGroup := router.Group("/default") applyRoutes(defaultGroup, s.getDefaultRoute()) + spyFamilyGroup := router.Group("/spyfamily") + applyRoutes(spyFamilyGroup, s.getSpyFamilyRoute()) + return router } diff --git a/internal/sbi/server.go b/internal/sbi/server.go index dc47a32..75e6444 100644 --- a/internal/sbi/server.go +++ b/internal/sbi/server.go @@ -8,6 +8,7 @@ import ( "time" "github.com/andy89923/nf-example/internal/logger" + "github.com/andy89923/nf-example/internal/sbi/processor" "github.com/andy89923/nf-example/pkg/app" "github.com/andy89923/nf-example/pkg/factory" "github.com/gin-gonic/gin" @@ -15,6 +16,7 @@ import ( type nfApp interface { app.App + Processor() *processor.Processor } type Server struct { diff --git a/pkg/service/init.go b/pkg/service/init.go index b067421..febf375 100644 --- a/pkg/service/init.go +++ b/pkg/service/init.go @@ -10,6 +10,7 @@ import ( nf_context "github.com/andy89923/nf-example/internal/context" "github.com/andy89923/nf-example/internal/logger" "github.com/andy89923/nf-example/internal/sbi" + "github.com/andy89923/nf-example/internal/sbi/processor" "github.com/andy89923/nf-example/pkg/app" "github.com/andy89923/nf-example/pkg/factory" "github.com/sirupsen/logrus" @@ -24,6 +25,7 @@ type NfApp struct { wg sync.WaitGroup sbiServer *sbi.Server + processor *processor.Processor } var _ app.App = &NfApp{} @@ -46,6 +48,12 @@ func NewApp(ctx context.Context, cfg *factory.Config, tlsKeyLogPath string) (*Nf sbiServer := sbi.NewServer(nf, tlsKeyLogPath) nf.sbiServer = sbiServer + processor, err := processor.NewProcessor(nf) + if err != nil { + return nf, err + } + nf.processor = processor + return nf, nil } @@ -57,6 +65,10 @@ func (a *NfApp) Context() *nf_context.NFContext { return a.nfCtx } +func (a *NfApp) Processor() *processor.Processor { + return a.processor +} + func (a *NfApp) SetLogEnable(enable bool) { logger.MainLog.Infof("Log enable is set to [%v]", enable) if enable && logger.Log.Out == os.Stderr {