forked from Fuyu/router
Add Renderer
This commit is contained in:
parent
78000acf6c
commit
ae68248d25
14
context.go
14
context.go
@ -9,14 +9,15 @@ import (
|
|||||||
|
|
||||||
// Context is passed to handlers and middlewares
|
// Context is passed to handlers and middlewares
|
||||||
type Context struct {
|
type Context struct {
|
||||||
|
router *Router
|
||||||
Request *http.Request
|
Request *http.Request
|
||||||
Response http.ResponseWriter
|
Response http.ResponseWriter
|
||||||
Param func(string) string
|
Param func(string) string
|
||||||
store map[string]interface{}
|
store map[string]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newContext(res http.ResponseWriter, req *http.Request, param httprouter.Params) *Context {
|
func newContext(router *Router, res http.ResponseWriter, req *http.Request, param httprouter.Params) *Context {
|
||||||
return &Context{req, res, param.ByName, make(map[string]interface{})}
|
return &Context{router, req, res, param.ByName, make(map[string]interface{})}
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns the given status code and writes the bytes to the body
|
// String returns the given status code and writes the bytes to the body
|
||||||
@ -45,6 +46,15 @@ func (c *Context) JSON(code int, data interface{}) error {
|
|||||||
return json.NewEncoder(c.Response).Encode(data) // TODO: Encode to buffer first to prevent partial responses on error
|
return json.NewEncoder(c.Response).Encode(data) // TODO: Encode to buffer first to prevent partial responses on error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Context) Render(code int, template string, data interface{}) error {
|
||||||
|
if c.router.Renderer == nil {
|
||||||
|
panic(`Cannot call render without a renderer set`)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Response.WriteHeader(code)
|
||||||
|
return c.router.Renderer.Render(c.Response, template, data, c)
|
||||||
|
}
|
||||||
|
|
||||||
// Set sets a value in the context. Set is not safe to be used concurrently
|
// Set sets a value in the context. Set is not safe to be used concurrently
|
||||||
func (c *Context) Set(key string, value interface{}) {
|
func (c *Context) Set(key string, value interface{}) {
|
||||||
c.store[key] = value
|
c.store[key] = value
|
||||||
|
16
router.go
16
router.go
@ -20,8 +20,8 @@ type GetHandle func(*Context) error
|
|||||||
|
|
||||||
// Router is the router itself
|
// Router is the router itself
|
||||||
type Router struct {
|
type Router struct {
|
||||||
routes []route
|
routes []route
|
||||||
Port int
|
Renderer Renderer
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new Router
|
// New returns a new Router
|
||||||
@ -83,11 +83,11 @@ func (r *Router) getHttpr() *httprouter.Router {
|
|||||||
|
|
||||||
for _, v := range r.routes {
|
for _, v := range r.routes {
|
||||||
if handle, ok := v.Handle.(GetHandle); ok {
|
if handle, ok := v.Handle.(GetHandle); ok {
|
||||||
httpr.Handle(v.Method, v.Path, handleGET(handle))
|
httpr.Handle(v.Method, v.Path, handleGET(r, handle))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
httpr.Handle(v.Method, v.Path, handlePOST(v.Handle))
|
httpr.Handle(v.Method, v.Path, handlePOST(r, v.Handle))
|
||||||
}
|
}
|
||||||
|
|
||||||
return httpr
|
return httpr
|
||||||
@ -119,11 +119,11 @@ func checkInterfaceHandle(f interface{}) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlePOST(f interface{}) httprouter.Handle {
|
func handlePOST(r *Router, f interface{}) httprouter.Handle {
|
||||||
funcRv, inputRt := reflect.ValueOf(f), reflect.TypeOf(f).In(1)
|
funcRv, inputRt := reflect.ValueOf(f), reflect.TypeOf(f).In(1)
|
||||||
|
|
||||||
return func(res http.ResponseWriter, req *http.Request, param httprouter.Params) {
|
return func(res http.ResponseWriter, req *http.Request, param httprouter.Params) {
|
||||||
c := newContext(res, req, param)
|
c := newContext(r, res, req, param)
|
||||||
|
|
||||||
data := reflect.New(inputRt)
|
data := reflect.New(inputRt)
|
||||||
{
|
{
|
||||||
@ -141,9 +141,9 @@ func handlePOST(f interface{}) httprouter.Handle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleGET(f GetHandle) httprouter.Handle {
|
func handleGET(r *Router, f GetHandle) httprouter.Handle {
|
||||||
return func(res http.ResponseWriter, req *http.Request, param httprouter.Params) {
|
return func(res http.ResponseWriter, req *http.Request, param httprouter.Params) {
|
||||||
c := newContext(res, req, param)
|
c := newContext(r, res, req, param)
|
||||||
|
|
||||||
err := f(c)
|
err := f(c)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user