-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
69 lines (55 loc) · 1.68 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
package main
import (
"context"
"errors"
"flag"
"github.com/gin-gonic/gin"
"github.com/joeig/eee-safe/pkg/threema"
"github.com/spf13/viper"
"log"
)
// StorageBackend is an interface for basic storage operations
type StorageBackend interface {
PutBackup(ctx context.Context, backupInput *threema.BackupInput) error
GetBackup(ctx context.Context, backupID threema.BackupID) (*threema.BackupOutput, error)
DeleteBackup(ctx context.Context, backupID threema.BackupID) error
}
// AppContext contains the application context.
type AppContext struct {
Config *Config
StorageBackend StorageBackend
RequestIDGenerator RequestIDGenerator
}
// InitializeStorageBackend takes the configured storage backend type and initializes the storage backend.
func (a *AppContext) InitializeStorageBackend() error {
storageBackend := mapStorageBackendType(a.Config, &a.Config.StorageBackends)
if storageBackend == nil {
return errors.New("invalid storage backend")
}
a.StorageBackend = storageBackend
return nil
}
func main() {
configFile := flag.String("config", "config.yml", "Configuration file")
flag.Parse()
appCtx := &AppContext{
Config: &Config{},
RequestIDGenerator: NewRandomRequestIDGenerator(),
}
if err := appCtx.Config.Read(viper.New(), *configFile); err != nil {
panic(err)
}
if err := appCtx.InitializeStorageBackend(); err != nil {
panic(err)
}
gin.SetMode("release")
router := getGinEngine(appCtx)
if appCtx.Config.Server.TLS.Enable {
log.Fatal(router.RunTLS(
appCtx.Config.Server.ListenAddress,
appCtx.Config.Server.TLS.CertFile,
appCtx.Config.Server.TLS.KeyFile,
))
}
log.Fatal(router.Run(appCtx.Config.Server.ListenAddress))
}