mirror of
https://github.com/labstack/echo.git
synced 2025-07-13 01:30:31 +02:00
54
bolt.go
54
bolt.go
@ -8,30 +8,25 @@ import (
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
Bolt struct {
|
Bolt struct {
|
||||||
Router *router
|
Router *router
|
||||||
handlers []HandlerFunc
|
handlers []HandlerFunc
|
||||||
maxParam byte
|
maxParam byte
|
||||||
notFoundHandler HandlerFunc
|
notFoundHandler HandlerFunc
|
||||||
methodNotAllowedHandler HandlerFunc
|
methodNotAllowedHandler HandlerFunc
|
||||||
pool sync.Pool
|
internalServerErrorHandler HandlerFunc
|
||||||
|
pool sync.Pool
|
||||||
}
|
}
|
||||||
HandlerFunc func(*Context)
|
HandlerFunc func(*Context)
|
||||||
Format byte
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
FmtJSON Format = 1 + iota
|
|
||||||
FmtMsgPack
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MIME_JSON = "application/json"
|
MIME_JSON = "application/json"
|
||||||
MIME_MP = "application/x-msgpack"
|
MIME_MP = "application/x-msgpack"
|
||||||
|
|
||||||
HdrAccept = "Accept"
|
HeaderAccept = "Accept"
|
||||||
HdrContentDisposition = "Content-Disposition"
|
HeaderContentDisposition = "Content-Disposition"
|
||||||
HdrContentLength = "Content-Length"
|
HeaderContentLength = "Content-Length"
|
||||||
HdrContentType = "Content-Type"
|
HeaderContentType = "Content-Type"
|
||||||
)
|
)
|
||||||
|
|
||||||
var MethodMap = map[string]uint8{
|
var MethodMap = map[string]uint8{
|
||||||
@ -50,19 +45,22 @@ func New(opts ...func(*Bolt)) (b *Bolt) {
|
|||||||
b = &Bolt{
|
b = &Bolt{
|
||||||
maxParam: 5,
|
maxParam: 5,
|
||||||
notFoundHandler: func(c *Context) {
|
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) {
|
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.Router = NewRouter(b)
|
||||||
b.pool.New = func() interface{} {
|
b.pool.New = func() interface{} {
|
||||||
return &Context{
|
return &Context{
|
||||||
Writer: NewResponse(nil),
|
Response: &response{},
|
||||||
params: make(Params, b.maxParam),
|
params: make(Params, b.maxParam),
|
||||||
store: make(store),
|
store: make(store),
|
||||||
i: -1,
|
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.
|
// Use adds middleware(s) in the chain.
|
||||||
func (b *Bolt) Use(h ...HandlerFunc) {
|
func (b *Bolt) Use(h ...HandlerFunc) {
|
||||||
b.handlers = append(b.handlers, h...)
|
b.handlers = append(b.handlers, h...)
|
||||||
@ -148,7 +152,7 @@ func (b *Bolt) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
|||||||
// Find and execute handler
|
// Find and execute handler
|
||||||
h, c, s := b.Router.Find(r.Method, r.URL.Path)
|
h, c, s := b.Router.Find(r.Method, r.URL.Path)
|
||||||
if h != nil {
|
if h != nil {
|
||||||
c.Writer.ResponseWriter = rw
|
c.Response.ResponseWriter = rw
|
||||||
c.Request = r
|
c.Request = r
|
||||||
h(c)
|
h(c)
|
||||||
} else {
|
} else {
|
||||||
@ -164,3 +168,7 @@ func (b *Bolt) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
|||||||
func (b *Bolt) Run(addr string) {
|
func (b *Bolt) Run(addr string) {
|
||||||
log.Fatal(http.ListenAndServe(addr, b))
|
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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Context struct {
|
Context struct {
|
||||||
Request *http.Request
|
Request *http.Request
|
||||||
Writer *response
|
Response *response
|
||||||
Response *http.Response
|
|
||||||
params Params
|
params Params
|
||||||
handlers []HandlerFunc
|
handlers []HandlerFunc
|
||||||
store map[string]interface{}
|
store map[string]interface{}
|
||||||
l int // Handlers' length
|
l int // Handlers' length
|
||||||
i int // Current handler index
|
i int // Current handler index
|
||||||
|
bolt *Bolt
|
||||||
}
|
}
|
||||||
store map[string]interface{}
|
store map[string]interface{}
|
||||||
)
|
)
|
||||||
@ -29,31 +27,25 @@ func (c *Context) Param(n string) string {
|
|||||||
return c.params.Get(n)
|
return c.params.Get(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) Bind(f Format, i interface{}) (err error) {
|
//**********
|
||||||
switch f {
|
// Bind
|
||||||
case FmtJSON:
|
//**********
|
||||||
dec := json.NewDecoder(c.Request.Body)
|
func (c *Context) BindJSON(i interface{}) {
|
||||||
if err = dec.Decode(i); err != nil {
|
dec := json.NewDecoder(c.Request.Body)
|
||||||
log.Printf("bolt: %s", err)
|
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) {
|
// Render
|
||||||
var body []byte
|
//************
|
||||||
switch f {
|
func (c *Context) RenderJSON(n int, i interface{}) {
|
||||||
case FmtJSON:
|
enc := json.NewEncoder(c.Response)
|
||||||
body, err = json.Marshal(i)
|
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.
|
// 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{}) {
|
func (c *Context) Set(key string, val interface{}) {
|
||||||
c.store[key] = val
|
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) {
|
func getUsers(c *bolt.Context) {
|
||||||
c.Render(http.StatusOK, bolt.FMT_JSON, users)
|
c.RenderJSON(http.StatusOK, users)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getUser(c *bolt.Context) {
|
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() {
|
func main() {
|
||||||
b := bolt.New()
|
b := bolt.New()
|
||||||
b.Get("/users", getUsers)
|
b.Get("/users", getUsers)
|
||||||
b.Get("/users/:id", getUser)
|
b.Get("/users/:id", getUser)
|
||||||
// go b.RunHttp(":8080")
|
b.Run(":8080")
|
||||||
// go b.RunWebSocket(":8081")
|
|
||||||
b.RunTcp(":8082")
|
|
||||||
}
|
}
|
||||||
|
10
response.go
10
response.go
@ -8,9 +8,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// ResponseWriter interface {
|
|
||||||
// }
|
|
||||||
|
|
||||||
response struct {
|
response struct {
|
||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
status int
|
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) {
|
func (r *response) WriteHeader(c int) {
|
||||||
r.status = c
|
r.status = c
|
||||||
r.ResponseWriter.WriteHeader(c)
|
r.ResponseWriter.WriteHeader(c)
|
||||||
|
@ -255,7 +255,7 @@ func lcp(a, b string) (i int) {
|
|||||||
|
|
||||||
func (r *router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
func (r *router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||||
h, c, rep := r.Find(req.Method, req.URL.Path)
|
h, c, rep := r.Find(req.Method, req.URL.Path)
|
||||||
c.Writer.ResponseWriter = rw
|
c.Response.ResponseWriter = rw
|
||||||
if h != nil {
|
if h != nil {
|
||||||
h(c)
|
h(c)
|
||||||
} else {
|
} else {
|
||||||
|
Reference in New Issue
Block a user