forked from fasthttp/router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
104 lines (85 loc) · 1.99 KB
/
Copy pathexample_test.go
File metadata and controls
104 lines (85 loc) · 1.99 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
package httx_test
import (
"context"
"encoding/json"
"errors"
"log"
"log/slog"
"net/http"
"strconv"
"sync"
"time"
"github.com/sirkostya009/httx"
)
type ExampleServer struct {
/// some injected deps
r *httx.Mux
}
type customWriter struct {
http.ResponseWriter
status int
}
func (w *customWriter) WriteHeader(statusCode int) {
if w.status == 0 {
w.status = statusCode
}
w.ResponseWriter.WriteHeader(statusCode)
}
var cwPool = sync.Pool{New: func() any {
return &customWriter{}
}}
func (s *ExampleServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
cw := cwPool.Get().(*customWriter)
defer cwPool.Put(cw)
*cw = customWriter{w, 0}
start := time.Now()
s.r.ServeHTTP(cw, r)
end := time.Now()
if cw.status == 0 {
cw.status = 200
}
logger := slog.InfoContext
if cw.status >= 400 {
logger = slog.ErrorContext
}
logger(r.Context(),
"incoming",
slog.Int("code", cw.status),
slog.String("method", r.Method),
slog.String("path", r.URL.Path),
slog.Duration("time", end.Sub(start)))
}
func Example() {
mux := httx.NewMux()
mux.OnError = func(w http.ResponseWriter, r *http.Request, err error) {
w.WriteHeader(500)
_, _ = w.Write([]byte(err.Error()))
}
requests := 0
mux.Use(func(next httx.HandlerFunc) httx.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
requests++
return next(w, r)
}
})
mux.GET("/error", func(w http.ResponseWriter, r *http.Request) error {
return errors.New("oops!")
})
mux.GET(`/{id:\d{1,23}}`, func(w http.ResponseWriter, r *http.Request) error {
id, _ := strconv.Atoi(r.PathValue("id")) // Go's 1.22 PathValue-compatible, regex ensures id is a valid int
_, err := someCtxFunc(r.Context(), id)
if err != nil {
return err
}
return json.NewEncoder(w).Encode(requests)
})
httpSrv := &http.Server{
Addr: ":2235",
Handler: &ExampleServer{r: mux},
// some other optional config
}
log.Fatal(httpSrv.ListenAndServe())
}
func someCtxFunc(context.Context, int) (any, error) {
return nil, nil
}