-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (65 loc) · 1.79 KB
/
main.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
package main
import (
"fmt"
"os"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/spf13/pflag"
"github.com/alexandrevilain/teleinfo-timescaledb/cmd/server/handlers"
"github.com/alexandrevilain/teleinfo-timescaledb/pkg/auth"
"github.com/alexandrevilain/teleinfo-timescaledb/pkg/database"
"github.com/alexandrevilain/teleinfo-timescaledb/pkg/log"
)
func main() {
err := run()
if err != nil {
fmt.Printf("Error: %s", err)
os.Exit(1)
}
}
func run() error {
logger, err := log.CreateLogger()
if err != nil {
return err
}
k := koanf.New(".")
f := pflag.NewFlagSet("", pflag.ContinueOnError)
f.Usage = func() {
fmt.Println(f.FlagUsages())
os.Exit(0)
}
f.StringSlice("config", []string{"config.yaml"}, "path to one or more .yaml config files")
f.Parse(os.Args[1:])
configFiles, _ := f.GetStringSlice("config")
for _, c := range configFiles {
if err := k.Load(file.Provider(c), yaml.Parser()); err != nil {
return fmt.Errorf("error loading file: %v", err)
}
}
cfg := &Config{}
k.Unmarshal("", cfg)
db, err := database.Open(cfg.DB)
if err != nil {
return fmt.Errorf("connecting to db: %w", err)
}
logger.Info("Starting server")
e := echo.New()
e.HideBanner = true
e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
LogURI: true,
LogStatus: true,
LogError: true,
LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
logger.Info("request", "URI", v.URI, "status", v.Status, "error", v.Error)
return nil
},
}))
e.Use(auth.Middleware(cfg.AuthorizedTokens))
if err := handlers.AddRoutes(logger, db, e); err != nil {
return err
}
return e.Start(cfg.ListenAdress)
}