Skip to content

Commit

Permalink
Add server start output and refactor print function
Browse files Browse the repository at this point in the history
  • Loading branch information
funnyzak committed Feb 16, 2024
1 parent f932599 commit 5caf25c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
20 changes: 18 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,25 @@ func main() {

srv := controller.ServerWeb(port)

startOutput := func() {
fmt.Println()
fmt.Println("Server is running with config:")
utils.PrintStructFieldsAndValues(singleton.Conf, "")

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

if err := graceful.Graceful(func() error {
fmt.Printf(utils.Colorize("Server is running on port %d", utils.ColorGreen), port)
startOutput()

return srv.ListenAndServe()
}, func(c context.Context) error {
fmt.Print(utils.Colorize("Server is shutting down", utils.ColorRed))
Expand All @@ -49,7 +66,6 @@ func main() {
}); err != nil {
fmt.Println(utils.Colorize("Server is shutting down with error: %s", utils.ColorRed), err)
}

}

func initService() {
Expand Down
27 changes: 17 additions & 10 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,31 +133,38 @@ func GetNetworkIPs() ([]string, error) {
return ips, nil
}

func PrintStructFieldsAndValues(s interface{}, title string) error {
func PrintStructFieldsAndValues(s interface{}, indent string) {
v := reflect.ValueOf(s)

if v.Kind() == reflect.Ptr {
v = v.Elem()
}

if v.Kind() != reflect.Struct {
return fmt.Errorf("PrintStructFieldsAndValues: %v is not a struct", v.Type())
fmt.Printf("%s%v is not a struct\n", indent, v.Type())
return
}

typeOfS := v.Type()

fmt.Println()
if title != "" {
fmt.Printf("%s\n", title)
}
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
if field.CanInterface() {
fmt.Printf(" - %-20s: %v\n", typeOfS.Field(i).Name, Colorize(ColorGreen, fmt.Sprint(field.Interface())))
fmt.Printf("%s - %s: ", indent, typeOfS.Field(i).Name)

if field.Kind() == reflect.Struct {
fmt.Println()
PrintStructFieldsAndValues(field.Interface(), indent+" ")
} else if field.Kind() == reflect.Ptr && field.Elem().Kind() == reflect.Struct {
fmt.Println()
PrintStructFieldsAndValues(field.Interface(), indent+" ")
} else {
if field.CanInterface() {
fmt.Println(Colorize(ColorGreen, fmt.Sprint(field.Interface())))
} else {
fmt.Println()
}
}
}
fmt.Println()
return nil
}

func ParseBool(val string, defVal bool) bool {
Expand Down
12 changes: 7 additions & 5 deletions service/singleton/singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/rs/zerolog"
"github.com/spf13/viper"
"gorm.io/driver/sqlite"
"gorm.io/gorm"

Expand All @@ -17,22 +18,23 @@ import (
var Version = "0.0.1"

var (
Conf *gconfig.Config
Log *zerolog.Logger
DB *gorm.DB
ViperConf *viper.Viper
Conf *gconfig.Config
Log *zerolog.Logger
DB *gorm.DB
)

func InitSingleton() {
// TOO: init db
}

func InitConfig(name string) {
_config, err := utils.ReadViperConfig(name, "yaml", []string{".", "./config", "../"})
ViperConf, err := utils.ReadViperConfig(name, "yaml", []string{".", "./config", "../"})
if err != nil {
panic(fmt.Errorf("unable to read config: %s", err))
}

if err := _config.Unmarshal(&Conf); err != nil {
if err := ViperConf.Unmarshal(&Conf); err != nil {
panic(fmt.Errorf("unable to unmarshal config: %s", err))
}
}
Expand Down

0 comments on commit 5caf25c

Please sign in to comment.