-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmain.go
More file actions
92 lines (79 loc) · 2.59 KB
/
Copy pathmain.go
File metadata and controls
92 lines (79 loc) · 2.59 KB
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
package main
import (
"flag"
"log"
"log/slog"
"os"
"github.com/checkmarble/marble-backend/api"
"github.com/checkmarble/marble-backend/cmd"
"github.com/checkmarble/marble-backend/utils"
)
// Static variable set at compilation-time through linker flags.
//
// $ go build -ldflags '-X main.apiVersion=v0.10.0 -X ...' .
var (
apiVersion string = "dev"
segmentWriteKey string = ""
)
var compiledConfig = cmd.CompiledConfig{
Version: apiVersion,
SegmentWriteKey: segmentWriteKey,
}
func main() {
shouldRunMigrations := flag.Bool("migrations", false, "Run migrations")
migrateDownTo := flag.Int64("down-to", -1, "Migrate down to a specific version (expects an integer version number)")
shouldRunServer := flag.Bool("server", false, "Run server")
shouldRunWorker := flag.Bool("worker", false, "Run workers on the task queues")
shouldRunAnalyticsServer := flag.Bool("analytics", false, "Run analytics server")
// DEVELOPMENT-ONLY: those flags are used to help debugging and cannot be used in production
var (
workerOnly = utils.Ptr("")
workerOnlyArgs = utils.Ptr("")
)
if apiVersion == "dev" || os.Getenv("ENABLE_WORKER_ONESHOT") == "1" {
workerOnly = flag.String("worker-only", "", "only run a specific job to completion")
workerOnlyArgs = flag.String("worker-args", "", "JSON-encoded arguments to the worker")
}
flag.Parse()
// reset the argument to nil if not passed or default value is passed, for clearer edge case handling
// in the code downstream.
if *migrateDownTo == -1 {
migrateDownTo = nil
}
if !*shouldRunWorker && (*workerOnly != "" || *workerOnlyArgs != "") {
log.Fatal("-worker-only and -worker-args can only be used when running the worker")
}
logger := utils.NewLogger("text")
logger.Debug("Flags",
slog.Bool("shouldRunMigrations", *shouldRunMigrations),
slog.Bool("shouldRunServer", *shouldRunServer),
slog.Bool("shouldRunWorker", *shouldRunWorker),
slog.Bool("shouldRunAnalyticsServer", *shouldRunAnalyticsServer),
)
if migrateDownTo != nil && !*shouldRunMigrations {
log.Fatal("-down-to can only be used together with -migrations")
}
if *shouldRunMigrations {
if err := cmd.RunMigrations(apiVersion, migrateDownTo); err != nil {
log.Fatal(err)
}
}
if *shouldRunServer {
err := cmd.RunServer(compiledConfig, api.ServerModeDefault)
if err != nil {
log.Fatal(err)
}
}
if *shouldRunAnalyticsServer {
err := cmd.RunServer(compiledConfig, api.ServerModeAnalytics)
if err != nil {
log.Fatal(err)
}
}
if *shouldRunWorker {
err := cmd.RunTaskQueue(apiVersion, *workerOnly, *workerOnlyArgs)
if err != nil {
log.Fatal(err)
}
}
}