-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
88 lines (70 loc) · 1.79 KB
/
main.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
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
package main
import (
"flag"
"fmt"
"net/http"
"time"
"dearcode.net/crab/http/client"
"dearcode.net/crab/http/server"
)
type regexpTest struct {
}
func (rt *regexpTest) GET(w http.ResponseWriter, req *http.Request) {
user, ok := server.RESTValue(req, "user")
if !ok {
panic("context `user` not found")
}
id, ok := server.RESTValue(req, "id")
if !ok {
panic("context `user` not found")
}
_, ok = server.RESTValue(req, "idx")
if ok {
panic("find invalid key idx")
}
fmt.Fprintf(w, "user:%v, id:%v", user, id)
}
type index struct {
r *http.Request
}
func (i *index) GET(w http.ResponseWriter, req *http.Request) {
i.r = req
w.Write([]byte(fmt.Sprintf("client:%v addr:%p", i.r.RemoteAddr, i)))
}
func testRESTClient() {
url := "http://127.0.0.1:9000/regexp/mailchina/test/"
//url := fmt.Sprintf("http://127.0.0.1:9000/regexp/mailchina/test/u1%v", time.Now().UnixNano())
buf, err := client.New().Get(url, nil, nil)
if err != nil {
panic(err.Error())
}
fmt.Printf("response:%s\n", buf)
}
func testHTTPClient() {
url := fmt.Sprintf("http://127.0.0.1:9000/main/index/?id=111&tm=%v", time.Now().UnixNano())
buf, err := client.New().Get(url, nil, nil)
if err != nil {
panic(err.Error())
}
fmt.Printf("response:%s\n", buf)
}
func testHandler(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "testHandler:%v", req.RemoteAddr)
}
func main() {
addr := flag.String("h", ":9000", "api listen address")
flag.Parse()
server.Register(&index{})
server.RegisterPrefix(®expTest{}, "/regexp/{user}/test/{id}")
server.RegisterHandler(testHandler, "GET", "/testHandler/")
ln, err := server.Start(*addr)
if err != nil {
panic(err.Error())
}
fmt.Printf("listen:%v\n", ln.Addr())
for i := 0; i < 5; i++ {
time.Sleep(time.Second)
testHTTPClient()
testRESTClient()
}
}