Skip to content

Commit

Permalink
refactor packages, update go.mod and add config validator
Browse files Browse the repository at this point in the history
  • Loading branch information
free5gc-org committed Mar 23, 2022
1 parent 4a7f68e commit a6b839d
Show file tree
Hide file tree
Showing 15 changed files with 392 additions and 237 deletions.
18 changes: 14 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
# Swap files
*.swp

# Toolchain
# Goland project folder
# Golang project folder
.idea/

# Visual Studio Code
.vscode/

# Build
build/
log/
vendor/

# emacs/vim
GPATH
GRTAGS
GTAGS
TAGS
tags
cscope.*
# mac

# macOS
.DS_Store

# debug
# Debug
*.log
*.pcap

17 changes: 6 additions & 11 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ run:
# "/" will be replaced by current OS file path separator to properly work
# on Windows.
skip-files:
- "api_.*\\.go$"
- "model_.*\\.go$"
- "routers.go"
- "client.go"
- "configuration.go"
- "nas.go"
# by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules":
# If invoked with -mod=readonly, the go command is disallowed from the implicit
# automatic updating of go.mod described above. Instead, it fails when any changes
Expand Down Expand Up @@ -249,13 +243,14 @@ linters:
# Additional
- lll
- godox
#- gomnd
#- goconst
# - gomnd
# - goconst
# - gocognit
# - maligned
# - nestif
# - gomodguard
- nakedret
# - golint
- gci
- misspell
- gofumpt
Expand All @@ -266,9 +261,9 @@ linters:
- dogsled
- bodyclose
- asciicheck
#- stylecheck
# - unparam
# - wsl
# - stylecheck
# - unparam
# - wsl

#disable-all: false
fast: true
Expand Down
9 changes: 0 additions & 9 deletions CHANGELOG.md

This file was deleted.

File renamed without changes.
69 changes: 69 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"fmt"
"os"
"runtime/debug"

"github.com/asaskevich/govalidator"
"github.com/urfave/cli"

"github.com/free5gc/n3iwf/internal/logger"
"github.com/free5gc/n3iwf/pkg/service"
"github.com/free5gc/util/version"
)

var N3IWF = &service.N3IWF{}

func main() {
defer func() {
if p := recover(); p != nil {
// Print stack for panic to log. Fatalf() will let program exit.
logger.AppLog.Fatalf("panic: %v\n%s", p, string(debug.Stack()))
}
}()

app := cli.NewApp()
app.Name = "n3iwf"
app.Usage = "Non-3GPP Interworking Function (N3IWF)"
app.Action = action
app.Flags = N3IWF.GetCliCmd()
if err := app.Run(os.Args); err != nil {
logger.AppLog.Errorf("N3IWF Run Error: %v\n", err)
}
}

func action(c *cli.Context) error {
if err := initLogFile(c.String("log"), c.String("log5gc")); err != nil {
logger.AppLog.Errorf("%+v", err)
return err
}

if err := N3IWF.Initialize(c); err != nil {
switch errType := err.(type) {
case govalidator.Errors:
validErrs := err.(govalidator.Errors).Errors()
for _, validErr := range validErrs {
logger.CfgLog.Errorf("%+v", validErr)
}
default:
logger.CfgLog.Errorf("%+v", errType)
}
logger.CfgLog.Errorf("[-- PLEASE REFER TO SAMPLE CONFIG FILE COMMENTS --]")
return fmt.Errorf("Failed to initialize !!")
}

logger.AppLog.Infoln(c.App.Name)
logger.AppLog.Infoln("N3IWF version: ", version.GetVersion())

N3IWF.Start()

return nil
}

func initLogFile(logNfPath, log5gcPath string) error {
if err := logger.LogFileHook(logNfPath, log5gcPath); err != nil {
return err
}
return nil
}
11 changes: 4 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ go 1.14
require (
git.cs.nctu.edu.tw/calee/sctp v1.1.0
github.com/antonfisher/nested-logrus-formatter v1.3.1
github.com/free5gc/aper v1.0.2
github.com/free5gc/idgenerator v1.0.0
github.com/free5gc/logger_conf v1.0.0
github.com/free5gc/logger_util v1.0.0
github.com/free5gc/ngap v1.0.2
github.com/free5gc/path_util v1.0.0
github.com/free5gc/version v1.0.0
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
github.com/free5gc/aper v1.0.3
github.com/free5gc/ngap v1.0.5
github.com/free5gc/util v1.0.1
github.com/sirupsen/logrus v1.8.1
github.com/urfave/cli v1.22.5
github.com/vishvananda/netlink v1.1.0
Expand Down
135 changes: 89 additions & 46 deletions go.sum

Large diffs are not rendered by default.

49 changes: 35 additions & 14 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
formatter "github.com/antonfisher/nested-logrus-formatter"
"github.com/sirupsen/logrus"

"github.com/free5gc/logger_conf"
"github.com/free5gc/logger_util"
aperLogger "github.com/free5gc/aper/logger"
ngapLogger "github.com/free5gc/ngap/logger"
logger_util "github.com/free5gc/util/logger"
)

var log *logrus.Logger
Expand Down Expand Up @@ -39,16 +40,6 @@ func init() {
FieldsOrder: []string{"component", "category"},
}

free5gcLogHook, err := logger_util.NewFileHook(logger_conf.Free5gcLogFile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0o666)
if err == nil {
log.Hooks.Add(free5gcLogHook)
}

selfLogHook, err := logger_util.NewFileHook(logger_conf.NfLogDir+"n3iwf.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0o666)
if err == nil {
log.Hooks.Add(selfLogHook)
}

AppLog = log.WithFields(logrus.Fields{"component": "N3IWF", "category": "App"})
InitLog = log.WithFields(logrus.Fields{"component": "N3IWF", "category": "Init"})
CfgLog = log.WithFields(logrus.Fields{"component": "N3IWF", "category": "CFG"})
Expand All @@ -62,10 +53,40 @@ func init() {
UtilLog = log.WithFields(logrus.Fields{"component": "N3IWF", "category": "Util"})
}

func LogFileHook(logNfPath string, log5gcPath string) error {
if fullPath, err := logger_util.CreateFree5gcLogFile(log5gcPath); err == nil {
if fullPath != "" {
free5gcLogHook, hookErr := logger_util.NewFileHook(fullPath, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0o666)
if hookErr != nil {
return hookErr
}
log.Hooks.Add(free5gcLogHook)
aperLogger.GetLogger().Hooks.Add(free5gcLogHook)
ngapLogger.GetLogger().Hooks.Add(free5gcLogHook)
}
} else {
return err
}

if fullPath, err := logger_util.CreateNfLogFile(logNfPath, "n3iwf.log"); err == nil {
selfLogHook, hookErr := logger_util.NewFileHook(fullPath, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0o666)
if hookErr != nil {
return hookErr
}
log.Hooks.Add(selfLogHook)
aperLogger.GetLogger().Hooks.Add(selfLogHook)
ngapLogger.GetLogger().Hooks.Add(selfLogHook)
} else {
return err
}

return nil
}

func SetLogLevel(level logrus.Level) {
log.SetLevel(level)
}

func SetReportCaller(set bool) {
log.SetReportCaller(set)
func SetReportCaller(enable bool) {
log.SetReportCaller(enable)
}
7 changes: 3 additions & 4 deletions internal/util/initContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/free5gc/n3iwf/internal/logger"
"github.com/free5gc/n3iwf/pkg/context"
"github.com/free5gc/n3iwf/pkg/factory"
"github.com/free5gc/path_util"
)

var contextLog *logrus.Entry
Expand Down Expand Up @@ -113,7 +112,7 @@ func InitN3IWFContext() bool {

if factory.N3iwfConfig.Configuration.PrivateKey == "" {
contextLog.Warn("No private key file path specified, load default key file...")
keyPath = path_util.Free5gcPath("free5gc/support/TLS/n3iwf.key")
keyPath = N3iwfDefaultKeyPath
} else {
keyPath = factory.N3iwfConfig.Configuration.PrivateKey
}
Expand Down Expand Up @@ -154,7 +153,7 @@ func InitN3IWFContext() bool {

if factory.N3iwfConfig.Configuration.CertificateAuthority == "" {
contextLog.Warn("No certificate authority file path specified, load default CA certificate...")
keyPath = path_util.Free5gcPath("free5gc/support/TLS/n3iwf.pem")
keyPath = N3iwfDefaultPemPath
} else {
keyPath = factory.N3iwfConfig.Configuration.CertificateAuthority
}
Expand Down Expand Up @@ -193,7 +192,7 @@ func InitN3IWFContext() bool {

if factory.N3iwfConfig.Configuration.Certificate == "" {
contextLog.Warn("No certificate file path specified, load default certificate...")
keyPath = path_util.Free5gcPath("free5gc/support/TLS/n3iwf.pem")
keyPath = N3iwfDefaultPemPath
} else {
keyPath = factory.N3iwfConfig.Configuration.Certificate
}
Expand Down
7 changes: 7 additions & 0 deletions internal/util/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package util

const (
N3iwfDefaultPemPath = "./config/TLS/n3iwf.pem"
N3iwfDefaultKeyPath = "./config/TLS/n3iwf.key"
N3iwfDefaultConfigPath = "./config/n3iwfcfg.yaml"
)
45 changes: 0 additions & 45 deletions n3iwf.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
gtpv1 "github.com/wmnsk/go-gtp/gtpv1"
"golang.org/x/net/ipv4"

"github.com/free5gc/idgenerator"
"github.com/free5gc/n3iwf/internal/logger"
"github.com/free5gc/ngap/ngapType"
"github.com/free5gc/util/idgenerator"
)

var contextLog *logrus.Entry
Expand Down
Loading

0 comments on commit a6b839d

Please sign in to comment.