initial
This commit is contained in:
commit
25439eab5b
10 changed files with 185 additions and 0 deletions
41
config/config.go
Normal file
41
config/config.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
package config
|
||||
|
||||
type Config struct {
|
||||
App App
|
||||
Http Http
|
||||
TasksSource TasksSource
|
||||
}
|
||||
|
||||
type App struct {
|
||||
Mode string
|
||||
LogLvl string
|
||||
}
|
||||
|
||||
type Http struct {
|
||||
Host string
|
||||
Port string
|
||||
}
|
||||
|
||||
type TasksSource struct {
|
||||
Host string
|
||||
Port string
|
||||
}
|
||||
|
||||
func NewConfig() Config {
|
||||
return Config{
|
||||
App: App{
|
||||
Mode: getEnv("APP_MODE", "dev"),
|
||||
LogLvl: getEnv("APP_LOG_LVL", "debug"),
|
||||
},
|
||||
|
||||
Http: Http{
|
||||
Host: getEnv("HOST", "0.0.0.0"),
|
||||
Port: getEnv("PORT", "41082"),
|
||||
},
|
||||
|
||||
TasksSource: TasksSource{
|
||||
Host: getEnv("TASK_API_HOST", ""),
|
||||
Port: getEnv("TASK_API_PORT", ""),
|
||||
},
|
||||
}
|
||||
}
|
||||
10
config/helper.go
Normal file
10
config/helper.go
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package config
|
||||
|
||||
import "os"
|
||||
|
||||
func getEnv(key, fallback string) string {
|
||||
if value, ok := os.LookupEnv(key); ok {
|
||||
return value
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
48
config/logging.go
Normal file
48
config/logging.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/sirupsen/logrus"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func LogSetup(mode, lvl string) {
|
||||
l, err := logrus.ParseLevel(lvl)
|
||||
if err != nil {
|
||||
l = logrus.InfoLevel
|
||||
}
|
||||
|
||||
logrus.SetLevel(l)
|
||||
|
||||
switch mode {
|
||||
case "release":
|
||||
case "dev":
|
||||
{
|
||||
logrus.SetReportCaller(true)
|
||||
logrus.SetFormatter(&CustomFormatter{})
|
||||
logrus.SetOutput(os.Stdout)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type CustomFormatter struct{}
|
||||
|
||||
func (f *CustomFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
||||
timestamp := entry.Time.Format("2006-01-02 15:04:05")
|
||||
level := strings.ToUpper(entry.Level.String())
|
||||
msg := entry.Message
|
||||
|
||||
file := ""
|
||||
line := 0
|
||||
if entry.Caller != nil {
|
||||
file = filepath.Base(entry.Caller.File)
|
||||
line = entry.Caller.Line
|
||||
}
|
||||
|
||||
logLine := fmt.Sprintf("[%s][%s][%s:%d] %s\n",
|
||||
level, timestamp, file, line, msg)
|
||||
|
||||
return []byte(logLine), nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue