Skip to content

Commit

Permalink
Step towards v2
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Dec 4, 2015
1 parent 5cd194b commit 72459fe
Show file tree
Hide file tree
Showing 14 changed files with 211 additions and 160 deletions.
109 changes: 77 additions & 32 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,48 @@ import (
"encoding/xml"
"net/http"
"path/filepath"
"time"

"net/url"

"bytes"

"golang.org/x/net/context"
xcontext "golang.org/x/net/context"
"golang.org/x/net/websocket"
)

type (
// Context represents context for the current request. It holds request and
// response objects, path parameters, data and registered handler.
Context struct {
context.Context
Context interface {
xcontext.Context
Request() *http.Request
Response() *Response
Socket() *websocket.Conn
Path() string
P(int) string
Param(string) string
Query(string) string
Form(string) string
Set(string, interface{})
Get(string) interface{}
Bind(interface{}) error
Render(int, string, interface{}) error
HTML(int, string) error
String(int, string) error
JSON(int, interface{}) error
JSONIndent(int, interface{}, string, string) error
JSONP(int, string, interface{}) error
XML(int, interface{}) error
XMLIndent(int, interface{}, string, string) error
File(string, string, bool) error
NoContent(int) error
Redirect(int, string) error
Error(err error)
Echo() *Echo
}

context struct {
request *http.Request
response *Response
socket *websocket.Conn
Expand All @@ -29,12 +57,13 @@ type (
store store
echo *Echo
}

store map[string]interface{}
)

// NewContext creates a Context object.
func NewContext(req *http.Request, res *Response, e *Echo) *Context {
return &Context{
func NewContext(req *http.Request, res *Response, e *Echo) Context {
return &context{
request: req,
response: res,
echo: e,
Expand All @@ -43,28 +72,44 @@ func NewContext(req *http.Request, res *Response, e *Echo) *Context {
}
}

func (c *context) Deadline() (deadline time.Time, ok bool) {
return
}

func (c *context) Done() <-chan struct{} {
return nil
}

func (c *context) Err() error {
return nil
}

func (c *context) Value(key interface{}) interface{} {
return nil
}

// Request returns *http.Request.
func (c *Context) Request() *http.Request {
func (c *context) Request() *http.Request {
return c.request
}

// Response returns *Response.
func (c *Context) Response() *Response {
func (c *context) Response() *Response {
return c.response
}

// Socket returns *websocket.Conn.
func (c *Context) Socket() *websocket.Conn {
func (c *context) Socket() *websocket.Conn {
return c.socket
}

// Path returns the registered path for the handler.
func (c *Context) Path() string {
func (c *context) Path() string {
return c.path
}

// P returns path parameter by index.
func (c *Context) P(i int) (value string) {
func (c *context) P(i int) (value string) {
l := len(c.pnames)
if i < l {
value = c.pvalues[i]
Expand All @@ -73,7 +118,7 @@ func (c *Context) P(i int) (value string) {
}

// Param returns path parameter by name.
func (c *Context) Param(name string) (value string) {
func (c *context) Param(name string) (value string) {
l := len(c.pnames)
for i, n := range c.pnames {
if n == name && i < l {
Expand All @@ -85,25 +130,25 @@ func (c *Context) Param(name string) (value string) {
}

// Query returns query parameter by name.
func (c *Context) Query(name string) string {
func (c *context) Query(name string) string {
if c.query == nil {
c.query = c.request.URL.Query()
}
return c.query.Get(name)
}

// Form returns form parameter by name.
func (c *Context) Form(name string) string {
func (c *context) Form(name string) string {
return c.request.FormValue(name)
}

// Get retrieves data from the context.
func (c *Context) Get(key string) interface{} {
func (c *context) Get(key string) interface{} {
return c.store[key]
}

// Set saves data in the context.
func (c *Context) Set(key string, val interface{}) {
func (c *context) Set(key string, val interface{}) {
if c.store == nil {
c.store = make(store)
}
Expand All @@ -112,13 +157,13 @@ func (c *Context) Set(key string, val interface{}) {

// Bind binds the request body into specified type `i`. The default binder does
// it based on Content-Type header.
func (c *Context) Bind(i interface{}) error {
func (c *context) Bind(i interface{}) error {
return c.echo.binder.Bind(c.request, i)
}

// Render renders a template with data and sends a text/html response with status
// code. Templates can be registered using `Echo.SetRenderer()`.
func (c *Context) Render(code int, name string, data interface{}) (err error) {
func (c *context) Render(code int, name string, data interface{}) (err error) {
if c.echo.renderer == nil {
return RendererNotRegistered
}
Expand All @@ -133,23 +178,23 @@ func (c *Context) Render(code int, name string, data interface{}) (err error) {
}

// HTML sends an HTTP response with status code.
func (c *Context) HTML(code int, html string) (err error) {
func (c *context) HTML(code int, html string) (err error) {
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
c.response.WriteHeader(code)
c.response.Write([]byte(html))
return
}

// String sends a string response with status code.
func (c *Context) String(code int, s string) (err error) {
func (c *context) String(code int, s string) (err error) {
c.response.Header().Set(ContentType, TextPlainCharsetUTF8)
c.response.WriteHeader(code)
c.response.Write([]byte(s))
return
}

// JSON sends a JSON response with status code.
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)
if err != nil {
return err
Expand All @@ -161,7 +206,7 @@ func (c *Context) JSON(code int, i interface{}) (err error) {
}

// JSONIndent sends a JSON response with status code, but it applies prefix and indent to format the output.
func (c *Context) JSONIndent(code int, i interface{}, prefix string, indent string) (err error) {
func (c *context) JSONIndent(code int, i interface{}, prefix string, indent string) (err error) {
b, err := json.MarshalIndent(i, prefix, indent)
if err != nil {
return err
Expand All @@ -170,15 +215,15 @@ func (c *Context) JSONIndent(code int, i interface{}, prefix string, indent stri
return
}

func (c *Context) json(code int, b []byte) {
func (c *context) json(code int, b []byte) {
c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8)
c.response.WriteHeader(code)
c.response.Write(b)
}

// JSONP sends a JSONP response with status code. It uses `callback` to construct
// the JSONP payload.
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)
if err != nil {
return err
Expand All @@ -192,7 +237,7 @@ func (c *Context) JSONP(code int, callback string, i interface{}) (err error) {
}

// XML sends an XML response with status code.
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)
if err != nil {
return err
Expand All @@ -205,7 +250,7 @@ func (c *Context) XML(code int, i interface{}) (err error) {
}

// XMLIndent sends an XML response with status code, but it applies prefix and indent to format the output.
func (c *Context) XMLIndent(code int, i interface{}, prefix string, indent string) (err error) {
func (c *context) XMLIndent(code int, i interface{}, prefix string, indent string) (err error) {
b, err := xml.MarshalIndent(i, prefix, indent)
if err != nil {
return err
Expand All @@ -214,7 +259,7 @@ func (c *Context) XMLIndent(code int, i interface{}, prefix string, indent strin
return
}

func (c *Context) xml(code int, b []byte) {
func (c *context) xml(code int, b []byte) {
c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8)
c.response.WriteHeader(code)
c.response.Write([]byte(xml.Header))
Expand All @@ -224,7 +269,7 @@ func (c *Context) xml(code int, b []byte) {
// File sends a response with the content of the file. If `attachment` is set
// to true, the client is prompted to save the file with provided `name`,
// name can be empty, in that case name of the file is used.
func (c *Context) File(path, name string, attachment bool) (err error) {
func (c *context) File(path, name string, attachment bool) (err error) {
dir, file := filepath.Split(path)
if attachment {
c.response.Header().Set(ContentDisposition, "attachment; filename="+name)
Expand All @@ -236,13 +281,13 @@ func (c *Context) File(path, name string, attachment bool) (err error) {
}

// NoContent sends a response with no body and a status code.
func (c *Context) NoContent(code int) error {
func (c *context) NoContent(code int) error {
c.response.WriteHeader(code)
return nil
}

// Redirect redirects the request using http.Redirect with status code.
func (c *Context) Redirect(code int, url string) error {
func (c *context) Redirect(code int, url string) error {
if code < http.StatusMultipleChoices || code > http.StatusTemporaryRedirect {
return InvalidRedirectCode
}
Expand All @@ -251,16 +296,16 @@ func (c *Context) Redirect(code int, url string) error {
}

// Error invokes the registered HTTP error handler. Generally used by middleware.
func (c *Context) Error(err error) {
func (c *context) Error(err error) {
c.echo.httpErrorHandler(err, c)
}

// Echo returns the `Echo` instance.
func (c *Context) Echo() *Echo {
func (c *context) Echo() *Echo {
return c.echo
}

func (c *Context) reset(r *http.Request, w http.ResponseWriter, e *Echo) {
func (c *context) reset(r *http.Request, w http.ResponseWriter, e *Echo) {
c.request = r
c.response.reset(w, e)
c.query = nil
Expand Down
Loading

0 comments on commit 72459fe

Please sign in to comment.