-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
183 lines (157 loc) · 4.38 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
package main
import (
"context"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/go-chi/chi"
"github.com/joho/godotenv"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp"
"go.opentelemetry.io/otel/exporters/otlp/otlpgrpc"
"google.golang.org/grpc"
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
"go.opentelemetry.io/otel/sdk/metric/selector/simple"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"chat/app"
)
const addr = ":8000"
var tp *sdktrace.TracerProvider
func main() {
app.InitLogger()
err := godotenv.Load()
if err != nil {
app.Sugar.Fatalw("Error loading .env file: ", err)
}
pgHost, ok := os.LookupEnv("POSTGRES_HOST")
if !ok {
app.Sugar.Fatal("Could not find POSTGRES_HOST env")
}
pgDb, ok := os.LookupEnv("POSTGRES_DB")
if !ok {
app.Sugar.Fatal("Could not find POSTGRES_DB env")
}
pgUser, ok := os.LookupEnv("POSTGRES_USER")
if !ok {
app.Sugar.Fatal("Could not find POSTGRES_USER env")
}
pgPassword, ok := os.LookupEnv("POSTGRES_PASSWORD")
if !ok {
app.Sugar.Fatal("Could not find POSTGRES_PASSWORD env")
}
pgPortStr, ok := os.LookupEnv("POSTGRES_PORT")
if !ok {
app.Sugar.Fatal("Could not find POSTGRES_PORT env")
}
pgPort, err := strconv.ParseInt(pgPortStr, 10, 16)
if err != nil {
app.Sugar.Fatalf("Could not convert POSTGRES_PORT to a number. %v", pgPortStr)
}
pgConfig := app.PgConfig{
Host: pgHost,
Db: pgDb,
User: pgUser,
Password: pgPassword,
Port: uint16(pgPort),
}
scyllaHost, ok := os.LookupEnv("SCYLLA_HOST")
if !ok {
app.Sugar.Fatal("Could not find SCYLLA_HOST env")
}
scyllaKeyspace, ok := os.LookupEnv("KEYSPACE")
if !ok {
app.Sugar.Fatal("Could not find KEYSPACE env")
}
scyConfig := app.ScyllaConfig{
Host: scyllaHost,
Keyspace: scyllaKeyspace,
}
application := app.NewApp(pgConfig, scyConfig, "templates/*.html")
tracerCleanup := initTracer()
defer tracerCleanup()
defer application.Pg.Close()
app.Sugar.Infow("Setting up router.")
routes := application.Routes()
FileServer(routes, "/", http.Dir("./frontend"))
err = http.ListenAndServe(addr, routes)
if err != nil {
app.Sugar.Fatal("error starting server: ", err)
}
app.Sugar.Info("Starting server.")
}
func FileServer(r chi.Router, path string, root http.FileSystem) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit any URL parameters.")
}
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
path += "/"
}
path += "*"
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
fs := http.StripPrefix(pathPrefix, http.FileServer(root))
fs.ServeHTTP(w, r)
})
}
func addTracing(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
tracer := otel.Tracer("")
ctx, span := tracer.Start(req.Context(), "")
defer span.End()
next.ServeHTTP(w, req.WithContext(ctx))
})
}
func initTracer() func() {
ctx := context.Background()
exporter, err := otlp.NewExporter(
ctx,
otlpgrpc.NewDriver(
otlpgrpc.WithInsecure(),
otlpgrpc.WithEndpoint("localhost:4317"),
otlpgrpc.WithDialOption(grpc.WithBlock()),
),
)
if err != nil {
app.Sugar.Warn("failed to initialize honeycomb export pipeline: ", err)
return func() {}
}
pusher := controller.New(
processor.New(
simple.NewWithExactDistribution(),
exporter,
),
controller.WithExporter(exporter),
controller.WithCollectPeriod(5*time.Second),
)
err = pusher.Start(ctx)
if err != nil {
app.Sugar.Warn("error starting pusher: ", err)
}
bsp := sdktrace.NewBatchSpanProcessor(exporter)
provider := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSpanProcessor(bsp),
// sdktrace.WithBatcher(exporter),
)
otel.SetTracerProvider(provider)
// global.SetMeterProvider(pusher.MeterProvider())
// propagator := propagation.NewCompositeTextMapPropagator(propagation.Baggage{}, propagation.TraceContext{})
// otel.SetTextMapPropagator(propagator)
app.Sugar.Info("Tracing initialized.")
return func() {
ctx := context.Background()
err := provider.Shutdown(ctx)
if err != nil {
app.Sugar.Fatal(err)
}
err = pusher.Stop(ctx)
if err != nil {
app.Sugar.Fatal(err)
}
}
}