From 50d344e5ca3d2027c84c9c69a3d2c844f3a3638d Mon Sep 17 00:00:00 2001 From: newb1er Date: Thu, 25 Apr 2024 18:17:44 +0800 Subject: [PATCH] refactor: interface design --- .../sbi/{server => }/api_nssaiavailability.go | 2 +- internal/sbi/{server => }/api_nsselection.go | 2 +- internal/sbi/nssf.go | 13 ---------- internal/sbi/processor/processor.go | 1 - internal/sbi/{server => }/router.go | 2 +- internal/sbi/{server => }/server.go | 26 ++++++++++++++----- pkg/service/init.go | 21 ++++++--------- 7 files changed, 31 insertions(+), 36 deletions(-) rename internal/sbi/{server => }/api_nssaiavailability.go (98%) rename internal/sbi/{server => }/api_nsselection.go (96%) delete mode 100644 internal/sbi/nssf.go rename internal/sbi/{server => }/router.go (98%) rename internal/sbi/{server => }/server.go (82%) diff --git a/internal/sbi/server/api_nssaiavailability.go b/internal/sbi/api_nssaiavailability.go similarity index 98% rename from internal/sbi/server/api_nssaiavailability.go rename to internal/sbi/api_nssaiavailability.go index dea7043..8f817b3 100644 --- a/internal/sbi/server/api_nssaiavailability.go +++ b/internal/sbi/api_nssaiavailability.go @@ -1,4 +1,4 @@ -package server +package sbi import ( "net/http" diff --git a/internal/sbi/server/api_nsselection.go b/internal/sbi/api_nsselection.go similarity index 96% rename from internal/sbi/server/api_nsselection.go rename to internal/sbi/api_nsselection.go index 638f63e..210247e 100644 --- a/internal/sbi/server/api_nsselection.go +++ b/internal/sbi/api_nsselection.go @@ -1,4 +1,4 @@ -package server +package sbi import ( "net/http" diff --git a/internal/sbi/nssf.go b/internal/sbi/nssf.go deleted file mode 100644 index dafe39e..0000000 --- a/internal/sbi/nssf.go +++ /dev/null @@ -1,13 +0,0 @@ -package sbi - -import ( - nssf_context "github.com/free5gc/nssf/internal/context" - "github.com/free5gc/nssf/internal/sbi/processor" - "github.com/free5gc/nssf/pkg/factory" -) - -type Nssf interface { - Config() *factory.Config - Context() *nssf_context.NSSFContext - Processor() *processor.Processor -} diff --git a/internal/sbi/processor/processor.go b/internal/sbi/processor/processor.go index f5fd4ef..d7f336f 100644 --- a/internal/sbi/processor/processor.go +++ b/internal/sbi/processor/processor.go @@ -9,7 +9,6 @@ import ( type Nssf interface { Config() *factory.Config Context() *nssf_context.NSSFContext - Processor() *Processor } type Processor struct { diff --git a/internal/sbi/server/router.go b/internal/sbi/router.go similarity index 98% rename from internal/sbi/server/router.go rename to internal/sbi/router.go index b00efab..0e6288c 100644 --- a/internal/sbi/server/router.go +++ b/internal/sbi/router.go @@ -1,4 +1,4 @@ -package server +package sbi import ( "github.com/gin-gonic/gin" diff --git a/internal/sbi/server/server.go b/internal/sbi/server.go similarity index 82% rename from internal/sbi/server/server.go rename to internal/sbi/server.go index 0085541..76b88b2 100644 --- a/internal/sbi/server/server.go +++ b/internal/sbi/server.go @@ -1,4 +1,4 @@ -package server +package sbi import ( "context" @@ -9,22 +9,32 @@ import ( "github.com/gin-gonic/gin" + nssf_context "github.com/free5gc/nssf/internal/context" "github.com/free5gc/nssf/internal/logger" - "github.com/free5gc/nssf/internal/sbi" + "github.com/free5gc/nssf/internal/sbi/processor" "github.com/free5gc/nssf/pkg/factory" "github.com/free5gc/util/httpwrapper" logger_util "github.com/free5gc/util/logger" ) +type Nssf interface { + Config() *factory.Config + Context() *nssf_context.NSSFContext +} + type Server struct { - sbi.Nssf + Nssf httpServer *http.Server router *gin.Engine + processor *processor.Processor } -func NewServer(nssf sbi.Nssf, tlsKeyLogPath string) *Server { - s := &Server{Nssf: nssf} +func NewServer(nssf Nssf, tlsKeyLogPath string) *Server { + s := &Server{ + Nssf: nssf, + processor: processor.NewProcessor(nssf), + } s.router = newRouter(s) @@ -39,6 +49,10 @@ func NewServer(nssf sbi.Nssf, tlsKeyLogPath string) *Server { return s } +func (s *Server) Processor() *processor.Processor { + return s.processor +} + func (s *Server) Run(wg *sync.WaitGroup) { logger.SBILog.Info("Starting server...") @@ -73,7 +87,7 @@ func (s *Server) shutdownHttpServer() { } } -func bindRouter(nssf sbi.Nssf, router *gin.Engine, tlsKeyLogPath string) (*http.Server, error) { +func bindRouter(nssf Nssf, router *gin.Engine, tlsKeyLogPath string) (*http.Server, error) { sbiConfig := nssf.Config().Configuration.Sbi bindAddr := fmt.Sprintf("%s:%d", sbiConfig.BindingIPv4, sbiConfig.Port) diff --git a/pkg/service/init.go b/pkg/service/init.go index 125bad3..68f94b5 100644 --- a/pkg/service/init.go +++ b/pkg/service/init.go @@ -19,21 +19,23 @@ import ( "github.com/free5gc/nssf/internal/logger" "github.com/free5gc/nssf/internal/sbi" "github.com/free5gc/nssf/internal/sbi/consumer" - "github.com/free5gc/nssf/internal/sbi/processor" - "github.com/free5gc/nssf/internal/sbi/server" "github.com/free5gc/nssf/pkg/factory" ) +type App interface { + Config() *factory.Config + Context() *nssf_context.NSSFContext +} + type NssfApp struct { cfg *factory.Config nssfCtx *nssf_context.NSSFContext wg sync.WaitGroup - sbiServer *server.Server - processor *processor.Processor + sbiServer *sbi.Server } -var _ sbi.Nssf = &NssfApp{} +var _ App = &NssfApp{} func NewApp(cfg *factory.Config, tlsKeyLogPath string) (*NssfApp, error) { nssf := &NssfApp{cfg: cfg, wg: sync.WaitGroup{}} @@ -41,10 +43,7 @@ func NewApp(cfg *factory.Config, tlsKeyLogPath string) (*NssfApp, error) { nssf.SetLogLevel(cfg.GetLogLevel()) nssf.SetReportCaller(cfg.GetLogReportCaller()) - processor := processor.NewProcessor(nssf) - nssf.processor = processor - - sbiServer := server.NewServer(nssf, tlsKeyLogPath) + sbiServer := sbi.NewServer(nssf, tlsKeyLogPath) nssf.sbiServer = sbiServer nssf_context.Init() @@ -60,10 +59,6 @@ func (a *NssfApp) Context() *nssf_context.NSSFContext { return a.nssfCtx } -func (a *NssfApp) Processor() *processor.Processor { - return a.processor -} - func (a *NssfApp) SetLogEnable(enable bool) { logger.MainLog.Infof("Log enable is set to [%v]", enable) if enable && logger.Log.Out == os.Stderr {