-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
82 lines (71 loc) · 2.23 KB
/
web.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
package web
import (
"context"
"github.com/dimfeld/httptreemux/v5"
"github.com/google/uuid"
"log"
"net/http"
"os"
"syscall"
"time"
)
// Handler is a type that handles a http request within out own little mux
// mini framework.
type Handler func(ctx context.Context, w http.ResponseWriter, r *http.Request) error
// App is the entrypoint into out application and what configures our context
// object for each of out http handlers. Feel free to add any configuration
// data/logic on this App struct.
type App struct {
*httptreemux.ContextMux
shutdown chan os.Signal
mw []Middleware
}
// NewApp creates an App value that handles a set of routes for the application.
func NewApp(shutdown chan os.Signal, mw ...Middleware) *App {
return &App{
ContextMux: httptreemux.NewContextMux(),
shutdown: shutdown,
mw: mw,
}
}
// SignalShutdown is used to gracefully shut down the app when an integrity
// issue is identified.
func (a *App) SignalShutdown() {
a.shutdown <- syscall.SIGTERM
}
// Handle sets a handler function for a given HTTP method and path pair
// to the application server mux.
func (a *App) Handle(method, group, path string, handler Handler, mw ...Middleware) {
// First wrap handler specific middleware around this handler.
handler = wrapMiddleware(mw, handler)
// Add the application's general middleware to the handler chain.
handler = wrapMiddleware(a.mw, handler)
// The function to execute for each request.
h := func(w http.ResponseWriter, r *http.Request) {
// Pull the context with the required values to process the request.
ctx := r.Context()
// Set the context with the required values to process the request.
v := Values{
//TraceID: span.SpanContext().TraceID().String(),
TraceID: uuid.New().String(),
Now: time.Now(),
}
ctx = context.WithValue(ctx, key, &v)
v1, err := GetValues(ctx)
if err != nil {
return // web.NewShutdownError("web value missing from context)
}
log.Println("*******web.go******")
log.Println(v1.TraceID)
// Call the wrapped handler function.
if err := handler(ctx, w, r); err != nil {
a.SignalShutdown()
return
}
}
finalPath := path
if group != "" {
finalPath = "/" + group + path
}
a.ContextMux.Handle(method, finalPath, h)
}