forked from jianke2008/goapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttptest.go
61 lines (50 loc) · 1.08 KB
/
httptest.go
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
package main
import (
"net/http"
"regexp"
"fmt"
)
type MyHandle func(http.ResponseWriter, *http.Request)
type BaseHttp interface {
filter()
connect()
}
type MyRouter struct {
pattern string
handler MyHandle
}
func (myRouter MyRouter) filter() {
fmt.Println("过滤http请求")
}
func (myRouter MyRouter) connect() {
fmt.Println("http请求")
http.HandleFunc(myRouter.pattern, MyHandle(myRouter.handler))
}
func main(){
var b BaseHttp
b = MyRouter{"/", route}
b.filter()
b.connect()
//http.HandleFunc("/", route)
http.HandleFunc("/api", route)
http.ListenAndServe(":8080", nil)
}
var num = regexp.MustCompile(`\d`)
var str = regexp.MustCompile(`\w`)
func route(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.Path)
switch {
case num.MatchString(r.URL.Path):
digits(w, r)
case str.MatchString(r.URL.Path):
paramStr(w, r)
default:
w.Write([]byte("位置匹配项"))
}
}
func digits(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("receive digits"))
}
func paramStr(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("receive string"))
}