Skip to content

Commit

Permalink
refactor: add debug mode and network IP exclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
funnyzak committed Feb 22, 2024
1 parent bc706c3 commit 227b37e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 36 deletions.
84 changes: 48 additions & 36 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/ory/graceful"
Expand All @@ -15,81 +16,92 @@ import (
"go-gin/service/singleton"
)

type CliParam struct {
type ClIParam struct {
Version bool // Show version
ConfigName string // Config file name
Port uint // Server port
Debug bool // Debug mode
}

var (
cliParam CliParam
)
var cliParam ClIParam

func init() {
func parseCommandLineParams() (cliParam ClIParam) {
flag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true
flag.BoolVarP(&cliParam.Version, "version", "v", false, "show version")
flag.StringVarP(&cliParam.ConfigName, "config", "c", "config", "config file name")
flag.UintVarP(&cliParam.Port, "port", "p", 0, "server port")
flag.BoolVarP(&cliParam.Debug, "debug", "d", false, "debug mode")
flag.Parse()
flag.Lookup("config").NoOptDefVal = "config"
singleton.InitConfig(cliParam.ConfigName)
singleton.InitLog(singleton.Conf)
singleton.InitTimezoneAndCache()
singleton.InitDBFromPath(singleton.Conf.DBPath)
initService()
return cliParam
}

func main() {
if cliParam.Version {
fmt.Println(singleton.Version)
return
}
func loadConfig() {
cliParam = parseCommandLineParams()

port := singleton.Conf.Server.Port
if cliParam.Port != 0 {
port = cliParam.Port
singleton.InitConfig(cliParam.ConfigName)
if cliParam.Port > 0 && cliParam.Port < 65536 {
singleton.Conf.Server.Port = cliParam.Port
}
if cliParam.Debug {
singleton.Conf.Debug = cliParam.Debug
}
}

srv := controller.ServerWeb(port)

startOutput := func() {
func startupOutput(httpserver *http.Server) {
if singleton.Conf.Debug {
fmt.Println()
fmt.Printf("Service version: %s\n", utils.Colorize(utils.ColorGreen, singleton.Version))

fmt.Println()
fmt.Println("Server available routes:")
mygin.PrintRoute(srv.Handler.(*gin.Engine))
mygin.PrintRoute(httpserver.Handler.(*gin.Engine))

fmt.Println()
fmt.Println("Server is running with config:")
fmt.Println("Server running with config:")
utils.PrintStructFieldsAndValues(singleton.Conf, "")
}

fmt.Println()
ipv4s, err := ip.GetIPv4NetworkIPs()
if ipv4s != nil && err == nil {
fmt.Println("Server is running at:")
for _, ip := range ipv4s {
fmt.Printf(" - %-7s: %s\n", "Network", utils.Colorize(utils.ColorGreen, fmt.Sprintf("http://%s:%d", ip, port)))
}
fmt.Println()
fmt.Println("Server is running at:")
fmt.Printf(" - %-7s: %s\n", "Local", utils.Colorize(utils.ColorGreen, fmt.Sprintf("http://127.0.0.1:%d", singleton.Conf.Server.Port)))
ipv4s, err := ip.GetIPv4NetworkIPs()
if ipv4s != nil && err == nil {
for _, ip := range ipv4s {
fmt.Printf(" - %-7s: %s\n", "Network", utils.Colorize(utils.ColorGreen, fmt.Sprintf("http://%s:%d", ip, singleton.Conf.Server.Port)))
}
fmt.Println()
}
fmt.Println()
}

fmt.Printf("Current service version: %s\n", utils.Colorize(utils.ColorGreen, singleton.Version))
fmt.Println()
func init() {
loadConfig()
singleton.InitLog(singleton.Conf)
singleton.InitTimezoneAndCache()
singleton.InitDBFromPath(singleton.Conf.DBPath)
initService()
}

func main() {
if cliParam.Version {
fmt.Println(singleton.Version)
return
}

srv := controller.ServerWeb(singleton.Conf.Server.Port)
if err := graceful.Graceful(func() error {
startOutput()
startupOutput(srv)
return srv.ListenAndServe()
}, func(c context.Context) error {
fmt.Print(utils.Colorize("Server is shutting down", utils.ColorRed))
srv.Shutdown(c)
return nil
}); err != nil {
fmt.Println(utils.Colorize("Server is shutting down with error: %s", utils.ColorRed), err)
fmt.Printf("Server is shutting down with error: %s", utils.Colorize(utils.ColorRed, err.Error()))
}
}

func initService() {
// Load all services in the singleton package
singleton.LoadSingleton()

// if _, err := singleton.Cron.AddFunc("0 * * * * *", sayHello); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/utils/ip/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func GetNetworkIPs() ([]string, error) {

ips := make([]string, 0)
for _, i := range ifaces {
if i.Flags&net.FlagLoopback != 0 {
continue
}
addrs, err := i.Addrs()
if err != nil {
continue
Expand Down

0 comments on commit 227b37e

Please sign in to comment.