-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
106 lines (82 loc) · 2.07 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
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 (
"context"
"errors"
"flag" //nolint:depguard // We only allow to import the flag package in here
"fmt"
"io/fs"
"log/slog"
"net/http"
"os"
"github.com/helsinki-systems/settleup/pkg/api"
"github.com/helsinki-systems/settleup/pkg/server"
"github.com/joho/godotenv"
)
//nolint:gochecknoglobals // Nice to use as a global
var logTarget = os.Stderr
func run(ctx context.Context, c Config) error {
ac := api.New(c.apiConf)
if _, err := ac.Login(ctx); err != nil {
return fmt.Errorf("failed to login at API: %w", err)
}
srv := server.New(c.serverConf, ac)
if err := srv.ListenAndServe(); err != nil {
return fmt.Errorf("server failed to listen and serve: %w", err)
}
return nil
}
func main() {
httpTCPBind := flag.String("http.tcp.bind", ":8080", "the TCP socket to bind to")
debug := flag.Bool("debug", false, "enable debug mode")
flag.Parse()
ctx := context.Background()
ll := new(slog.LevelVar)
ll.Set(slog.LevelInfo)
l := slog.New(slog.NewJSONHandler(logTarget, &slog.HandlerOptions{
Level: ll,
}))
slog.SetDefault(l)
if err := godotenv.Load(); err != nil {
if errors.Is(err, fs.ErrNotExist) {
l.Info("no .env file found, service may fail to start")
} else {
l.Error(
"failed to load env",
"err", err,
)
}
}
// We have a debug env var as well as a debug CLI flag
if getenv("DEBUG", "false") == "true" {
*debug = true
}
if *debug {
ll.Set(slog.LevelDebug)
}
c := Config{
serverConf: server.Config{
Logger: l.With("svc", "server"),
HTTPTCPBind: *httpTCPBind,
},
apiConf: api.Config{
Logger: l.With("svc", "api"),
HTTPClient: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
},
APIConf: api.APIConfig{
BaseURL: getenv("API_BASE_URL", ""),
Key: getenv("API_KEY", ""),
SettleUpConf: api.SettleUpConfig{
Username: getenv("SETTLEUP_USERNAME", ""),
Password: getenv("SETTLEUP_PASSWORD", ""),
GroupID: getenv("SETTLEUP_GROUP_ID", ""),
},
},
},
}
if err := run(ctx, c); err != nil {
l.Error(err.Error())
}
}