Skip to content

Commit

Permalink
refactor: sbi server
Browse files Browse the repository at this point in the history
  • Loading branch information
TYuan0816 committed May 9, 2024
1 parent 252a187 commit 25daec5
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 80 deletions.
19 changes: 17 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
package main

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

"github.com/urfave/cli"

Expand Down Expand Up @@ -60,19 +63,31 @@ func action(cliCtx *cli.Context) error {

logger.MainLog.Infoln("AUSF version: ", version.GetVersion())

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
}()

cfg, err := factory.ReadConfig(cliCtx.String("config"))
if err != nil {
sigCh <- nil
return err
}
factory.AusfConfig = cfg

ausf, err := service.NewApp(cfg)
ausf, err := service.NewApp(ctx, cfg, tlsKeyLogPath)
if err != nil {
sigCh <- nil
return err
}
AUSF = ausf

ausf.Start(tlsKeyLogPath)
ausf.Start()
AUSF.WaitRoutineStopped()

return nil
}
Expand Down
2 changes: 2 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var (
InitLog *logrus.Entry
CfgLog *logrus.Entry
CtxLog *logrus.Entry
SBILog *logrus.Entry
GinLog *logrus.Entry
ConsumerLog *logrus.Entry
UeAuthLog *logrus.Entry
Expand All @@ -33,6 +34,7 @@ func init() {
InitLog = NfLog.WithField(logger_util.FieldCategory, "Init")
CfgLog = NfLog.WithField(logger_util.FieldCategory, "CFG")
CtxLog = NfLog.WithField(logger_util.FieldCategory, "CTX")
SBILog = NfLog.WithField(logger_util.FieldCategory, "SBI")
GinLog = NfLog.WithField(logger_util.FieldCategory, "GIN")
ConsumerLog = NfLog.WithField(logger_util.FieldCategory, "Consumer")
UeAuthLog = NfLog.WithField(logger_util.FieldCategory, "UeAuth")
Expand Down
123 changes: 123 additions & 0 deletions internal/sbi/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package sbi

import (
"context"
"fmt"
"log"
"net/http"
"runtime/debug"
"sync"
"time"

"github.com/free5gc/ausf/pkg/app"
"github.com/free5gc/ausf/pkg/factory"

ausf_context "github.com/free5gc/ausf/internal/context"
"github.com/free5gc/ausf/internal/logger"
"github.com/free5gc/ausf/internal/sbi/consumer"
"github.com/free5gc/ausf/internal/sbi/processor"

Check failure on line 18 in internal/sbi/server.go

View workflow job for this annotation

GitHub Actions / build (1.21)

no required module provides package github.com/free5gc/ausf/internal/sbi/processor; to add it:
"github.com/free5gc/ausf/internal/util"
"github.com/free5gc/openapi/models"
"github.com/free5gc/util/httpwrapper"
logger_util "github.com/free5gc/util/logger"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

type ServerAusf interface {
app.App

Consumer() *consumer.Consumer

Check failure on line 30 in internal/sbi/server.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

undefined: consumer.Consumer (typecheck)
Processor() *processor.Processor
}

type Server struct {
ServerAusf

httpServer *http.Server
router *gin.Engine
}

func NewServer(ausf ServerAusf, tlsKeyLogPath string) (*Server, error) {
s := &Server{
ServerAusf: ausf,
router: logger_util.NewGinWithLogrus(logger.GinLog),
}

routes := s.getUeAuthenticationRoutes()

Check failure on line 47 in internal/sbi/server.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

s.getUeAuthenticationRoutes undefined (type *Server has no field or method getUeAuthenticationRoutes) (typecheck)
group := s.router.Group(factory.AusfAuthResUriPrefix)
routerAuthorizationCheck := util.NewRouterAuthorizationCheck(models.ServiceName_NAUSF_AUTH)
group.Use(func(c *gin.Context) {
routerAuthorizationCheck.Check(c, ausf_context.GetSelf())
})
applyRoutes(group, routes)

Check failure on line 53 in internal/sbi/server.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

undefined: applyRoutes (typecheck)

cfg := s.Config()
bindAddr := cfg.GetSbiBindingAddr()
logger.SBILog.Infof("Binding addr: [%s]", bindAddr)
var err error
if s.httpServer, err = httpwrapper.NewHttp2Server(bindAddr, tlsKeyLogPath, s.router); err != nil {
logger.InitLog.Errorf("Initialize HTTP server failed: %v", err)
return nil, err
}
s.httpServer.ErrorLog = log.New(logger.SBILog.WriterLevel(logrus.ErrorLevel), "HTTP2: ", 0)

return s, nil
}

func (s *Server) Run(traceCtx context.Context, wg *sync.WaitGroup) error {
var err error
_, s.Context().NfId, err = s.Consumer().RegisterNFInstance(context.Background())
if err != nil {
logger.InitLog.Errorf("AUSF register to NRF Error[%s]", err.Error())
}

wg.Add(1)
go s.startServer(wg)

return nil
}

func (s *Server) Stop() {
const defaultShutdownTimeout time.Duration = 2 * time.Second

if s.httpServer != nil {
logger.SBILog.Infof("Stop SBI server (listen on %s)", s.httpServer.Addr)
toCtx, cancel := context.WithTimeout(context.Background(), defaultShutdownTimeout)
defer cancel()
if err := s.httpServer.Shutdown(toCtx); err != nil {
logger.SBILog.Errorf("Could not close SBI server: %#v", err)
}
}
}

func (s *Server) startServer(wg *sync.WaitGroup) {
defer func() {
if p := recover(); p != nil {
// Print stack for panic to log. Fatalf() will let program exit.
logger.SBILog.Fatalf("panic: %v\n%s", p, string(debug.Stack()))
s.Terminate()
}
wg.Done()
}()

logger.SBILog.Infof("Start SBI server (listen on %s)", s.httpServer.Addr)

var err error
cfg := s.Config()
scheme := cfg.GetSbiScheme()
if scheme == "http" {
err = s.httpServer.ListenAndServe()
} else if scheme == "https" {
err = s.httpServer.ListenAndServeTLS(
cfg.GetCertPemPath(),
cfg.GetCertKeyPath())
} else {
err = fmt.Errorf("No support this scheme[%s]", scheme)
}

if err != nil && err != http.ErrServerClosed {
logger.SBILog.Errorf("SBI server error: %v", err)
}
logger.SBILog.Warnf("SBI server (listen on %s) stopped", s.httpServer.Addr)
}
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 (
ausf_context "github.com/free5gc/ausf/internal/context"
"github.com/free5gc/ausf/pkg/factory"
)

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

Start()
Terminate()

Context() *ausf_context.AUSFContext
Config() *factory.Config
}
54 changes: 54 additions & 0 deletions pkg/factory/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package factory
import (
"errors"
"fmt"
"os"
"strconv"
"sync"

Expand Down Expand Up @@ -235,3 +236,56 @@ func (c *Config) GetLogReportCaller() bool {
}
return c.Logger.ReportCaller
}

func (c *Config) GetSbiBindingAddr() string {
c.RLock()
defer c.RUnlock()
return c.GetSbiBindingIP() + ":" + strconv.Itoa(c.GetSbiPort())
}

func (c *Config) GetSbiBindingIP() string {
c.RLock()
defer c.RUnlock()
bindIP := "0.0.0.0"
if c.Configuration == nil || c.Configuration.Sbi == nil {
return bindIP
}
if c.Configuration.Sbi.BindingIPv4 != "" {
if bindIP = os.Getenv(c.Configuration.Sbi.BindingIPv4); bindIP != "" {
logger.CfgLog.Infof("Parsing ServerIPv4 [%s] from ENV Variable", bindIP)
} else {
bindIP = c.Configuration.Sbi.BindingIPv4
}
}
return bindIP
}

func (c *Config) GetSbiPort() int {
c.RLock()
defer c.RUnlock()
if c.Configuration != nil && c.Configuration.Sbi != nil && c.Configuration.Sbi.Port != 0 {
return c.Configuration.Sbi.Port
}
return AusfSbiDefaultPort
}

func (c *Config) GetSbiScheme() string {
c.RLock()
defer c.RUnlock()
if c.Configuration != nil && c.Configuration.Sbi != nil && c.Configuration.Sbi.Scheme != "" {
return c.Configuration.Sbi.Scheme
}
return AusfSbiDefaultScheme
}

func (c *Config) GetCertPemPath() string {
c.RLock()
defer c.RUnlock()
return c.Configuration.Sbi.Tls.Pem
}

func (c *Config) GetCertKeyPath() string {
c.RLock()
defer c.RUnlock()
return c.Configuration.Sbi.Tls.Key
}
Loading

0 comments on commit 25daec5

Please sign in to comment.