/
main.go
79 lines (67 loc) · 1.96 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"github.com/kostyay/otel-demo/common/log"
"github.com/kostyay/otel-demo/common/otel"
"github.com/kostyay/otel-demo/common/version"
"github.com/kostyay/otel-demo/controller/internal/config"
"github.com/kostyay/otel-demo/controller/internal/handler"
"github.com/kostyay/otel-demo/controller/internal/math"
"github.com/kostyay/otel-demo/controller/internal/storage"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)
func run(ctx context.Context, cfg *config.Options) error {
db, err := storage.New(cfg)
if err != nil {
return fmt.Errorf("unable to initialize storage: %w", err)
}
log.Info("storage initialized")
m, err := math.New(ctx, cfg, db)
if err != nil {
return fmt.Errorf("unable to initialize math agent: %w", err)
}
log.Info("math agent initialized")
controller := handler.New(db, m)
mux := http.NewServeMux()
// The generated constructors return a path and a plain net/http
// handler.
controller.Register(mux)
return http.ListenAndServe(
cfg.ListenAddr,
// For gRPC clients, it's convenient to support HTTP/2 without TLS. You can
// avoid x/net/http2 by using http.ListenAndServeTLS.
h2c.NewHandler(mux, &http2.Server{}),
)
}
func main() {
ctx := context.Background()
log.Info(fmt.Sprintf("Starting server - %s;%s", version.ServiceName, version.Version))
cfg, err := config.Parse()
if err != nil {
log.WithError(err).Error("failed to parse config")
os.Exit(1)
}
tp, err := otel.InitTracing(ctx, otel.Config{
ProjectID: cfg.GoogleCloudProject,
ServiceName: version.ServiceName,
ServiceVersion: version.Version,
})
if err != nil {
log.WithError(err).Error("failed to init trace")
os.Exit(1)
}
defer func() {
if err := tp.Shutdown(ctx); err != nil {
log.WithError(err).Error("failed to shutdown trace")
}
}()
log.Info("Trace initialized")
if err := run(ctx, cfg); err != nil {
log.WithError(err).Error("failed to run server")
os.Exit(1)
}
}