-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.go
More file actions
215 lines (189 loc) · 6.41 KB
/
router.go
File metadata and controls
215 lines (189 loc) · 6.41 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Package mux is a small, idomatic http router
package mux
import (
"errors"
"io/fs"
"log/slog"
"net/http"
"slices"
"strings"
"time"
)
// Middleware defines a function that wraps an http.Handler.
type Middleware func(http.Handler) http.Handler
// Router provides a chain of middlewares and routes.
type Router struct {
*http.ServeMux
chain http.Handler
methods []string
notFound func(http.ResponseWriter, *http.Request)
notAllowed func(http.ResponseWriter, string, int)
}
// defaultRouter creates a new Router using the default ServeMux.
func defaultRouter() *Router {
mux := http.NewServeMux()
router := &Router{
ServeMux: mux,
chain: mux,
methods: []string{},
notFound: http.NotFound,
notAllowed: http.Error,
}
// set up
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
method := r.Method
_, current := mux.Handler(r)
var allowed []string
for _, method := range router.methods {
// If we find a pattern that's different from the pattern for the
// current fallback handler then we know there are actually other handlers
// that could match with a method change, so we should handle as
// method not allowed
r.Method = method
if _, pattern := mux.Handler(r); pattern != current {
allowed = append(allowed, method)
}
}
r.Method = method
if len(allowed) != 0 {
w.Header().Set("Allow", strings.Join(allowed, ", "))
router.notAllowed(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
// http.Error(w, "Custom Not Found", http.StatusNotFound)
router.notFound(w, r)
})
return router
}
// NewRouter creates a new Router with the given middleware applied.
func NewRouter(middleware ...Middleware) *Router {
router := defaultRouter()
router.Use(middleware...)
return router
}
// NotFound sets a custome not found handler.
func (router *Router) NotFound(h func(http.ResponseWriter, *http.Request)) *Router {
router.notFound = http.HandlerFunc(h)
return router
}
// NotAllowed sets a custom method not allowed error.
func (router *Router) NotAllowed(h func(http.ResponseWriter, string, int)) *Router {
router.notAllowed = h
return router
}
// Group creates a sub-router for the given prefix and applies middleware to it.
func (router *Router) Group(prefix string, middlewares ...Middleware) *Router {
for _, m := range middlewares {
if m == nil {
panic("Router.Group: middleware cannot be nil")
}
}
subRouter := defaultRouter()
subRouter.notFound = router.notFound
subRouter.notAllowed = router.notAllowed
subRouter.Use(middlewares...)
router.Handle(prefix+"/", http.StripPrefix(prefix, subRouter))
return subRouter
}
// Use adds a chain of middlewares to the router.
func (router *Router) Use(middlewares ...Middleware) {
for _, m := range middlewares {
router.chain = m(router.chain)
}
}
// All registers the handler for all methods on given pattern.
func (router *Router) All(pattern string, handler http.HandlerFunc) {
methods := allMethods()
for _, method := range methods {
router.addMethod(method)
}
router.HandleFunc(pattern, handler)
}
// Post registers the handler for post requests on given pattern.
func (router *Router) Post(pattern string, handler http.HandlerFunc) {
router.addMethod(http.MethodPost)
router.HandleFunc(http.MethodPost+"\t"+pattern, handler)
}
// Get registers the handler for get requests on given pattern.
func (router *Router) Get(pattern string, handler http.HandlerFunc) {
router.addMethod(http.MethodGet)
router.addMethod(http.MethodHead)
router.HandleFunc(http.MethodGet+"\t"+pattern, handler)
}
// Delete registers the handler for delete requests on given pattern.
func (router *Router) Delete(pattern string, handler http.HandlerFunc) {
router.addMethod(http.MethodDelete)
router.HandleFunc(http.MethodDelete+"\t"+pattern, handler)
}
// Put registers the handler for Put requests on given pattern.
func (router *Router) Put(pattern string, handler http.HandlerFunc) {
router.addMethod(http.MethodPut)
router.HandleFunc(http.MethodPut+"\t"+pattern, handler)
}
// Patch registers the handler for patch requests on given pattern.
func (router *Router) Patch(pattern string, handler http.HandlerFunc) {
router.addMethod(http.MethodPatch)
router.HandleFunc(http.MethodPatch+"\t"+pattern, handler)
}
// CustomMethod registers a handler for a custom method request.
func (router *Router) CustomMethod(method, pattern string, handle http.HandlerFunc) {
router.addMethod(method)
router.HandleFunc(method+"\t"+pattern, handle)
}
// ServeHTTP implements the http.Handler interface.
func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
router.chain.ServeHTTP(w, r)
}
// Static registers the handle to serve static files.
func (router *Router) Static(pattern, dir string) {
if !strings.HasSuffix(pattern, "/") {
pattern += "/"
}
router.Handle(pattern, http.StripPrefix(pattern, http.FileServer(http.Dir(dir))))
}
// StaticFS registers the handle to serve static files from FS filesystem.
// ex.
// //go:embded images
// var content embed.FS
// router.StaticFS("/images/", content) .
func (router *Router) StaticFS(pattern string, fs fs.FS) {
if !strings.HasSuffix(pattern, "/") {
pattern += "/"
}
router.Handle(pattern, http.StripPrefix(pattern, http.FileServer(http.FS(fs))))
}
// ServeFile registers a ServeFile handler.
func (router *Router) ServeFile(pattern, file string) {
router.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, file)
})
}
// ServeFileFS registers a ServeFileFS handler.
func (router *Router) ServeFileFS(pattern, file string, fs fs.FS) {
router.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
http.ServeFileFS(w, r, fs, file)
})
}
// Run starts the HTTP server and logs any error that occurs.
func (router *Router) Run(addr string) {
server := http.Server{
Addr: addr,
ReadHeaderTimeout: time.Second,
Handler: router,
}
slog.Info("Starting server:", "Address", addr)
if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
slog.Error("Router.Run: failed to start server: ", "error", err)
}
}
func (router *Router) addMethod(method string) {
if !slices.Contains(router.methods, method) {
router.methods = append(router.methods, method)
}
}
func allMethods() []string {
return []string{
http.MethodGet, http.MethodHead, http.MethodPost, http.MethodPut, http.MethodPatch,
http.MethodDelete, http.MethodConnect, http.MethodOptions, http.MethodTrace,
}
}