1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-12-09 10:24:14 -08:00
parent f0b235c108
commit 133f7acf21
6 changed files with 89 additions and 74 deletions

View File

@ -14,7 +14,7 @@ import (
type ( type (
// Binder is the interface that wraps the Bind method. // Binder is the interface that wraps the Bind method.
Binder interface { Binder interface {
Bind(interface{}, Context) error Bind(i interface{}, c Context) error
} }
binder struct{} binder struct{}

View File

@ -23,7 +23,7 @@ type (
Request() *http.Request Request() *http.Request
// SetRequest sets `*http.Request`. // SetRequest sets `*http.Request`.
SetRequest(*http.Request) SetRequest(r *http.Request)
// Request returns `*Response`. // Request returns `*Response`.
Response() *Response Response() *Response
@ -42,25 +42,25 @@ type (
Path() string Path() string
// SetPath sets the registered path for the handler. // SetPath sets the registered path for the handler.
SetPath(string) SetPath(p string)
// Param returns path parameter by name. // Param returns path parameter by name.
Param(string) string Param(name string) string
// ParamNames returns path parameter names. // ParamNames returns path parameter names.
ParamNames() []string ParamNames() []string
// SetParamNames sets path parameter names. // SetParamNames sets path parameter names.
SetParamNames(...string) SetParamNames(names ...string)
// ParamValues returns path parameter values. // ParamValues returns path parameter values.
ParamValues() []string ParamValues() []string
// SetParamValues sets path parameter values. // SetParamValues sets path parameter values.
SetParamValues(...string) SetParamValues(values ...string)
// QueryParam returns the query param for the provided name. // QueryParam returns the query param for the provided name.
QueryParam(string) string QueryParam(name string) string
// QueryParams returns the query parameters as `url.Values`. // QueryParams returns the query parameters as `url.Values`.
QueryParams() url.Values QueryParams() url.Values
@ -69,90 +69,96 @@ type (
QueryString() string QueryString() string
// FormValue returns the form field value for the provided name. // FormValue returns the form field value for the provided name.
FormValue(string) string FormValue(name string) string
// FormParams returns the form parameters as `url.Values`. // FormParams returns the form parameters as `url.Values`.
FormParams() (url.Values, error) FormParams() (url.Values, error)
// FormFile returns the multipart form file for the provided name. // FormFile returns the multipart form file for the provided name.
FormFile(string) (*multipart.FileHeader, error) FormFile(name string) (*multipart.FileHeader, error)
// MultipartForm returns the multipart form. // MultipartForm returns the multipart form.
MultipartForm() (*multipart.Form, error) MultipartForm() (*multipart.Form, error)
// Cookie returns the named cookie provided in the request. // Cookie returns the named cookie provided in the request.
Cookie(string) (*http.Cookie, error) Cookie(name string) (*http.Cookie, error)
// SetCookie adds a `Set-Cookie` header in HTTP response. // SetCookie adds a `Set-Cookie` header in HTTP response.
SetCookie(*http.Cookie) SetCookie(cookie *http.Cookie)
// Cookies returns the HTTP cookies sent with the request. // Cookies returns the HTTP cookies sent with the request.
Cookies() []*http.Cookie Cookies() []*http.Cookie
// Get retrieves data from the context. // Get retrieves data from the context.
Get(string) interface{} Get(key string) interface{}
// Set saves data in the context. // Set saves data in the context.
Set(string, interface{}) Set(key string, val interface{})
// Bind binds the request body into provided type `i`. The default binder // Bind binds the request body into provided type `i`. The default binder
// does it based on Content-Type header. // does it based on Content-Type header.
Bind(interface{}) error Bind(i interface{}) error
// Render renders a template with data and sends a text/html response with status // Render renders a template with data and sends a text/html response with status
// code. Templates can be registered using `Echo.Renderer`. // code. Templates can be registered using `Echo.Renderer`.
Render(int, string, interface{}) error Render(code int, name string, data interface{}) error
// HTML sends an HTTP response with status code. // HTML sends an HTTP response with status code.
HTML(int, string) error HTML(code int, html string) error
// HTMLBlob sends an HTTP blob response with status code. // HTMLBlob sends an HTTP blob response with status code.
HTMLBlob(int, []byte) error HTMLBlob(code int, b []byte) error
// String sends a string response with status code. // String sends a string response with status code.
String(int, string) error String(code int, s string) error
// JSON sends a JSON response with status code. // JSON sends a JSON response with status code.
JSON(int, interface{}) error JSON(code int, i interface{}) error
// JSONPretty sends a pretty-print JSON with status code.
JSONPretty(code int, i interface{}, indent string) error
// JSONBlob sends a JSON blob response with status code. // JSONBlob sends a JSON blob response with status code.
JSONBlob(int, []byte) error JSONBlob(code int, b []byte) error
// JSONP sends a JSONP response with status code. It uses `callback` to construct // JSONP sends a JSONP response with status code. It uses `callback` to construct
// the JSONP payload. // the JSONP payload.
JSONP(int, string, interface{}) error JSONP(code int, callback string, i interface{}) error
// JSONPBlob sends a JSONP blob response with status code. It uses `callback` // JSONPBlob sends a JSONP blob response with status code. It uses `callback`
// to construct the JSONP payload. // to construct the JSONP payload.
JSONPBlob(int, string, []byte) error JSONPBlob(code int, callback string, b []byte) error
// XML sends an XML response with status code. // XML sends an XML response with status code.
XML(int, interface{}) error XML(code int, i interface{}) error
// XMLPretty sends a pretty-print XML with status code.
XMLPretty(code int, i interface{}, indent string) error
// XMLBlob sends a XML blob response with status code. // XMLBlob sends a XML blob response with status code.
XMLBlob(int, []byte) error XMLBlob(code int, b []byte) error
// Blob sends a blob response with status code and content type. // Blob sends a blob response with status code and content type.
Blob(int, string, []byte) error Blob(code int, contentType string, b []byte) error
// Stream sends a streaming response with status code and content type. // Stream sends a streaming response with status code and content type.
Stream(int, string, io.Reader) error Stream(code int, contentType string, r io.Reader) error
// File sends a response with the content of the file. // File sends a response with the content of the file.
File(string) error File(file string) error
// Attachment sends a response as attachment, prompting client to save the // Attachment sends a response as attachment, prompting client to save the
// file. // file.
Attachment(string, string) error Attachment(file string, name string) error
// Inline sends a response as inline, opening the file in the browser. // Inline sends a response as inline, opening the file in the browser.
Inline(string, string) error Inline(file string, name string) error
// NoContent sends a response with no body and a status code. // NoContent sends a response with no body and a status code.
NoContent(int) error NoContent(code int) error
// Redirect redirects the request with status code. // Redirect redirects the request with status code.
Redirect(int, string) error Redirect(code int, url string) error
// Error invokes the registered HTTP error handler. Generally used by middleware. // Error invokes the registered HTTP error handler. Generally used by middleware.
Error(err error) Error(err error)
@ -161,7 +167,7 @@ type (
Handler() HandlerFunc Handler() HandlerFunc
// SetHandler sets the matched handler by router. // SetHandler sets the matched handler by router.
SetHandler(HandlerFunc) SetHandler(h HandlerFunc)
// Logger returns the `Logger` instance. // Logger returns the `Logger` instance.
Logger() Logger Logger() Logger
@ -172,7 +178,7 @@ type (
// Reset resets the context after request completes. It must be called along // Reset resets the context after request completes. It must be called along
// with `Echo#AcquireContext()` and `Echo#ReleaseContext()`. // with `Echo#AcquireContext()` and `Echo#ReleaseContext()`.
// See `Echo#ServeHTTP()` // See `Echo#ServeHTTP()`
Reset(*http.Request, http.ResponseWriter) Reset(r *http.Request, w http.ResponseWriter)
} }
context struct { context struct {
@ -329,6 +335,10 @@ func (c *context) Cookies() []*http.Cookie {
return c.request.Cookies() return c.request.Cookies()
} }
func (c *context) Get(key string) interface{} {
return c.store[key]
}
func (c *context) Set(key string, val interface{}) { func (c *context) Set(key string, val interface{}) {
if c.store == nil { if c.store == nil {
c.store = make(Map) c.store = make(Map)
@ -336,10 +346,6 @@ func (c *context) Set(key string, val interface{}) {
c.store[key] = val c.store[key] = val
} }
func (c *context) Get(key string) interface{} {
return c.store[key]
}
func (c *context) Bind(i interface{}) error { func (c *context) Bind(i interface{}) error {
return c.echo.Binder.Bind(i, c) return c.echo.Binder.Bind(i, c)
} }
@ -369,11 +375,16 @@ func (c *context) String(code int, s string) (err error) {
func (c *context) JSON(code int, i interface{}) (err error) { func (c *context) JSON(code int, i interface{}) (err error) {
b, err := json.Marshal(i) b, err := json.Marshal(i)
if c.echo.Debug {
b, err = json.MarshalIndent(i, "", " ")
}
if err != nil { if err != nil {
return err return
}
return c.JSONBlob(code, b)
}
func (c *context) JSONPretty(code int, i interface{}, indent string) (err error) {
b, err := json.MarshalIndent(i, "", indent)
if err != nil {
return
} }
return c.JSONBlob(code, b) return c.JSONBlob(code, b)
} }
@ -385,7 +396,7 @@ func (c *context) JSONBlob(code int, b []byte) (err error) {
func (c *context) JSONP(code int, callback string, i interface{}) (err error) { func (c *context) JSONP(code int, callback string, i interface{}) (err error) {
b, err := json.Marshal(i) b, err := json.Marshal(i)
if err != nil { if err != nil {
return err return
} }
return c.JSONPBlob(code, callback, b) return c.JSONPBlob(code, callback, b)
} }
@ -405,15 +416,20 @@ func (c *context) JSONPBlob(code int, callback string, b []byte) (err error) {
func (c *context) XML(code int, i interface{}) (err error) { func (c *context) XML(code int, i interface{}) (err error) {
b, err := xml.Marshal(i) b, err := xml.Marshal(i)
if c.echo.Debug {
b, err = xml.MarshalIndent(i, "", " ")
}
if err != nil { if err != nil {
return err return
} }
return c.XMLBlob(code, b) return c.XMLBlob(code, b)
} }
func (c *context) XMLPretty(code int, i interface{}, indent string) (err error) {
b, err := xml.MarshalIndent(i, "", indent)
if err != nil {
return
}
return c.JSONBlob(code, b)
}
func (c *context) XMLBlob(code int, b []byte) (err error) { func (c *context) XMLBlob(code int, b []byte) (err error) {
c.response.Header().Set(HeaderContentType, MIMEApplicationXMLCharsetUTF8) c.response.Header().Set(HeaderContentType, MIMEApplicationXMLCharsetUTF8)
c.response.WriteHeader(code) c.response.WriteHeader(code)

View File

@ -10,31 +10,31 @@ type (
// Logger defines the logging interface. // Logger defines the logging interface.
Logger interface { Logger interface {
Output() io.Writer Output() io.Writer
SetOutput(io.Writer) SetOutput(w io.Writer)
Level() log.Lvl
SetLevel(log.Lvl)
Prefix() string Prefix() string
SetPrefix(string) SetPrefix(p string)
Print(...interface{}) Level() log.Lvl
Printf(string, ...interface{}) SetLevel(v log.Lvl)
Printj(log.JSON) Print(i ...interface{})
Debug(...interface{}) Printf(format string, args ...interface{})
Debugf(string, ...interface{}) Printj(j log.JSON)
Debugj(log.JSON) Debug(i ...interface{})
Info(...interface{}) Debugf(format string, args ...interface{})
Infof(string, ...interface{}) Debugj(j log.JSON)
Infoj(log.JSON) Info(i ...interface{})
Warn(...interface{}) Infof(format string, args ...interface{})
Warnf(string, ...interface{}) Infoj(j log.JSON)
Warnj(log.JSON) Warn(i ...interface{})
Error(...interface{}) Warnf(format string, args ...interface{})
Errorf(string, ...interface{}) Warnj(j log.JSON)
Errorj(log.JSON) Error(i ...interface{})
Fatal(...interface{}) Errorf(format string, args ...interface{})
Fatalj(log.JSON) Errorj(j log.JSON)
Fatalf(string, ...interface{}) Fatal(i ...interface{})
Panic(...interface{}) Fatalj(j log.JSON)
Panicj(log.JSON) Fatalf(format string, args ...interface{})
Panicf(string, ...interface{}) Panic(i ...interface{})
Panicj(j log.JSON)
Panicf(format string, args ...interface{})
} }
) )

View File

@ -10,4 +10,4 @@ type = "recipes"
url = "/recipes" url = "/recipes"
+++ +++
## Echo Examples <script>location = "/recipes/hello-world";</script>

View File

@ -14,7 +14,7 @@ Browse to `https://<your_domain>`. If everything goes fine, you should see a wel
message with TLS enabled on the website. message with TLS enabled on the website.
> >
- For added security you should specify host policy in auto TLS manage - For added security you should specify host policy in auto TLS manager
- To redirect HTTP traffic to HTTPS, you can use [redirect middleware](/middleware/redirect#https-redirect) - To redirect HTTP traffic to HTTPS, you can use [redirect middleware](/middleware/redirect#https-redirect)
## Server ## Server

View File

@ -43,7 +43,6 @@ User signup
- Retrieve user credentials from the body and validate against database. - Retrieve user credentials from the body and validate against database.
- For invalid email or password, send `400 - Bad Request` response. - For invalid email or password, send `400 - Bad Request` response.
- For valid email and password, save user in database and send `201 - Created` response. - For valid email and password, save user in database and send `201 - Created` response.
-
#### Request #### Request