Skip to content

Commit

Permalink
- Moved static file serving to a new handler package
Browse files Browse the repository at this point in the history
- Middleware at route level
- Group middleware is a in closure now

Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Feb 15, 2016
1 parent 6bb871f commit 51acf46
Show file tree
Hide file tree
Showing 17 changed files with 485 additions and 609 deletions.
50 changes: 35 additions & 15 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package echo
import (
"encoding/json"
"encoding/xml"
"io"
"mime"
"net/http"
"os"
"path/filepath"
"time"

Expand Down Expand Up @@ -42,10 +45,11 @@ type (
JSONP(int, string, interface{}) error
XML(int, interface{}) error
XMLBlob(int, []byte) error
File(string, string, bool) error
Attachment(string) error
NoContent(int) error
Redirect(int, string) error
Error(err error)
Handle(Context) error
Logger() logger.Logger
Object() *context
}
Expand All @@ -59,12 +63,17 @@ type (
pvalues []string
query url.Values
store store
handler Handler
echo *Echo
}

store map[string]interface{}
)

const (
indexPage = "index.html"
)

// NewContext creates a Context object.
func NewContext(req engine.Request, res engine.Response, e *Echo) Context {
return &context{
Expand All @@ -73,9 +82,14 @@ func NewContext(req engine.Request, res engine.Response, e *Echo) Context {
echo: e,
pvalues: make([]string, *e.maxParam),
store: make(store),
handler: notFoundHandler,
}
}

func (c *context) Handle(ctx Context) error {
return c.handler.Handle(ctx)
}

func (c *context) Deadline() (deadline time.Time, ok bool) {
return
}
Expand Down Expand Up @@ -166,7 +180,7 @@ func (c *context) Bind(i interface{}) error {
// code. Templates can be registered using `Echo.SetRenderer()`.
func (c *context) Render(code int, name string, data interface{}) (err error) {
if c.echo.renderer == nil {
return RendererNotRegistered
return ErrRendererNotRegistered
}
buf := new(bytes.Buffer)
if err = c.echo.renderer.Render(buf, name, data); err != nil {
Expand Down Expand Up @@ -250,17 +264,17 @@ func (c *context) XMLBlob(code int, b []byte) (err error) {
return
}

// 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) {
dir, file := filepath.Split(path)
if attachment {
c.response.Header().Set(ContentDisposition, "attachment; filename="+name)
}
if err = c.echo.serveFile(dir, file, c); err != nil {
c.response.Header().Del(ContentDisposition)
// Attachment sends specified file as an attachment to the client.
func (c *context) Attachment(file string) (err error) {
f, err := os.Open(file)
if err != nil {
return
}
_, name := filepath.Split(file)
c.response.Header().Set(ContentDisposition, "attachment; filename="+name)
c.response.Header().Set(ContentType, c.detectContentType(file))
c.response.WriteHeader(http.StatusOK)
_, err = io.Copy(c.response, f)
return
}

Expand All @@ -273,7 +287,7 @@ func (c *context) NoContent(code int) error {
// Redirect redirects the request using http.Redirect with status code.
func (c *context) Redirect(code int, url string) error {
if code < http.StatusMultipleChoices || code > http.StatusTemporaryRedirect {
return InvalidRedirectCode
return ErrInvalidRedirectCode
}
// TODO: v2
// http.Redirect(c.response, c.request, url, code)
Expand All @@ -295,10 +309,16 @@ func (c *context) Object() *context {
return c
}

func (c *context) reset(req engine.Request, res engine.Response, e *Echo) {
func (c *context) detectContentType(name string) (t string) {
if t = mime.TypeByExtension(filepath.Ext(name)); t == "" {
t = OctetStream
}
return
}

func (c *context) reset(req engine.Request, res engine.Response) {
c.request = req
c.response = res
c.query = nil
c.store = nil
c.echo = e
}
19 changes: 5 additions & 14 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,13 @@ func TestContext(t *testing.T) {
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
}

// File
// Attachment
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
err = c.File("_fixture/images/walle.png", "", false)
err = c.Attachment("_fixture/images/walle.png")
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, 219885, rec.Body.Len())
}

// File as attachment
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
err = c.File("_fixture/images/walle.png", "WALLE.PNG", true)
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Status())
assert.Equal(t, rec.Header().Get(ContentDisposition), "attachment; filename=WALLE.PNG")
assert.Equal(t, rec.Header().Get(ContentDisposition), "attachment; filename=walle.png")
assert.Equal(t, 219885, rec.Body.Len())
}

Expand All @@ -194,7 +185,7 @@ func TestContext(t *testing.T) {
assert.Equal(t, http.StatusInternalServerError, c.Response().Status())

// reset
c.Object().reset(req, test.NewResponseRecorder(), e)
c.Object().reset(req, test.NewResponseRecorder())
}

func TestContextPath(t *testing.T) {
Expand Down Expand Up @@ -263,7 +254,7 @@ func testBindError(t *testing.T, c Context, ct string) {
}
default:
if assert.IsType(t, new(HTTPError), err) {
assert.Equal(t, UnsupportedMediaType, err)
assert.Equal(t, ErrUnsupportedMediaType, err)
}

}
Expand Down
Loading

0 comments on commit 51acf46

Please sign in to comment.