Skip to content

Commit

Permalink
feat: add SPYxFamily service
Browse files Browse the repository at this point in the history
  • Loading branch information
andy89923 committed Jul 5, 2024
1 parent 5eafcc5 commit 0a7bdad
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type NFContext struct {
UriScheme models.UriScheme
BindingIPv4 string
SBIPort int

SpyFamilyData map[string]string
}

var nfContext = NFContext{}
Expand All @@ -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 {
Expand Down
44 changes: 44 additions & 0 deletions internal/sbi/api_spyfamily.go
Original file line number Diff line number Diff line change
@@ -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)
}
20 changes: 20 additions & 0 deletions internal/sbi/processor/processor.go
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 16 additions & 0 deletions internal/sbi/processor/spy_family.go
Original file line number Diff line number Diff line change
@@ -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))
}
3 changes: 3 additions & 0 deletions internal/sbi/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions internal/sbi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ 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"
)

type nfApp interface {
app.App
Processor() *processor.Processor
}

type Server struct {
Expand Down
12 changes: 12 additions & 0 deletions pkg/service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -24,6 +25,7 @@ type NfApp struct {
wg sync.WaitGroup

sbiServer *sbi.Server
processor *processor.Processor
}

var _ app.App = &NfApp{}
Expand All @@ -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
}

Expand All @@ -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 {
Expand Down

0 comments on commit 0a7bdad

Please sign in to comment.