-
Notifications
You must be signed in to change notification settings - Fork 0
/
starter.go
106 lines (95 loc) · 3.43 KB
/
starter.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"flag"
"fmt"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/viper"
)
var (
configFileName string
requests = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "http_endpoint_equests_count",
Help: "The amount of requests to an endpoint",
}, []string{"endpoint", "method", "type"},
)
)
type ConfigType struct {
Logging ConfigLogging `mapstructure:"logging"`
Port string `mapstructure:"port"`
Backend ConfigBackend `mapstructure:"backend"`
Prometheus ConfigPrometheus `mapstructure:"prometheus"`
}
type ConfigLogging struct {
Level string `mapstructure:"level"`
Format string `mapstructure:"format"`
File string `mapstructure:"file"`
}
type ConfigBackend struct {
Host string `mapstructure:"host"`
Port string `mapstructure:"port"`
Protocol string `mapstructure:"protocol"`
Cert string `mapstructure:"cert"`
CertDir string `mapstructure:"certificateDirectory"`
insecure bool `mapstructure:"insecure"`
Key string `mapstructure:"key"`
Username string `mapstructure:"username"`
}
type ConfigPrometheus struct {
Enabled bool `mapstructure:"enabled"`
Endpoint string `mapstructure:"endpoint"`
}
const (
BaseENVname = "KVDBW"
)
func ConfigRead(configFileName string, configOutput *ConfigType) {
configReader := viper.New()
configReader.SetConfigName(configFileName)
configReader.SetConfigType("yaml")
configReader.AddConfigPath("/app/")
configReader.AddConfigPath(".")
configReader.SetEnvPrefix(BaseENVname)
configReader.SetDefault("logging.level", "Debug")
configReader.SetDefault("logging.format", "text")
configReader.SetDefault("port", 8080)
configReader.SetDefault("backend.host", "kvdb")
// https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
configReader.SetDefault("backend.port", 443)
configReader.SetDefault("backend.protocol", "https")
configReader.SetDefault("backend.cert", "client.crt")
configReader.SetDefault("backend.key", "client.key")
configReader.SetDefault("backend.certificateDirectory", "/certificates/")
configReader.SetDefault("backend.username", "system")
configReader.SetDefault("backend.insecure", false)
configReader.SetDefault("prometheus.enabled", true)
configReader.SetDefault("prometheus.endpoint", "/system/metrics")
err := configReader.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %w", err))
}
configReader.AutomaticEnv()
configReader.Unmarshal(configOutput)
}
type Health struct {
Status string `json:"status"`
}
func main() {
flag.StringVar(&configFileName, "config", "config", "Use a different config file name")
flag.Parse()
App := new(Application)
ConfigRead(configFileName, &App.Config)
App.setupLogging()
httpClient := InitClient(App.Config.Backend)
App.KVDBClient = httpClient
if App.Config.Prometheus.Enabled {
App.Logger.Info(fmt.Sprintf("Metrics enabled at %v", App.Config.Prometheus.Endpoint))
http.Handle(App.Config.Prometheus.Endpoint, promhttp.Handler())
}
http.HandleFunc("/", http.HandlerFunc(App.RootController))
http.HandleFunc("/system/health", http.HandlerFunc(App.HealthActuator))
App.Logger.Info(fmt.Sprintf("Serving on port %v", App.Config.Port))
log.Fatal(http.ListenAndServe(":"+App.Config.Port, nil))
}