Skip to content

Commit d3e1ef7

Browse files
author
XieBiao
committed
move handler into demo
1 parent d6f4896 commit d3e1ef7

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

examples/main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@ package main
22

33
import (
44
"github.com/hhxsv5/go-db-proxy-api"
5+
"time"
6+
"io"
7+
"net/http"
58
)
69

710
func main() {
811
//user := mydefault.CreateUser("18780207350")
912
//fmt.Println(user)
1013
//fmt.Printf("%p", user)
1114

12-
p := godpa.NewProxy()
15+
var handlers = map[string]func(w http.ResponseWriter, r *http.Request){}
16+
handlers["/"] = func(w http.ResponseWriter, r *http.Request) {
17+
response := "Hello http server by golang!\n"
18+
response += "Now: " + time.Now().String() + "\n"
19+
io.WriteString(w, response)
20+
}
21+
22+
p := godpa.NewProxy(handlers)
1323
p.Run()
1424
}

proxy.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package godpa
33
import (
44
"github.com/hhxsv5/go-db-proxy-api/http"
55
nhttp "net/http"
6-
"time"
7-
"io"
86
)
97

108
type Proxy struct {
@@ -15,14 +13,7 @@ func (p *Proxy) Run() {
1513
p.server.Run()
1614
}
1715

18-
func NewProxy() *Proxy {
19-
var handlers = map[string]func(w nhttp.ResponseWriter, r *nhttp.Request){}
20-
handlers["/"] = func(w nhttp.ResponseWriter, r *nhttp.Request) {
21-
response := "H" +
22-
"ello http server by golang!\n"
23-
response += "Now: " + time.Now().String() + "\n"
24-
io.WriteString(w, response)
25-
}
16+
func NewProxy(handlers map[string]func(w nhttp.ResponseWriter, r *nhttp.Request)) *Proxy {
2617
s := http.NewServer(handlers)
2718
return &Proxy{s}
2819
}

0 commit comments

Comments
 (0)