mirror of
https://github.com/labstack/echo.git
synced 2025-01-01 22:09:21 +02:00
commit
6b02099ee6
7
echo.go
7
echo.go
@ -293,7 +293,7 @@ func (e *Echo) add(method, path string, h Handler) {
|
||||
// Static serves static files.
|
||||
func (e *Echo) Static(path, root string) {
|
||||
fs := http.StripPrefix(path, http.FileServer(http.Dir(root)))
|
||||
e.Get(path+"/*", func(c *Context) *HTTPError {
|
||||
e.Get(path+"*", func(c *Context) *HTTPError {
|
||||
fs.ServeHTTP(c.Response, c.Request)
|
||||
return nil
|
||||
})
|
||||
@ -312,6 +312,11 @@ func (e *Echo) Index(file string) {
|
||||
e.ServeFile("/", file)
|
||||
}
|
||||
|
||||
// Favicon serves the default favicon - GET /favicon.ico.
|
||||
func (e *Echo) Favicon(file string) {
|
||||
e.ServeFile("/favicon.ico", file)
|
||||
}
|
||||
|
||||
func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
c := e.pool.Get().(*Context)
|
||||
h, echo := e.Router.Find(r.Method, r.URL.Path, c)
|
||||
|
11
echo_test.go
11
echo_test.go
@ -39,6 +39,17 @@ func TestEchoIndex(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEchoFavicon(t *testing.T) {
|
||||
e := New()
|
||||
e.Favicon("examples/web/public/favicon.ico")
|
||||
w := httptest.NewRecorder()
|
||||
r, _ := http.NewRequest(GET, "/favicon.ico", nil)
|
||||
e.ServeHTTP(w, r)
|
||||
if w.Code != 200 {
|
||||
t.Errorf("status code should be 200, found %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEchoStatic(t *testing.T) {
|
||||
e := New()
|
||||
e.Static("/scripts", "examples/web/public/scripts")
|
||||
|
BIN
examples/web/public/favicon.ico
Executable file
BIN
examples/web/public/favicon.ico
Executable file
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
@ -1,11 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="x-ua-compatible" content="ie=edge">
|
||||
<title>Echo</title>
|
||||
<link rel="shortcut icon" href="favicon.ico" />
|
||||
<meta name="description" content="">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
</head>
|
||||
<body>
|
||||
Echo!
|
||||
<script src="/scripts/main.js"></script>
|
||||
<h1>Echo!</h1>
|
||||
<script src="/scripts/main.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -36,8 +36,12 @@ func (t *Template) Render(w io.Writer, name string, data interface{}) *echo.HTTP
|
||||
return nil
|
||||
}
|
||||
|
||||
func welcome(c *echo.Context) {
|
||||
c.Render(http.StatusOK, "welcome", "Joe")
|
||||
//----------
|
||||
// Handlers
|
||||
//----------
|
||||
|
||||
func welcome(c *echo.Context) *echo.HTTPError {
|
||||
return c.Render(http.StatusOK, "welcome", "Joe")
|
||||
}
|
||||
|
||||
func createUser(c *echo.Context) *echo.HTTPError {
|
||||
@ -74,15 +78,18 @@ func main() {
|
||||
s := stats.New()
|
||||
e.Use(s.Handler)
|
||||
// Route
|
||||
e.Get("/stats", func(c *echo.Context) {
|
||||
c.JSON(http.StatusOK, s.Data())
|
||||
e.Get("/stats", func(c *echo.Context) *echo.HTTPError {
|
||||
return c.JSON(http.StatusOK, s.Data())
|
||||
})
|
||||
|
||||
// Serve index file
|
||||
e.Index("public/index.html")
|
||||
|
||||
// Serve favicon
|
||||
e.Favicon("public/favicon.ico")
|
||||
|
||||
// Serve static files
|
||||
e.Static("/scripts", "public/scripts")
|
||||
e.Static("/scripts/", "public/scripts")
|
||||
|
||||
//--------
|
||||
// Routes
|
||||
@ -109,19 +116,21 @@ func main() {
|
||||
|
||||
// Group with parent middleware
|
||||
a := e.Group("/admin")
|
||||
a.Use(func(c *echo.Context) {
|
||||
a.Use(func(c *echo.Context) *echo.HTTPError {
|
||||
// Security middleware
|
||||
return nil
|
||||
})
|
||||
a.Get("", func(c *echo.Context) {
|
||||
c.String(http.StatusOK, "Welcome admin!")
|
||||
a.Get("", func(c *echo.Context) *echo.HTTPError {
|
||||
return c.String(http.StatusOK, "Welcome admin!")
|
||||
})
|
||||
|
||||
// Group with no parent middleware
|
||||
g := e.Group("/files", func(c *echo.Context) {
|
||||
g := e.Group("/files", func(c *echo.Context) *echo.HTTPError {
|
||||
// Security middleware
|
||||
return nil
|
||||
})
|
||||
g.Get("", func(c *echo.Context) {
|
||||
c.String(http.StatusOK, "Your files!")
|
||||
g.Get("", func(c *echo.Context) *echo.HTTPError {
|
||||
return c.String(http.StatusOK, "Your files!")
|
||||
})
|
||||
|
||||
// Start server
|
||||
|
@ -137,11 +137,11 @@ func (r *router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
|
||||
}
|
||||
}
|
||||
|
||||
func newNode(t ntype, pfx string, p *node, c children, h HandlerFunc, pnames []string, echo *Echo) *node {
|
||||
func newNode(t ntype, pre string, p *node, c children, h HandlerFunc, pnames []string, echo *Echo) *node {
|
||||
return &node{
|
||||
typ: t,
|
||||
label: pfx[0],
|
||||
prefix: pfx,
|
||||
label: pre[0],
|
||||
prefix: pre,
|
||||
parent: p,
|
||||
children: c,
|
||||
handler: h,
|
||||
|
@ -193,18 +193,17 @@ Sends an HTML HTTP response with status code.
|
||||
|
||||
### Static files
|
||||
|
||||
`echo.Static(path, root string)` serves static files. For example, code below
|
||||
serves all files from `public/scripts` directory for any path starting
|
||||
with `/scripts/`
|
||||
`echo.Static(path, root string)` serves static files. For example, code below serves
|
||||
files from directory `public/scripts` for any URL path starting with `/scripts/`.
|
||||
|
||||
```go
|
||||
e.Static("/scripts", "public/scripts")
|
||||
e.Static("/scripts/", "public/scripts")
|
||||
```
|
||||
|
||||
### Serving a file
|
||||
|
||||
`echo.ServeFile(path, file string)` serves a file. For example, code below serves
|
||||
welcome.html for path `/welcome`
|
||||
file `welcome.html` for URL path `/welcome`.
|
||||
|
||||
```go
|
||||
e.ServeFile("/welcome", "welcome.html")
|
||||
@ -212,13 +211,22 @@ e.ServeFile("/welcome", "welcome.html")
|
||||
|
||||
### Serving an index file
|
||||
|
||||
`echo.Index(file string)` serves an index file. For example, code below serves
|
||||
index.html for path `/`
|
||||
`echo.Index(file string)` serves an index file. For example, code below serves file
|
||||
`index.html`.
|
||||
|
||||
```go
|
||||
e.Index("index.html")
|
||||
```
|
||||
|
||||
### Serving favicon
|
||||
|
||||
`echo.Favicon(file string)` serves default favicon - `GET /favicon.ico`. For example,
|
||||
code below serves file `favicon.ico`.
|
||||
|
||||
```go
|
||||
e.Index("public/favicon.ico")
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
Echo advocates centralized HTTP error handling by returning `*echo.HTTPError` from
|
||||
|
Loading…
Reference in New Issue
Block a user