-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
189 lines (156 loc) · 5.21 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"context"
"fmt"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/alecthomas/kong"
"github.com/johnstairs/pathenvconfig"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/ismrmrd/mrd-storage-server/api"
"github.com/ismrmrd/mrd-storage-server/core"
"github.com/ismrmrd/mrd-storage-server/database"
"github.com/ismrmrd/mrd-storage-server/storage"
)
const (
ConfigDatabaseProviderSqlite = "sqlite"
ConfigDatabaseProviderPostgresql = "postgresql"
ConfigStorageProviderFileSystem = "filesystem"
ConfigStorageProviderAzureBlob = "azureblob"
)
type Args struct {
PrettyPrint bool `help:"Pretty-print logs." short:"p"`
LogLevel string `help:"Set the minimum log level to emit." short:"l" default:"Info" enum:"Debug,Info,Warn,Error,Fatal,Panic,Disabled"`
RequireParentPid int `help:"Exit when the parent process' PID differs from the given value." default:"-1" hidden:""`
}
func main() {
args := Args{}
kong.Parse(&args, kong.UsageOnError())
configureZerolog(args)
startParentProcessCheck(args)
config := loadConfig()
db, blobStore, err := assembleDataStores(config)
if err != nil {
log.Fatal().Err(err).Send()
}
handler := assembleHandler(db, blobStore, config)
go garbageCollectionLoop(context.Background(), db, blobStore)
l, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Port))
if err != nil {
log.Fatal().Err(err).Send()
}
log.Info().Msgf("Listening on port %d", config.Port)
err = http.Serve(l, handler)
log.Fatal().Err(err).Send()
}
func loadConfig() ConfigSpec {
var config ConfigSpec
err := pathenvconfig.Process("MRD_STORAGE_SERVER", &config)
if err != nil {
log.Fatal().Err(err).Send()
}
return config
}
func assembleDataStores(config ConfigSpec) (core.MetadataDatabase, core.BlobStore, error) {
db, err := createMetadataRepository(config)
if err != nil {
return nil, nil, fmt.Errorf("unable to initialize metadata database: %v", err)
}
blobStore, err := createBlobStore(config)
if err != nil {
return nil, nil, fmt.Errorf("unable to initialize storage: %v", err)
}
return db, blobStore, nil
}
func assembleHandler(db core.MetadataDatabase, blobStore core.BlobStore, config ConfigSpec) http.Handler {
return api.BuildRouter(db, blobStore, config.LogRequests)
}
func createMetadataRepository(config ConfigSpec) (core.MetadataDatabase, error) {
switch strings.ToLower(config.DatabaseProvider) {
case ConfigDatabaseProviderSqlite:
return database.OpenSqliteDatabase(config.DatabaseConnectionString)
case ConfigDatabaseProviderPostgresql:
return database.ConnectPostgresqlDatabase(config.DatabaseConnectionString, config.DatabasePassword)
}
return nil, fmt.Errorf("unrecognized database provider '%s'", config.DatabaseProvider)
}
func createBlobStore(config ConfigSpec) (core.BlobStore, error) {
switch strings.ToLower(config.StorageProvider) {
case ConfigStorageProviderFileSystem:
return storage.NewFileSystemStore(config.StorageConnectionString)
case ConfigStorageProviderAzureBlob:
return storage.NewAzureBlobStore(config.StorageConnectionString)
}
return nil, fmt.Errorf("unrecognized storage provider '%s'", config.StorageProvider)
}
func garbageCollectionLoop(ctx context.Context, db core.MetadataDatabase, blobStore core.BlobStore) {
ticker := time.NewTicker(30 * time.Minute)
for range ticker.C {
for i := 0; i < 10; i++ {
log.Ctx(ctx).Info().Msg("Begining garbage collection")
err := core.CollectGarbage(ctx, db, blobStore, time.Now().Add(-30*time.Minute).UTC())
if err == nil {
log.Ctx(ctx).Info().Msg("Garbage collection completed")
break
}
log.Ctx(ctx).Error().Msgf("Garbage collection failed: %v", err)
time.Sleep(30 * time.Second)
}
}
}
func configureZerolog(args Args) {
zerolog.TimeFieldFormat = "2006-01-02T15:04:05.999Z07:00"
var level zerolog.Level
switch args.LogLevel {
case "Trace":
level = zerolog.TraceLevel
case "Debug":
level = zerolog.DebugLevel
case "Info":
level = zerolog.InfoLevel
case "Warn":
level = zerolog.WarnLevel
case "Error":
level = zerolog.ErrorLevel
case "Fatal":
level = zerolog.FatalLevel
case "Panic":
level = zerolog.PanicLevel
case "Disabled":
level = zerolog.Disabled
}
zerolog.SetGlobalLevel(level)
if args.PrettyPrint {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
zerolog.DefaultContextLogger = &log.Logger
}
func startParentProcessCheck(args Args) {
if args.RequireParentPid < 0 {
return
}
// This is an undocumented "feature" to terminate this process when its parent process exits.
// When the parent process exits, this process will be adopted by the init process (PID 1) and therefore
// the call to os.Getppdid() will start returning 1.
go func() {
for {
if os.Getppid() != args.RequireParentPid {
log.Fatal().Msg("Terminating because the parent process has exited")
}
time.Sleep(time.Second)
}
}()
}
type ConfigSpec struct {
DatabaseProvider string `default:"sqlite"`
DatabaseConnectionString string `default:"_data/metadata.db"`
DatabasePassword string
StorageProvider string `default:"filesystem"`
StorageConnectionString string `default:"_data/blobs"`
Port int `default:"3333"`
LogRequests bool `default:"true"`
}