Skip to content

Commit 5e4ee53

Browse files
committed
修改http client结构
1 parent 0b1d865 commit 5e4ee53

File tree

3 files changed

+56
-28
lines changed

3 files changed

+56
-28
lines changed

http/client/client.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ import (
1111
"github.com/zssky/log"
1212
)
1313

14-
//Client 对http client简单封装.
15-
type Client struct {
14+
type httpClient struct {
1615
hc http.Client
1716
}
1817

1918
//NewClient 创建一个带超时控制的http client.
20-
func NewClient(timeout time.Duration) Client {
21-
return Client{
19+
func New(timeout time.Duration) httpClient {
20+
return httpClient{
2221
hc: http.Client{
2322
Transport: &http.Transport{
2423
Dial: func(netw, addr string) (net.Conn, error) {
@@ -39,7 +38,7 @@ func NewClient(timeout time.Duration) Client {
3938
}
4039
}
4140

42-
func (c Client) do(method, url string, headers map[string]string, body *bytes.Buffer) ([]byte, int, error) {
41+
func (c httpClient) do(method, url string, headers map[string]string, body *bytes.Buffer) ([]byte, int, error) {
4342
var req *http.Request
4443
var err error
4544

@@ -72,22 +71,18 @@ func (c Client) do(method, url string, headers map[string]string, body *bytes.Bu
7271
return data, resp.StatusCode, nil
7372
}
7473

75-
//Get get 请求...
76-
func (c Client) Get(url string, headers map[string]string, body *bytes.Buffer) ([]byte, int, error) {
74+
func (c httpClient) Get(url string, headers map[string]string, body *bytes.Buffer) ([]byte, int, error) {
7775
return c.do("GET", url, headers, body)
7876
}
7977

80-
//POST post 请求.
81-
func (c Client) POST(url string, headers map[string]string, body *bytes.Buffer) ([]byte, int, error) {
78+
func (c httpClient) POST(url string, headers map[string]string, body *bytes.Buffer) ([]byte, int, error) {
8279
return c.do("POST", url, headers, body)
8380
}
8481

85-
//PUT put 请求.
86-
func (c Client) PUT(url string, headers map[string]string, body *bytes.Buffer) ([]byte, int, error) {
82+
func (c httpClient) PUT(url string, headers map[string]string, body *bytes.Buffer) ([]byte, int, error) {
8783
return c.do("PUT", url, headers, body)
8884
}
8985

90-
//DELETE delete 请求.
91-
func (c Client) DELETE(url string, headers map[string]string, body *bytes.Buffer) ([]byte, int, error) {
86+
func (c httpClient) DELETE(url string, headers map[string]string, body *bytes.Buffer) ([]byte, int, error) {
9287
return c.do("DELETE", url, headers, body)
9388
}

http/server/handler.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"sync"
1111

1212
"github.com/google/btree"
13+
"github.com/juju/errors"
1314
"github.com/zssky/log"
1415
)
1516

@@ -24,9 +25,10 @@ type iface struct {
2425
}
2526

2627
type httpServer struct {
27-
path map[string]iface
28-
prefix *btree.BTree
29-
filter Filter
28+
path map[string]iface
29+
prefix *btree.BTree
30+
filter Filter
31+
listener net.Listener
3032
sync.RWMutex
3133
}
3234

@@ -84,14 +86,14 @@ func AddInterface(obj interface{}, path string, isPrefix bool) error {
8486
rv := reflect.ValueOf(obj)
8587
for i := 0; i < rv.NumMethod(); i++ {
8688
method := rt.Method(i).Name
87-
log.Debugf("rt:%v, %d, method:%v", rt, i, method)
89+
//log.Debugf("rt:%v, %d, method:%v", rt, i, method)
8890
switch method {
8991
case POST.String():
9092
case GET.String():
9193
case PUT.String():
9294
case DELETE.String():
9395
default:
94-
log.Debugf("ignore method:%v path:%v", method, path)
96+
log.Warnf("ignore %v %v %v", method, path, rt)
9597
continue
9698
}
9799

@@ -106,7 +108,7 @@ func AddInterface(obj interface{}, path string, isPrefix bool) error {
106108
panic(fmt.Sprintf("exist url:%v %v", method, path))
107109
}
108110
server.prefix.ReplaceOrInsert(&ifc)
109-
log.Debugf("add prefix:%v", path)
111+
log.Infof("add prefix %v %v %v", method, path, rt)
110112
continue
111113
}
112114

@@ -116,6 +118,7 @@ func AddInterface(obj interface{}, path string, isPrefix bool) error {
116118
}
117119

118120
server.path[ifc.path] = ifc
121+
log.Infof("add path %v %v %v", method, path, rt)
119122
}
120123

121124
return nil
@@ -187,6 +190,23 @@ func defaultFilter(_ http.ResponseWriter, r *http.Request) *http.Request {
187190
}
188191

189192
//Start 启动httpServer.
190-
func Start(l net.Listener) error {
191-
return http.Serve(l, server)
193+
func Start(addr string) error {
194+
ln, err := net.Listen("tcp", addr)
195+
if err != nil {
196+
return errors.Trace(err)
197+
}
198+
199+
server.Lock()
200+
server.listener = ln
201+
server.Unlock()
202+
203+
return http.Serve(ln, server)
204+
}
205+
206+
//Stop 停止httpServer监听, 进行中的任务并不会因此而停止.
207+
func Stop() error {
208+
server.Lock()
209+
defer server.Unlock()
210+
211+
return server.listener.Close()
192212
}

main.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package main
33
import (
44
"flag"
55
"fmt"
6-
"net"
76
"net/http"
7+
"time"
88

9+
"github.com/dearcode/crab/http/client"
910
"github.com/dearcode/crab/http/server"
1011
_ "github.com/dearcode/crab/server"
1112
)
@@ -19,17 +20,29 @@ func (i *index) GET(w http.ResponseWriter, req *http.Request) {
1920
w.Write([]byte(fmt.Sprintf("client:%v addr:%p", i.r.RemoteAddr, i)))
2021
}
2122

22-
func main() {
23-
addr := flag.String("h", ":9000", "api listen address")
24-
flag.Parse()
25-
26-
ln, err := net.Listen("tcp", *addr)
23+
func testHTTPClient() {
24+
url := "http://127.0.0.1:9000/index"
25+
buf, _, err := client.New(time.Second).Get(url, nil, nil)
2726
if err != nil {
2827
panic(err.Error())
2928
}
29+
fmt.Printf("response:%s\n", buf)
30+
}
31+
32+
func main() {
33+
addr := flag.String("h", ":9000", "api listen address")
34+
flag.Parse()
3035

3136
server.AddInterface(&index{}, "/index", false)
32-
if err = server.Start(ln); err != nil {
37+
38+
go func() {
39+
for i := 0; i < 5; i++ {
40+
time.Sleep(time.Second)
41+
testHTTPClient()
42+
}
43+
}()
44+
45+
if err := server.Start(*addr); err != nil {
3346
panic(err.Error())
3447
}
3548
}

0 commit comments

Comments
 (0)