Skip to content

Commit

Permalink
refactor: general app interface
Browse files Browse the repository at this point in the history
  • Loading branch information
yccodr committed May 8, 2024
1 parent 25fe2c1 commit db71e05
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 45 deletions.
14 changes: 1 addition & 13 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package main

import (
"context"
"os"
"os/signal"
"path/filepath"
"runtime/debug"
"syscall"

"github.com/urfave/cli"

Expand Down Expand Up @@ -65,16 +62,7 @@ func action(cliCtx *cli.Context) error {
}
NSSF = nssf

ctx, cancel := context.WithCancel(context.Background())
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)

go func() {
<-sigCh // Wait for interrupt signal to gracefully shutdown
cancel() // Notify each goroutine and wait them stopped
}()

nssf.Start(ctx)
nssf.Start()

Check failure on line 65 in cmd/main.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

Error return value of `nssf.Start` is not checked (errcheck)
nssf.Wait()

return nil
Expand Down
14 changes: 4 additions & 10 deletions internal/sbi/processor/processor.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
package processor

import (
nssf_context "github.com/free5gc/nssf/internal/context"
"github.com/free5gc/nssf/pkg/factory"
"github.com/free5gc/nssf/pkg/app"
)

type Nssf interface {
Config() *factory.Config
Context() *nssf_context.NSSFContext
}

type Processor struct {
Nssf
app.NssfApp
}

func NewProcessor(nssf Nssf) *Processor {
func NewProcessor(nssf app.NssfApp) *Processor {
return &Processor{
Nssf: nssf,
NssfApp: nssf,
}
}
19 changes: 7 additions & 12 deletions internal/sbi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,27 @@ 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/processor"
"github.com/free5gc/nssf/internal/util"
"github.com/free5gc/nssf/pkg/app"
"github.com/free5gc/nssf/pkg/factory"
"github.com/free5gc/openapi/models"
"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 {
Nssf
app.NssfApp

httpServer *http.Server
router *gin.Engine
processor *processor.Processor
}

func NewServer(nssf Nssf, tlsKeyLogPath string) *Server {
func NewServer(nssf app.NssfApp, tlsKeyLogPath string) *Server {
s := &Server{
Nssf: nssf,
NssfApp: nssf,
processor: processor.NewProcessor(nssf),
}

Expand Down Expand Up @@ -89,7 +84,7 @@ func (s *Server) shutdownHttpServer() {
}
}

func bindRouter(nssf Nssf, router *gin.Engine, tlsKeyLogPath string) (*http.Server, error) {
func bindRouter(nssf app.NssfApp, router *gin.Engine, tlsKeyLogPath string) (*http.Server, error) {
sbiConfig := nssf.Config().Configuration.Sbi
bindAddr := fmt.Sprintf("%s:%d", sbiConfig.BindingIPv4, sbiConfig.Port)

Expand Down Expand Up @@ -132,7 +127,7 @@ func (s *Server) unsecureServe() error {
}

func (s *Server) secureServe() error {
sbiConfig := s.Nssf.Config().Configuration.Sbi
sbiConfig := s.Config().Configuration.Sbi

pemPath := sbiConfig.Tls.Pem
if pemPath == "" {
Expand All @@ -148,7 +143,7 @@ func (s *Server) secureServe() error {
}

func (s *Server) serve() error {
sbiConfig := s.Nssf.Config().Configuration.Sbi
sbiConfig := s.Config().Configuration.Sbi

switch sbiConfig.Scheme {
case "http":
Expand Down
18 changes: 18 additions & 0 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package app

import (
nssf_context "github.com/free5gc/nssf/internal/context"
"github.com/free5gc/nssf/pkg/factory"
)

type NssfApp interface {
SetLogEnable(enable bool)
SetLogLevel(level string)
SetReportCaller(reportCaller bool)

Start() error
Terminate()

Context() *nssf_context.NSSFContext
Config() *factory.Config
}
29 changes: 19 additions & 10 deletions pkg/service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,21 @@ import (
"fmt"
"io"
"os"
"os/signal"
"runtime/debug"
"sync"
"syscall"

"github.com/sirupsen/logrus"

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/consumer"
"github.com/free5gc/nssf/pkg/app"
"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
Expand All @@ -34,7 +32,7 @@ type NssfApp struct {
sbiServer *sbi.Server
}

var _ App = &NssfApp{}
var _ app.NssfApp = &NssfApp{}

func NewApp(cfg *factory.Config, tlsKeyLogPath string) (*NssfApp, error) {
nssf := &NssfApp{cfg: cfg, wg: sync.WaitGroup{}}
Expand Down Expand Up @@ -127,24 +125,35 @@ func (a *NssfApp) deregisterFromNrf() {
}
}

func (a *NssfApp) Start(ctx context.Context) {
func (a *NssfApp) Start() error {
ctx, cancel := context.WithCancel(context.Background())
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)

go func() {
<-sigCh // Wait for interrupt signal to gracefully shutdown
cancel() // Notify each goroutine and wait them stopped
}()

err := a.registerToNrf()
if err != nil {
logger.MainLog.Errorf("Register to NRF failed: %+v", err)
return fmt.Errorf("register to NRF failed: %+v", err)
} else {
logger.MainLog.Infoln("Register to NRF successfully")
logger.MainLog.Infoln("register to NRF successfully")
}

// Graceful deregister when panic
defer func() {
if p := recover(); p != nil {
logger.InitLog.Errorf("panic: %v\n%s", p, string(debug.Stack()))
a.deregisterFromNrf()
logger.InitLog.Fatalf("panic: %v\n%s", p, string(debug.Stack()))
}
}()

a.sbiServer.Run(&a.wg)
go a.listenShutdown(ctx)

return nil
}

func (a *NssfApp) listenShutdown(ctx context.Context) {
Expand Down

0 comments on commit db71e05

Please sign in to comment.