/
gogo.go
73 lines (58 loc) · 1.47 KB
/
gogo.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
package gogo
import (
"context"
"net/http"
"github.com/pilwon/gogo/gogocontext"
"github.com/pilwon/gogo/middleware"
"github.com/pilwon/gogo/router"
)
type Config map[string]interface{}
var (
registeredRouter router.Router
)
func New() *Server {
return newServer(context.Background(), Config{})
}
func NewWithConfig(config Config) *Server {
return newServer(context.Background(), config)
}
func NewWithContext(c context.Context) *Server {
return newServer(c, Config{})
}
func NewWithContextConfig(c context.Context, config Config) *Server {
return newServer(c, config)
}
func Next(c context.Context, w http.ResponseWriter, r *http.Request) context.Context {
next, ok := nextMiddlewareFromContext(c)
if !ok {
http.NotFound(w, r)
} else if next != nil {
return next(c, w, r)
}
return c
}
func Params(c context.Context) gogocontext.Params {
return gogocontext.ParamsFromContext(c)
}
func Param(c context.Context, key string) string {
val, ok := gogocontext.ParamsFromContext(c)[key]
if !ok {
return ""
}
return val
}
func RegisterRouter(r router.Router) {
if registeredRouter != nil {
panic("Router already registered.")
}
registeredRouter = r
}
func MiddlewareFromHandler(h http.Handler) middleware.Handler {
return middleware.WrapHandler(h)
}
func MiddlewareFromRouteHandler(h router.Handler) middleware.Handler {
return router.MiddlewareFromRouterHandler(h)
}
func Wrap(h http.Handler) router.HandlerFunc {
return router.WrapHandler(h).(router.HandlerFunc)
}