mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
parent
7263e50e10
commit
082814c776
43
context.go
43
context.go
@ -7,6 +7,7 @@ import (
|
||||
"mime"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
@ -44,6 +45,7 @@ type (
|
||||
JSONP(int, string, interface{}) error
|
||||
XML(int, interface{}) error
|
||||
XMLBlob(int, []byte) error
|
||||
File(string) error
|
||||
Attachment(string) error
|
||||
NoContent(int) error
|
||||
Redirect(int, string) error
|
||||
@ -263,7 +265,30 @@ func (c *context) XMLBlob(code int, b []byte) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Attachment sends specified file as an attachment to the client.
|
||||
// File sends a response with the content of the file.
|
||||
func (c *context) File(file string) error {
|
||||
root, file := filepath.Split(file)
|
||||
fs := http.Dir(root)
|
||||
f, err := fs.Open(file)
|
||||
if err != nil {
|
||||
return ErrNotFound
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
fi, _ := f.Stat()
|
||||
if fi.IsDir() {
|
||||
file = path.Join(file, "index.html")
|
||||
f, err = fs.Open(file)
|
||||
if err != nil {
|
||||
return ErrNotFound
|
||||
}
|
||||
fi, _ = f.Stat()
|
||||
}
|
||||
|
||||
return ServeContent(c.Request(), c.Response(), f, fi)
|
||||
}
|
||||
|
||||
// Attachment sends a response as file attachment, prompting client to save the file.
|
||||
func (c *context) Attachment(file string) (err error) {
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
@ -271,7 +296,7 @@ func (c *context) Attachment(file string) (err error) {
|
||||
}
|
||||
_, name := filepath.Split(file)
|
||||
c.response.Header().Set(ContentDisposition, "attachment; filename="+name)
|
||||
c.response.Header().Set(ContentType, c.detectContentType(file))
|
||||
c.response.Header().Set(ContentType, detectContentType(file))
|
||||
c.response.WriteHeader(http.StatusOK)
|
||||
_, err = io.Copy(c.response, f)
|
||||
return
|
||||
@ -313,7 +338,19 @@ func (c *context) Object() *context {
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *context) detectContentType(name string) (t string) {
|
||||
func ServeContent(req engine.Request, res engine.Response, f http.File, fi os.FileInfo) error {
|
||||
// TODO: http.ServeContent(c.Response(), c.Request(), fi.Name(), fi.ModTime(), f)
|
||||
ct := mime.TypeByExtension(filepath.Ext(fi.Name()))
|
||||
if ct == "" {
|
||||
ct = OctetStream
|
||||
}
|
||||
req.Header().Set(ContentType, ct)
|
||||
res.WriteHeader(http.StatusOK)
|
||||
_, err := io.Copy(res, f)
|
||||
return err
|
||||
}
|
||||
|
||||
func detectContentType(name string) (t string) {
|
||||
if t = mime.TypeByExtension(filepath.Ext(name)); t == "" {
|
||||
t = OctetStream
|
||||
}
|
||||
|
15
echo.go
15
echo.go
@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"path"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
@ -348,6 +349,20 @@ func (e *Echo) Match(methods []string, path string, handler Handler, middleware
|
||||
}
|
||||
}
|
||||
|
||||
// Static serves files from provided `root` directory for `/<prefix>*` HTTP path.
|
||||
func (e *Echo) Static(prefix, root string) {
|
||||
e.Get(prefix+"*", HandlerFunc(func(c Context) error {
|
||||
return c.File(path.Join(root, c.P(0))) // Param `_`
|
||||
}))
|
||||
}
|
||||
|
||||
// File serves provided file for `/<path>` HTTP path.
|
||||
func (e *Echo) File(path, file string) {
|
||||
e.Get(path, HandlerFunc(func(c Context) error {
|
||||
return c.File(file)
|
||||
}))
|
||||
}
|
||||
|
||||
func (e *Echo) add(method, path string, handler Handler, middleware ...Middleware) {
|
||||
name := handlerName(handler)
|
||||
e.router.Add(method, path, HandlerFunc(func(c Context) error {
|
||||
|
39
echo_test.go
39
echo_test.go
@ -40,6 +40,45 @@ func TestEcho(t *testing.T) {
|
||||
assert.Equal(t, http.StatusInternalServerError, rec.Status())
|
||||
}
|
||||
|
||||
func TestEchoStatic(t *testing.T) {
|
||||
e := New()
|
||||
|
||||
// OK
|
||||
e.Static("/images", "_fixture/images")
|
||||
c, b := request(GET, "/images/walle.png", e)
|
||||
assert.Equal(t, http.StatusOK, c)
|
||||
assert.NotEmpty(t, b)
|
||||
|
||||
// No file
|
||||
e.Static("/images", "_fixture/scripts")
|
||||
c, _ = request(GET, "/images/bolt.png", e)
|
||||
assert.Equal(t, http.StatusNotFound, c)
|
||||
|
||||
// Directory
|
||||
e.Static("/images", "_fixture/images")
|
||||
c, _ = request(GET, "/images", e)
|
||||
assert.Equal(t, http.StatusNotFound, c)
|
||||
|
||||
// Directory with index.html
|
||||
e.Static("/", "_fixture")
|
||||
c, r := request(GET, "/", e)
|
||||
assert.Equal(t, http.StatusOK, c)
|
||||
assert.Equal(t, true, strings.HasPrefix(r, "<!doctype html>"))
|
||||
|
||||
// Sub-directory with index.html
|
||||
c, r = request(GET, "/folder", e)
|
||||
assert.Equal(t, http.StatusOK, c)
|
||||
assert.Equal(t, true, strings.HasPrefix(r, "<!doctype html>"))
|
||||
}
|
||||
|
||||
func TestEchoFile(t *testing.T) {
|
||||
e := New()
|
||||
e.File("/walle", "_fixture/images/walle.png")
|
||||
c, b := request(GET, "/walle", e)
|
||||
assert.Equal(t, http.StatusOK, c)
|
||||
assert.NotEmpty(t, b)
|
||||
}
|
||||
|
||||
func TestEchoMiddleware(t *testing.T) {
|
||||
e := New()
|
||||
buf := new(bytes.Buffer)
|
||||
|
@ -25,7 +25,7 @@ type (
|
||||
|
||||
// Gzip returns a middleware which compresses HTTP response using gzip compression
|
||||
// scheme.
|
||||
func Gzip(options ...*GzipOptions) echo.MiddlewareFunc {
|
||||
func Gzip(options ...GzipOptions) echo.MiddlewareFunc {
|
||||
return func(next echo.Handler) echo.Handler {
|
||||
scheme := "gzip"
|
||||
return echo.HandlerFunc(func(c echo.Context) error {
|
||||
|
@ -13,7 +13,7 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func Logger(options ...*LoggerOptions) echo.MiddlewareFunc {
|
||||
func Logger(options ...LoggerOptions) echo.MiddlewareFunc {
|
||||
return func(next echo.Handler) echo.Handler {
|
||||
return echo.HandlerFunc(func(c echo.Context) error {
|
||||
req := c.Request()
|
||||
|
@ -13,7 +13,7 @@ type (
|
||||
|
||||
// Recover returns a middleware which recovers from panics anywhere in the chain
|
||||
// and handles the control to the centralized HTTPErrorHandler.
|
||||
func Recover(options ...*RecoverOptions) echo.MiddlewareFunc {
|
||||
func Recover(options ...RecoverOptions) echo.MiddlewareFunc {
|
||||
return func(next echo.Handler) echo.Handler {
|
||||
// TODO: Provide better stack trace
|
||||
// - `https://github.com/go-errors/errors`
|
||||
|
@ -2,11 +2,8 @@ package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
)
|
||||
@ -19,10 +16,10 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func Static(root string, options ...*StaticOptions) echo.MiddlewareFunc {
|
||||
func Static(root string, options ...StaticOptions) echo.MiddlewareFunc {
|
||||
return func(next echo.Handler) echo.Handler {
|
||||
// Default options
|
||||
opts := new(StaticOptions)
|
||||
opts := StaticOptions{}
|
||||
if len(options) > 0 {
|
||||
opts = options[0]
|
||||
}
|
||||
@ -85,23 +82,7 @@ func Static(root string, options ...*StaticOptions) echo.MiddlewareFunc {
|
||||
}
|
||||
fi, _ = f.Stat() // Index file stat
|
||||
}
|
||||
ct := mime.TypeByExtension(filepath.Ext(fi.Name()))
|
||||
if ct == "" {
|
||||
ct = echo.OctetStream
|
||||
}
|
||||
c.Response().Header().Set(echo.ContentType, ct)
|
||||
c.Response().WriteHeader(http.StatusOK)
|
||||
_, err = io.Copy(c.Response(), f)
|
||||
return err
|
||||
// TODO:
|
||||
// http.ServeContent(c.Response(), c.Request(), fi.Name(), fi.ModTime(), f)
|
||||
return echo.ServeContent(c.Request(), c.Response(), f, fi)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Favicon serves the default favicon - GET /favicon.ico.
|
||||
func Favicon() echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
@ -15,12 +15,6 @@ menu:
|
||||
us to use HTTP servers beyond Go standard library. It currently supports standard HTTP server and [FastHTTP](https://github.com/valyala/fasthttp).
|
||||
- Context, Request and Response are converted to interfaces. [More...](https://github.com/labstack/echo/issues/146)
|
||||
- Handler signature is changed to `func (c echo.Context) error`.
|
||||
- Moved API's for serving static files into middleware.
|
||||
- `Echo#Index`
|
||||
- `Echo#Favicon`
|
||||
- `Echo#Static`
|
||||
- `Echo#ServeDir`
|
||||
- `Echo#ServeFile`
|
||||
- Dropped auto wrapping of handler and middleware to enforce compile time check.
|
||||
- Handler only accepts `Echo#Handler` interface.
|
||||
- Middleware only accepts `Echo#Middleware` interface.
|
||||
@ -33,4 +27,6 @@ it can be achieved via middleware.
|
||||
|
||||
#### How?
|
||||
|
||||
Quite easy, browse through [recipes](/recipes/hello-world) freshly converted to v2.
|
||||
Quite easy
|
||||
- Browse through [recipes](/recipes/hello-world) freshly converted to v2.
|
||||
- Read documentation and dig into test cases.
|
||||
|
@ -88,14 +88,35 @@ Sends an HTML response with status code.
|
||||
|
||||
Sends a string response with status code.
|
||||
|
||||
### File
|
||||
|
||||
`func (c *context) File(file string) error`
|
||||
|
||||
Sends a response with the content of the file.
|
||||
|
||||
### Attachment
|
||||
|
||||
`Context#Attachment(file string) (err error)`
|
||||
`Context#Attachment(file string) error`
|
||||
|
||||
Sends file as an attachment.
|
||||
Sends a response as file attachment, prompting client to save the file.
|
||||
|
||||
### Static files
|
||||
### Static Files
|
||||
|
||||
`Echo#Use(middleware.Static("public"))`
|
||||
`Echo#Use(middleware.Static(root string))`
|
||||
|
||||
Serves static files from public folder.
|
||||
Serves static files from the provided `root` directory.
|
||||
|
||||
`Echo#Static(prefix, root string)`
|
||||
|
||||
Serves files from provided `root` directory for `/<prefix>*` HTTP path.
|
||||
|
||||
`Echo#File(path, file string)`
|
||||
|
||||
Serves provided `file` for `/<path>` HTTP path.
|
||||
|
||||
*Examples*
|
||||
|
||||
- Serving static files with no prefix `e.Use(middleware.Static("public"))`
|
||||
- Serving static files with a prefix `e.Static("/static", "assets")`
|
||||
- Serving an index page `e.File("/", "public/index.html")`
|
||||
- Serving a favicon `e.File("/favicon.ico", "images/facicon.ico")`
|
||||
|
Loading…
Reference in New Issue
Block a user