Skip to content

Commit

Permalink
refactor N3IWF app
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-ywliu committed Jul 16, 2024
1 parent 85af4aa commit eb3660b
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 192 deletions.
71 changes: 67 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package main

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

"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/urfave/cli"

"github.com/free5gc/n3iwf/internal/logger"
Expand Down Expand Up @@ -37,33 +42,91 @@ func main() {
Name: "log, l",
Usage: "Output NF log to `FILE`",
},
cli.BoolFlag{
Name: "nolog, nl",
Usage: "Disable log to stdout/stderr",
},
cli.StringFlag{
Name: "loglevel, ll",
Usage: "Override logger level",
},
cli.BoolFlag{
Name: "reportcaller, rc",
Usage: "Enable logger report caller",
},
cli.BoolFlag{
Name: "debug, deb",
Usage: "Enable pprof debug",
},
}
if err := app.Run(os.Args); err != nil {
logger.MainLog.Errorf("N3IWF Run Error: %v\n", err)
}
}

func runPProfServer() {
r := gin.Default()
pprof.Register(r)
// Listen and Server in 0.0.0.0:6061
err := r.Run(":6061")
if err != nil {
logger.MainLog.Errorf("runPProfServer(): %v", err)
}
}

func action(cliCtx *cli.Context) error {
tlsKeyLogPath, err := initLogFile(cliCtx.StringSlice("log"))
debug := cliCtx.Bool("debug")
if debug {
go runPProfServer()
}
logPathSlice := cliCtx.StringSlice("log")
cfgPath := cliCtx.String("config")
noLog := cliCtx.Bool("nolog")
logLevel := cliCtx.String("loglevel")
reportCaller := cliCtx.Bool("reportcaller")

tlsKeyLogPath, err := initLogFile(logPathSlice)
if err != nil {
return err
}

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

cfg, err := factory.ReadConfig(cliCtx.String("config"))
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(cfgPath)
if err != nil {
close(sigCh)
return err
}
factory.N3iwfConfig = cfg

n3iwf, err := service.NewApp(cfg)
// Replace logger config with cli parameters
if noLog {
cfg.SetLogEnable(false)
}
if logLevel != "" {
cfg.SetLogLevel(logLevel)
}
if reportCaller {
cfg.SetLogReportCaller(true)
}

n3iwf, err := service.NewApp(ctx, cfg, tlsKeyLogPath)
if err != nil {
close(sigCh)
return err
}
N3IWF = n3iwf

n3iwf.Start(tlsKeyLogPath)
n3iwf.Start()

return nil
}
Expand Down
51 changes: 27 additions & 24 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,53 @@ go 1.21

require (
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
github.com/free5gc/aper v1.0.5
github.com/free5gc/ngap v1.0.8
github.com/free5gc/sctp v1.0.0
github.com/free5gc/util v1.0.6
github.com/sirupsen/logrus v1.8.1
github.com/free5gc/aper v1.0.6-0.20240503143507-2c4c4780b98f
github.com/free5gc/ngap v1.0.9-0.20240708062829-734d184eed74
github.com/free5gc/sctp v1.0.1
github.com/free5gc/util v1.0.7-0.20240713162917-350ee8f4af4c
github.com/gin-contrib/pprof v1.5.0
github.com/gin-gonic/gin v1.10.0
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.3
github.com/urfave/cli v1.22.5
github.com/vishvananda/netlink v1.1.0
github.com/wmnsk/go-gtp v0.8.0
golang.org/x/net v0.23.0
golang.org/x/sys v0.18.0
golang.org/x/net v0.25.0
golang.org/x/sys v0.20.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/antonfisher/nested-logrus-formatter v1.3.1 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/free5gc/openapi v1.0.8 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/free5gc/openapi v1.0.9-0.20240503143645-eac9f06c2f6b // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-jwt/jwt v3.2.1+incompatible // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/tim-ywliu/nested-logrus-formatter v1.3.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit eb3660b

Please sign in to comment.