This repository was archived by the owner on Apr 23, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +10
-12
lines changed Expand file tree Collapse file tree 2 files changed +10
-12
lines changed Original file line number Diff line number Diff line change @@ -32,11 +32,8 @@ func NewRequestHandler() *RequestHandler {
32
32
return app
33
33
}
34
34
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 )
40
37
}
41
38
42
39
func (a * RequestHandler ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package webserver
2
2
3
3
import (
4
4
"net/http"
5
+ "regexp"
5
6
6
7
log "github.com/sirupsen/logrus"
7
8
)
@@ -10,20 +11,20 @@ type Webserver struct {
10
11
Port string
11
12
ConfigPath string
12
13
13
- handlers map [ string ] Handler
14
+ routes [] * Route
14
15
}
15
16
16
17
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
+ })
21
22
}
22
23
23
24
func (w * Webserver ) Run () {
24
25
app := NewRequestHandler ()
25
- for pattern , handler := range w .handlers {
26
- app .Handle (pattern , handler )
26
+ for _ , route := range w .routes {
27
+ app .Handle (route )
27
28
}
28
29
29
30
log .Infof ("Serving with config at %s on HTTP port: %s\n " , w .ConfigPath , w .Port )
You can’t perform that action at this time.
0 commit comments