This repository has been archived by the owner on Aug 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (106 loc) · 2.91 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
package main
import (
"context"
"fmt"
"net/http"
"os"
firebase "firebase.google.com/go/v4"
"github.com/chriskuchin/roadrunner-results/pkg/controller"
"github.com/chriskuchin/roadrunner-results/pkg/db"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/honeycombio/honeycomb-opentelemetry-go"
"github.com/honeycombio/otel-config-go/otelconfig"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
)
var (
port string
debug bool
dbPath string
frontendFolder string
)
func main() {
app := &cli.App{
Name: "results",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "port",
Usage: "Port for the server to listen on",
Aliases: []string{"p"},
EnvVars: []string{"PORT", "NOMAD_HOST_PORT_run"},
Destination: &port,
Value: "3030",
},
&cli.BoolFlag{
Name: "debug",
Usage: "Enable Debug Logs",
Aliases: []string{"verbose"},
EnvVars: []string{""},
Destination: &debug,
Value: false,
},
&cli.StringFlag{
Name: "db-path",
Usage: "Specifies the path of the database",
Aliases: []string{"db"},
EnvVars: []string{"DB_PATH"},
Destination: &dbPath,
Value: "./results.db",
},
&cli.StringFlag{
Name: "frontend-folder",
Usage: "The frontend folder for the web assets",
Aliases: []string{"dist"},
EnvVars: []string{"FRONTEND_FOLDER"},
Destination: &frontendFolder,
Value: "./dist",
},
},
Commands: []*cli.Command{
{
Name: "server",
Usage: "launches the application web server",
Action: func(c *cli.Context) error {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
db := db.NewSQLite(dbPath)
app, err := firebase.NewApp(context.Background(), nil)
if err != nil {
log.Fatal().Msgf("error initializing app: %v\n", err)
}
r := chi.NewRouter()
// A good base middleware stack
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Mount("/", controller.Routes(app, db, frontendFolder, debug))
bsp := honeycomb.NewBaggageSpanProcessor()
otelshutdown, err := otelconfig.ConfigureOpenTelemetry(
otelconfig.WithSpanProcessor(bsp),
)
if err != nil {
log.Fatal().Err(err).Send()
}
defer otelshutdown()
log.Debug().Msgf("Launching server listening on: %s", port)
err = http.ListenAndServe(fmt.Sprintf(":%s", port), r)
if err != nil {
log.Error().Err(err).Send()
}
return err
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal().Err(err).Send()
}
}