-
Notifications
You must be signed in to change notification settings - Fork 78
/
testserver.go
91 lines (82 loc) · 2.6 KB
/
testserver.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
package main
import (
"log/slog"
"os"
"github.com/caarlos0/env/v9"
gin2 "github.com/gin-gonic/gin"
"github.com/grafana/beyla/test/integration/components/testserver/gin"
"github.com/grafana/beyla/test/integration/components/testserver/gorilla"
"github.com/grafana/beyla/test/integration/components/testserver/gorillamid"
"github.com/grafana/beyla/test/integration/components/testserver/gorillamid2"
grpctest "github.com/grafana/beyla/test/integration/components/testserver/grpc/server"
"github.com/grafana/beyla/test/integration/components/testserver/std"
)
/*
Server implementation to be used by integration tests.
Basically it's a server that accepts any method and path with a set of query parameters
that allow modifying its behavior (duration, response...)
*/
type config struct {
// STDPort to listen connections using the standard library
STDPort int `env:"STD_PORT" envDefault:"8080"`
// GinPort to listen connections using the Gin framework
GinPort int `env:"GIN_PORT" envDefault:"8081"`
// GorillaPort to listen connections using the Gorilla Mux framework
GorillaPort int `env:"GORILLA_PORT" envDefault:"8082"`
// GorillaPort to listen connections using the Gorilla Mux framework, but using a middleware that has custom ResposeWriter
GorillaMidPort int `env:"GORILLA_MID_PORT" envDefault:"8083"`
GorillaMid2Port int `env:"GORILLA_MID2_PORT" envDefault:"8087"`
LogLevel string `env:"LOG_LEVEL" envDefault:"INFO"`
}
func main() {
cfg := config{}
if err := env.Parse(&cfg); err != nil {
slog.Error("can't load configuration from environment", err)
os.Exit(-1)
}
setupLog(&cfg)
wait := make(chan struct{})
go func() {
std.Setup(cfg.STDPort)
close(wait)
}()
go func() {
gin2.SetMode(gin2.ReleaseMode)
gin.Setup(cfg.GinPort)
close(wait)
}()
go func() {
gorilla.Setup(cfg.GorillaPort, cfg.STDPort)
close(wait)
}()
go func() {
gorillamid.Setup(cfg.GorillaMidPort, cfg.STDPort)
close(wait)
}()
go func() {
gorillamid2.Setup(cfg.GorillaMid2Port, cfg.STDPort)
close(wait)
}()
go func() {
err := grpctest.Setup()
if err != nil {
slog.Error("HTTP server has unexpectedly stopped", err)
}
close(wait)
}()
// wait indefinitely unless any server crashes
<-wait
slog.Warn("stopping process")
}
func setupLog(cfg *config) {
lvl := slog.LevelInfo
err := lvl.UnmarshalText([]byte(cfg.LogLevel))
if err != nil {
slog.Error("unknown log level specified, choises are [DEBUG, INFO, WARN, ERROR]", err)
os.Exit(-1)
}
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: lvl,
})))
slog.Debug("logger is set", "level", lvl.String())
}