-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathroute.go
More file actions
48 lines (33 loc) · 778 Bytes
/
route.go
File metadata and controls
48 lines (33 loc) · 778 Bytes
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
package main
import (
"io"
"net/http"
"regexp"
"time"
)
type WebController struct {
Function func(http.ResponseWriter, *http.Request)
Method string
Pattern string
}
var mux []WebController
func init() {
mux = append(mux, WebController{post, "POST", "^/"})
mux = append(mux, WebController{get, "GET", "^/"})
}
type httpHandler struct{}
func (*httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t := time.Now()
for _, webController := range mux {
if m, _ := regexp.MatchString(webController.Pattern, r.URL.Path); m {
if r.Method == webController.Method {
webController.Function(w, r)
go writeLog(r, t, "match", webController.Pattern)
return
}
}
}
go writeLog(r, t, "unmatch", "")
io.WriteString(w, "")
return
}