initial
This commit is contained in:
commit
a72002540d
9 changed files with 133 additions and 0 deletions
21
config/config.go
Normal file
21
config/config.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package config
|
||||
|
||||
type Config struct {
|
||||
App AppConfig
|
||||
}
|
||||
|
||||
type AppConfig struct {
|
||||
GrpcHost string
|
||||
GrpcPort string
|
||||
LogLevel string
|
||||
}
|
||||
|
||||
func NewConfig() *Config {
|
||||
return &Config{
|
||||
App: AppConfig{
|
||||
GrpcHost: getEnv("GRPC_HOST", ""),
|
||||
GrpcPort: getEnv("GRPC_PORT", ""),
|
||||
LogLevel: getEnv("LOG_LEVEL", ""),
|
||||
},
|
||||
}
|
||||
}
|
||||
24
config/getEnv.go
Normal file
24
config/getEnv.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func getEnv(key, fallback string) string {
|
||||
if value, ok := os.LookupEnv(key); ok {
|
||||
return value
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func getEnvInt(key string, fallback int) int {
|
||||
if value, ok := os.LookupEnv(key); ok {
|
||||
num, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
return fallback
|
||||
}
|
||||
return num
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
35
config/logging.go
Normal file
35
config/logging.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"os"
|
||||
"path"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func LogSetup(lvl string) {
|
||||
l, err := log.ParseLevel(lvl)
|
||||
if err != nil {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
}
|
||||
|
||||
log.SetFormatter(
|
||||
&log.TextFormatter{
|
||||
FullTimestamp: true,
|
||||
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
|
||||
filename := path.Base(f.File)
|
||||
return fmt.Sprintf("%s()", f.Function), fmt.Sprintf(" %s:%d", filename, f.Line)
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
if l == log.DebugLevel {
|
||||
log.SetLevel(l)
|
||||
log.SetReportCaller(true)
|
||||
} else {
|
||||
log.SetLevel(l)
|
||||
}
|
||||
|
||||
log.SetOutput(os.Stdout)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue