-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.go
60 lines (52 loc) · 1.2 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* Copyright (c) 2024 Intel Corporation
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
*/
package kbs
import (
"fmt"
"os"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"intel/kbs/v1/config"
)
type App struct {
Config *config.Configuration
}
func (app *App) Run() {
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
viper.AutomaticEnv()
if app.configuration() == nil {
app.Config = config.DefaultConfig()
}
err := app.startServer()
if err != nil {
fmt.Printf("KBS application exit with an error : %v\n", err.Error())
os.Exit(1)
}
}
func (app *App) configuration() *config.Configuration {
if app.Config != nil {
return app.Config
}
cfg, err := config.LoadConfiguration()
if err == nil {
app.Config = cfg
return app.Config
}
return nil
}
func (app *App) configureLogs() error {
lv, err := logrus.ParseLevel(app.Config.LogLevel)
if err != nil {
return errors.Wrap(err, "Failed to initiate loggers. Invalid log level: "+app.Config.LogLevel)
}
logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetReportCaller(app.Config.LogCaller)
logrus.SetLevel(lv)
logrus.Info("logger initialized")
return nil
}