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 (
// Binder is the interface that wraps the Bind method.
Binder interface {
Bind(interface{}, Context) error
Bind(i interface{}, c Context) error
}
binder struct{}

View File

@ -23,7 +23,7 @@ type (
Request() *http.Request
// SetRequest sets `*http.Request`.
SetRequest(*http.Request)
SetRequest(r *http.Request)
// Request returns `*Response`.
Response() *Response
@ -42,25 +42,25 @@ type (
Path() string
// SetPath sets the registered path for the handler.
SetPath(string)
SetPath(p string)
// Param returns path parameter by name.
Param(string) string
Param(name string) string
// ParamNames returns path parameter names.
ParamNames() []string
// SetParamNames sets path parameter names.
SetParamNames(...string)
SetParamNames(names ...string)
// ParamValues returns path parameter values.
ParamValues() []string
// SetParamValues sets path parameter values.
SetParamValues(...string)
SetParamValues(values ...string)
// 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() url.Values
@ -69,90 +69,96 @@ type (
QueryString() string
// 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() (url.Values, error)
// 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() (*multipart.Form, error)
// 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(*http.Cookie)
SetCookie(cookie *http.Cookie)
// Cookies returns the HTTP cookies sent with the request.
Cookies() []*http.Cookie
// Get retrieves data from the context.
Get(string) interface{}
Get(key string) interface{}
// 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
// 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
// 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(int, string) error
HTML(code int, html string) error
// 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(int, string) error
String(code int, s string) error
// 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(int, []byte) error
JSONBlob(code int, b []byte) error
// JSONP sends a JSONP response with status code. It uses `callback` to construct
// 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`
// 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(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(int, []byte) error
XMLBlob(code int, b []byte) error
// 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(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(string) error
File(file string) error
// Attachment sends a response as attachment, prompting client to save the
// file.
Attachment(string, string) error
Attachment(file string, name string) error
// 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(int) error
NoContent(code int) error
// 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(err error)
@ -161,7 +167,7 @@ type (
Handler() HandlerFunc
// SetHandler sets the matched handler by router.
SetHandler(HandlerFunc)
SetHandler(h HandlerFunc)
// Logger returns the `Logger` instance.
Logger() Logger
@ -172,7 +178,7 @@ type (
// Reset resets the context after request completes. It must be called along
// with `Echo#AcquireContext()` and `Echo#ReleaseContext()`.
// See `Echo#ServeHTTP()`
Reset(*http.Request, http.ResponseWriter)
Reset(r *http.Request, w http.ResponseWriter)
}
context struct {
@ -329,6 +335,10 @@ func (c *context) Cookies() []*http.Cookie {
return c.request.Cookies()
}
func (c *context) Get(key string) interface{} {
return c.store[key]
}
func (c *context) Set(key string, val interface{}) {
if c.store == nil {
c.store = make(Map)
@ -336,10 +346,6 @@ func (c *context) Set(key string, val interface{}) {
c.store[key] = val
}
func (c *context) Get(key string) interface{} {
return c.store[key]
}
func (c *context) Bind(i interface{}) error {
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) {
b, err := json.Marshal(i)
if c.echo.Debug {
b, err = json.MarshalIndent(i, "", " ")
}
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)
}
@ -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) {
b, err := json.Marshal(i)
if err != nil {
return err
return
}
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) {
b, err := xml.Marshal(i)
if c.echo.Debug {
b, err = xml.MarshalIndent(i, "", " ")
}
if err != nil {
return err
return
}
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) {
c.response.Header().Set(HeaderContentType, MIMEApplicationXMLCharsetUTF8)
c.response.WriteHeader(code)

View File

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

View File

@ -10,4 +10,4 @@ type = "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.
>
- 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)
## Server

View File

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