Skip to content
This repository was archived by the owner on Apr 23, 2024. It is now read-only.

Commit 2070bc2

Browse files
committed
fix: keep in-code order of routes
1 parent 43e3501 commit 2070bc2

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

webserver/request_handler.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@ func NewRequestHandler() *RequestHandler {
3232
return app
3333
}
3434

35-
func (a *RequestHandler) Handle(pattern string, handler Handler) {
36-
re := regexp.MustCompile(pattern)
37-
route := Route{Pattern: re, Handler: handler}
38-
39-
a.Routes = append(a.Routes, route)
35+
func (a *RequestHandler) Handle(route *Route) {
36+
a.Routes = append(a.Routes, *route)
4037
}
4138

4239
func (a *RequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

webserver/webserver.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package webserver
22

33
import (
44
"net/http"
5+
"regexp"
56

67
log "github.com/sirupsen/logrus"
78
)
@@ -10,20 +11,20 @@ type Webserver struct {
1011
Port string
1112
ConfigPath string
1213

13-
handlers map[string]Handler
14+
routes []*Route
1415
}
1516

1617
func (w *Webserver) AddHandler(pattern string, handler Handler) {
17-
if w.handlers == nil {
18-
w.handlers = map[string]Handler{}
19-
}
20-
w.handlers[pattern] = handler
18+
w.routes = append(w.routes, &Route{
19+
Pattern: regexp.MustCompile(pattern),
20+
Handler: handler,
21+
})
2122
}
2223

2324
func (w *Webserver) Run() {
2425
app := NewRequestHandler()
25-
for pattern, handler := range w.handlers {
26-
app.Handle(pattern, handler)
26+
for _, route := range w.routes {
27+
app.Handle(route)
2728
}
2829

2930
log.Infof("Serving with config at %s on HTTP port: %s\n", w.ConfigPath, w.Port)

0 commit comments

Comments
 (0)