mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
Bind and render functions
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
9f6f4e8afb
commit
91bbd38251
54
bolt.go
54
bolt.go
@ -8,30 +8,25 @@ import (
|
||||
|
||||
type (
|
||||
Bolt struct {
|
||||
Router *router
|
||||
handlers []HandlerFunc
|
||||
maxParam byte
|
||||
notFoundHandler HandlerFunc
|
||||
methodNotAllowedHandler HandlerFunc
|
||||
pool sync.Pool
|
||||
Router *router
|
||||
handlers []HandlerFunc
|
||||
maxParam byte
|
||||
notFoundHandler HandlerFunc
|
||||
methodNotAllowedHandler HandlerFunc
|
||||
internalServerErrorHandler HandlerFunc
|
||||
pool sync.Pool
|
||||
}
|
||||
HandlerFunc func(*Context)
|
||||
Format byte
|
||||
)
|
||||
|
||||
const (
|
||||
FmtJSON Format = 1 + iota
|
||||
FmtMsgPack
|
||||
)
|
||||
|
||||
const (
|
||||
MIME_JSON = "application/json"
|
||||
MIME_MP = "application/x-msgpack"
|
||||
|
||||
HdrAccept = "Accept"
|
||||
HdrContentDisposition = "Content-Disposition"
|
||||
HdrContentLength = "Content-Length"
|
||||
HdrContentType = "Content-Type"
|
||||
HeaderAccept = "Accept"
|
||||
HeaderContentDisposition = "Content-Disposition"
|
||||
HeaderContentLength = "Content-Length"
|
||||
HeaderContentType = "Content-Type"
|
||||
)
|
||||
|
||||
var MethodMap = map[string]uint8{
|
||||
@ -50,19 +45,22 @@ func New(opts ...func(*Bolt)) (b *Bolt) {
|
||||
b = &Bolt{
|
||||
maxParam: 5,
|
||||
notFoundHandler: func(c *Context) {
|
||||
http.Error(c.Writer, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
http.Error(c.Response, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
},
|
||||
methodNotAllowedHandler: func(c *Context) {
|
||||
http.Error(c.Writer, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
||||
http.Error(c.Response, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
||||
},
|
||||
internalServerErrorHandler: func(c *Context) {
|
||||
http.Error(c.Response, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
},
|
||||
}
|
||||
b.Router = NewRouter(b)
|
||||
b.pool.New = func() interface{} {
|
||||
return &Context{
|
||||
Writer: NewResponse(nil),
|
||||
params: make(Params, b.maxParam),
|
||||
store: make(store),
|
||||
i: -1,
|
||||
Response: &response{},
|
||||
params: make(Params, b.maxParam),
|
||||
store: make(store),
|
||||
i: -1,
|
||||
}
|
||||
}
|
||||
|
||||
@ -92,6 +90,12 @@ func MethodNotAllowedHandler(h HandlerFunc) func(*Bolt) {
|
||||
}
|
||||
}
|
||||
|
||||
func InternalServerErrorHandler(h HandlerFunc) func(*Bolt) {
|
||||
return func(b *Bolt) {
|
||||
b.internalServerErrorHandler = h
|
||||
}
|
||||
}
|
||||
|
||||
// Use adds middleware(s) in the chain.
|
||||
func (b *Bolt) Use(h ...HandlerFunc) {
|
||||
b.handlers = append(b.handlers, h...)
|
||||
@ -148,7 +152,7 @@ func (b *Bolt) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||
// Find and execute handler
|
||||
h, c, s := b.Router.Find(r.Method, r.URL.Path)
|
||||
if h != nil {
|
||||
c.Writer.ResponseWriter = rw
|
||||
c.Response.ResponseWriter = rw
|
||||
c.Request = r
|
||||
h(c)
|
||||
} else {
|
||||
@ -164,3 +168,7 @@ func (b *Bolt) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||
func (b *Bolt) Run(addr string) {
|
||||
log.Fatal(http.ListenAndServe(addr, b))
|
||||
}
|
||||
|
||||
func (b *Bolt) Stop(addr string) {
|
||||
panic("implement it")
|
||||
}
|
||||
|
46
context.go
46
context.go
@ -2,21 +2,19 @@ package bolt
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type (
|
||||
Context struct {
|
||||
Request *http.Request
|
||||
Writer *response
|
||||
Response *http.Response
|
||||
Response *response
|
||||
params Params
|
||||
handlers []HandlerFunc
|
||||
store map[string]interface{}
|
||||
l int // Handlers' length
|
||||
i int // Current handler index
|
||||
bolt *Bolt
|
||||
}
|
||||
store map[string]interface{}
|
||||
)
|
||||
@ -29,31 +27,25 @@ func (c *Context) Param(n string) string {
|
||||
return c.params.Get(n)
|
||||
}
|
||||
|
||||
func (c *Context) Bind(f Format, i interface{}) (err error) {
|
||||
switch f {
|
||||
case FmtJSON:
|
||||
dec := json.NewDecoder(c.Request.Body)
|
||||
if err = dec.Decode(i); err != nil {
|
||||
log.Printf("bolt: %s", err)
|
||||
}
|
||||
//**********
|
||||
// Bind
|
||||
//**********
|
||||
func (c *Context) BindJSON(i interface{}) {
|
||||
dec := json.NewDecoder(c.Request.Body)
|
||||
if err := dec.Decode(i); err != nil {
|
||||
c.bolt.internalServerErrorHandler(c)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: return error, streaming?
|
||||
func (c *Context) Render(n int, f Format, i interface{}) (err error) {
|
||||
var body []byte
|
||||
switch f {
|
||||
case FmtJSON:
|
||||
body, err = json.Marshal(i)
|
||||
//************
|
||||
// Render
|
||||
//************
|
||||
func (c *Context) RenderJSON(n int, i interface{}) {
|
||||
enc := json.NewEncoder(c.Response)
|
||||
c.Response.WriteHeader(n)
|
||||
if err := enc.Encode(i); err != nil {
|
||||
c.bolt.internalServerErrorHandler(c)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("bolt: %s", err)
|
||||
}
|
||||
// c.Writer.Header().Set(HEADER_CONTENT_TYPE, MIME_JSON)
|
||||
c.Writer.WriteHeader(n)
|
||||
_, err = c.Writer.Write(body)
|
||||
return
|
||||
}
|
||||
|
||||
// Next executes the next handler in the chain.
|
||||
@ -71,3 +63,7 @@ func (c *Context) Get(key string) interface{} {
|
||||
func (c *Context) Set(key string, val interface{}) {
|
||||
c.store[key] = val
|
||||
}
|
||||
|
||||
func (c *Context) Redirect(n int, url string) {
|
||||
http.Redirect(c.Response, c.Request, url, n)
|
||||
}
|
||||
|
@ -26,18 +26,16 @@ func createUser(c *bolt.Context) {
|
||||
}
|
||||
|
||||
func getUsers(c *bolt.Context) {
|
||||
c.Render(http.StatusOK, bolt.FMT_JSON, users)
|
||||
c.RenderJSON(http.StatusOK, users)
|
||||
}
|
||||
|
||||
func getUser(c *bolt.Context) {
|
||||
c.Render(http.StatusOK, bolt.FMT_JSON, users[c.P(0)])
|
||||
c.RenderJSON(http.StatusOK, users[c.P(0)])
|
||||
}
|
||||
|
||||
func main() {
|
||||
b := bolt.New()
|
||||
b.Get("/users", getUsers)
|
||||
b.Get("/users/:id", getUser)
|
||||
// go b.RunHttp(":8080")
|
||||
// go b.RunWebSocket(":8081")
|
||||
b.RunTcp(":8082")
|
||||
b.Run(":8080")
|
||||
}
|
||||
|
10
response.go
10
response.go
@ -8,9 +8,6 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
// ResponseWriter interface {
|
||||
// }
|
||||
|
||||
response struct {
|
||||
http.ResponseWriter
|
||||
status int
|
||||
@ -18,13 +15,6 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func NewResponse(rw http.ResponseWriter) *response {
|
||||
return &response{
|
||||
ResponseWriter: rw,
|
||||
status: http.StatusOK,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *response) WriteHeader(c int) {
|
||||
r.status = c
|
||||
r.ResponseWriter.WriteHeader(c)
|
||||
|
Loading…
Reference in New Issue
Block a user