/
server.go
62 lines (53 loc) · 1.42 KB
/
server.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
package main
import (
"fmt"
"net/http"
"time"
"golang.org/x/net/context"
"github.com/AlexanderChen1989/xrest"
"github.com/AlexanderChen1989/xrest/plugs/body"
"github.com/AlexanderChen1989/xrest/plugs/close"
"github.com/AlexanderChen1989/xrest/plugs/limit"
"github.com/AlexanderChen1989/xrest/plugs/router"
"github.com/AlexanderChen1989/xrest/plugs/static"
"github.com/AlexanderChen1989/xrest/utils"
)
func helloRoute(ctx context.Context, w http.ResponseWriter, r *http.Request) {
params, _ := router.FetchParams(ctx)
utils.DumpJSON(
w,
map[string]interface{}{
"status": "success",
"msg": "Hello, " + params.ByName("name") + "!",
},
)
}
func slowRoute(ctx context.Context, w http.ResponseWriter, r *http.Request) {
time.Sleep(10 * time.Second)
utils.DumpJSON(w, `Im slow!`)
}
func newRouter() *router.Router {
r := router.New()
r.Get("/api/hello/:name", xrest.HandlerFunc(helloRoute))
r.Post("/api/hello/:name", xrest.HandlerFunc(helloRoute))
r.Get("/api/slow", xrest.HandlerFunc(slowRoute))
return r
}
func main() {
p := xrest.NewPipeline()
p.Plug(limit.New(1, time.Second))
p.Plug(close.New(func(r *http.Request) {
fmt.Printf("CLOSED: %#v\n", r.RemoteAddr)
}))
p.Plug(
static.New(
static.Dir("./static"),
static.Prefix("public"),
),
)
p.Plug(body.New(func(r *http.Request, err error) {
fmt.Println("Error: ", err)
}))
p.Plug(newRouter())
http.ListenAndServe(":8080", p.HTTPHandler())
}