-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (59 loc) · 1.62 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
package main
import (
"context"
"fmt"
"os"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/t-muehlberger/sharepass/pkg/api"
"github.com/t-muehlberger/sharepass/pkg/assets"
"github.com/t-muehlberger/sharepass/pkg/data"
"github.com/t-muehlberger/sharepass/pkg/secrets"
)
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --generate server,types,spec -o pkg/api/api.gen.go --package api openapi.yml
func main() {
if err := run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func run() error {
dataStore, err := getStore()
if err != nil {
return err
}
secretsSvc := secrets.Service{Store: dataStore}
var handler api.ServerInterface = &api.Handler{Svc: secretsSvc}
e := echo.New()
e.Use(middleware.Logger())
assets.RegisterSwaggerUiHandlers(e, "/swagger")
api.RegisterSwaggerDocHandler(e, "/api/v1/swagger.json")
api.RegisterHandlersWithBaseURL(e, handler, "/api/v1")
assets.RegisterWebUiHandlers(e)
metricsPass, metricsEnable := os.LookupEnv("METRICS_PASS")
if metricsEnable {
api.RegisterMetricsHandler(e, metricsPass)
api.CollectCountBackground(dataStore, context.Background())
}
e.Logger.Fatal(e.Start(":5000"))
return nil
}
func getStore() (secrets.Store, error) {
pgHost, usePostgres := os.LookupEnv("PG_HOST")
if !usePostgres {
return data.NewDiskStore("data")
}
pgDb, ok := os.LookupEnv("PG_DB")
if !ok {
pgDb = "postgres"
}
pgUser, ok := os.LookupEnv("PG_USER")
if !ok {
pgUser = "postgres"
}
pgPwd, ok := os.LookupEnv("PG_PWD")
if !ok {
pgPwd = "postgres"
}
return data.NewPostgresStore(pgHost, pgUser, pgPwd, pgDb)
}