-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
325 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
"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 | ||
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() | ||
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) | ||
|
||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.