This repository was archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
182 lines (138 loc) · 4.66 KB
/
main.go
File metadata and controls
182 lines (138 loc) · 4.66 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
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
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"log/slog"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"github.com/x-ethr/server"
"github.com/x-ethr/server/logging"
"github.com/x-ethr/server/middleware"
"github.com/x-ethr/server/middleware/name"
"github.com/x-ethr/server/middleware/servername"
"github.com/x-ethr/server/middleware/timeout"
"github.com/x-ethr/server/middleware/versioning"
"github.com/x-ethr/server/telemetry"
"go.opentelemetry.io/otel/trace"
)
// header is a dynamically linked string value - defaults to "server" - which represents the server name.
var header string = "server"
// service is a dynamically linked string value - defaults to "service" - which represents the service name.
var service string = "service"
// version is a dynamically linked string value - defaults to "development" - which represents the service's version.
var version string = "development" // production builds have version dynamically linked
var prefix = map[string]string{
(version): "v1", // default version prefix
}
// ctx, cancel represent the server's runtime context and cancellation handler.
var ctx, cancel = context.WithCancel(context.Background())
// port represents a cli flag that sets the server listening port
var port = flag.String("port", "8080", "Server Listening Port.")
var (
tracer = otel.Tracer(service)
)
func main() {
// Create an instance of the custom handler
mux := server.New()
mux.Middleware(middleware.New().Path().Middleware)
mux.Middleware(middleware.New().Timeout().Configuration(func(options *timeout.Settings) {
options.Timeout = 30 * time.Second
}).Middleware)
mux.Middleware(middleware.New().Server().Configuration(func(options *servername.Settings) {
options.Server = header
}).Middleware)
mux.Middleware(middleware.New().Service().Configuration(func(options *name.Settings) {
options.Service = service
}).Middleware)
mux.Middleware(middleware.New().Version().Configuration(func(options *versioning.Settings) {
options.Version.Service = version
}).Middleware)
mux.Middleware(middleware.New().Telemetry().Middleware)
mux.Register(fmt.Sprintf("GET /%s/%s", prefix[version], service), func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
resources := telemetry.Resources(ctx, service, version)
ctx, span := tracer.Start(ctx, fmt.Sprintf("%s - main", service))
span.SetAttributes(resources.Attributes()...)
defer span.End()
channel := make(chan map[string]interface{}, 1)
var process = func(ctx context.Context, span trace.Span, c chan map[string]interface{}) {
path := middleware.New().Path().Value(ctx)
var payload = map[string]interface{}{
middleware.New().Service().Value(ctx): map[string]interface{}{
"path": path,
"service": middleware.New().Service().Value(ctx),
"version": middleware.New().Version().Value(ctx).Service,
},
}
span.SetAttributes(attribute.String("path", path))
c <- payload
}
go process(ctx, span, channel)
select {
case <-ctx.Done():
return
case payload := <-channel:
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Request-ID", r.Header.Get("X-Request-ID"))
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(payload)
return
}
})
// Start the HTTP server
slog.Info("Starting Server ...", slog.String("local", fmt.Sprintf("http://localhost:%s", *(port))))
api := server.Server(ctx, mux, *port)
// Issue Cancellation Handler
server.Interrupt(ctx, cancel, api)
// Telemetry Setup
shutdown, e := telemetry.Setup(ctx, service, version, func(options *telemetry.Settings) {
if version == "development" && os.Getenv("CI") == "" {
options.Zipkin.Enabled = false
options.Tracer.Local = true
options.Metrics.Local = true
options.Logs.Local = true
}
})
if e != nil {
panic(e)
}
defer func() {
e = errors.Join(e, shutdown(ctx))
}()
// <-- Blocking
if e := api.ListenAndServe(); e != nil && !(errors.Is(e, http.ErrServerClosed)) {
slog.ErrorContext(ctx, "Error During Server's Listen & Serve Call ...", slog.String("error", e.Error()))
os.Exit(100)
}
// --> Exit
{
slog.InfoContext(ctx, "Graceful Shutdown Complete")
// Waiter
<-ctx.Done()
}
}
func init() {
flag.Parse()
level := slog.Level(-8)
if os.Getenv("CI") == "true" {
level = slog.LevelDebug
}
logging.Level(level)
if service == "service" && os.Getenv("CI") != "true" {
_, file, _, ok := runtime.Caller(0)
if ok {
service = filepath.Base(filepath.Dir(file))
}
}
handler := logging.Logger(func(o *logging.Options) { o.Service = service })
logger := slog.New(handler)
slog.SetDefault(logger)
}