Skip to content

Commit

Permalink
Merge pull request #1 from PereRohit/feat/config-server-logger-updates
Browse files Browse the repository at this point in the history
server, config & logger enhancement
  • Loading branch information
PereRohit authored Sep 15, 2022
2 parents a6be886 + d46c318 commit 05b14b9
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 5 deletions.
10 changes: 9 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import (
"reflect"
)

func LoadAsJson(filepath string, cfg interface{}) error {
type ServerConfig struct {
Host string `json:"host"`
Port string `json:"port"`
Version string `json:"version"`
Name string `json:"name"`
LogLevel string `json:"log_level"`
}

func LoadFromJson(filepath string, cfg interface{}) error {
content, err := ioutil.ReadFile(filepath)
if err != nil {
return err
Expand Down
68 changes: 65 additions & 3 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,83 @@ package log
import (
"log"
"os"
"sync"
"time"
)

type (
level int

Level string
)

const (
flags = 0
)

const (
LevelInfo Level = "info"
LevelWarn Level = "warn"
LevelError Level = "error"
LevelDebug Level = "debug"
)

const (
levelInfo level = iota
levelDebug
levelWarn
levelError
)

var (
selectedLogLevel = levelInfo
staticData = ""

logStaticDataSet sync.Once
)

func Info(v ...any) {
log.New(os.Stdout, "INF: "+time.Now().UTC().String()+" | ", flags).Println(v...)
if selectedLogLevel <= levelInfo {
log.New(os.Stdout, "INF: "+time.Now().UTC().String()+" | "+staticData, flags).Println(v...)
}
}

func Warn(v ...any) {
log.New(os.Stdout, "WRN: "+time.Now().UTC().String()+" | ", flags).Println(v...)
if selectedLogLevel <= levelWarn {
log.New(os.Stdout, "WRN: "+time.Now().UTC().String()+" | "+staticData, flags).Println(v...)
}
}

func Error(v ...any) {
log.New(os.Stdout, "ERR: "+time.Now().UTC().String()+" | ", flags).Println(v...)
if selectedLogLevel <= levelError {
log.New(os.Stdout, "ERR: "+time.Now().UTC().String()+" | "+staticData, flags).Println(v...)
}
}

func Debug(v ...any) {
if selectedLogLevel <= levelDebug {
log.New(os.Stdout, "DBG: "+time.Now().UTC().String()+" | "+staticData, flags).Println(v...)
}
}

func SetLogLevel(lvl string) {
switch Level(lvl) {
case LevelInfo:
selectedLogLevel = levelInfo
case LevelWarn:
selectedLogLevel = levelWarn
case LevelError:
selectedLogLevel = levelError
case LevelDebug:
selectedLogLevel = levelDebug
}
}

func SetStaticData(data string) {
if data == "" {
return
}
logStaticDataSet.Do(func() {
staticData = data + " | "
})
}
27 changes: 26 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
"net/http"
"os"
"os/signal"
"sync"
"syscall"

"github.com/PereRohit/util/config"
"github.com/PereRohit/util/constant"
"github.com/PereRohit/util/log"
)

func Run(r http.Handler) {
func Run(r http.Handler, svrConfig config.ServerConfig) {
// create channel to gracefully stop server
sc := make(chan os.Signal, 1)
signal.Notify(sc, os.Interrupt, syscall.SIGHUP,
Expand All @@ -23,16 +25,38 @@ func Run(r http.Handler) {
if !found || port == "" {
port = constant.DEFAULT_SERVER_PORT
}
if svrConfig.Port != "" {
port = svrConfig.Port
}
host, found := os.LookupEnv(constant.SERVER_HOST_ENV)
if !found || port == "" {
host = constant.DEFAULT_SERVER_HOST
}
if svrConfig.Host != "" {
host = svrConfig.Host
}

// set log data
log.SetLogLevel(svrConfig.LogLevel)
logStaticData := ""
if svrConfig.Name != "" {
logStaticData += "name: " + svrConfig.Name
}
if svrConfig.Version != "" {
logStaticData += " version: " + svrConfig.Version
}
log.SetStaticData(logStaticData)

s := &http.Server{
Addr: fmt.Sprintf("%s:%s", host, port),
Handler: r,
}
var srvStop sync.WaitGroup
defer srvStop.Wait()

go func() {
defer srvStop.Done()

// wait for termination signal
<-sc

Expand All @@ -44,6 +68,7 @@ func Run(r http.Handler) {
log.Info("Server Closed!!")
}()

srvStop.Add(1)
log.Info(fmt.Sprintf("Starting server(%s)", s.Addr))
err := s.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
Expand Down

0 comments on commit 05b14b9

Please sign in to comment.