1
0
mirror of https://github.com/labstack/echo.git synced 2025-05-13 22:06:36 +02:00

Added favicon api

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-05-13 15:20:09 -07:00
parent e55f5f1636
commit ba05b6e58e
6 changed files with 46 additions and 17 deletions

View File

@ -312,6 +312,11 @@ func (e *Echo) Index(file string) {
e.ServeFile("/", file) 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) { func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c := e.pool.Get().(*Context) c := e.pool.Get().(*Context)
h, echo := e.Router.Find(r.Method, r.URL.Path, c) h, echo := e.Router.Find(r.Method, r.URL.Path, c)

View File

@ -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) { func TestEchoStatic(t *testing.T) {
e := New() e := New()
e.Static("/scripts", "examples/web/public/scripts") e.Static("/scripts", "examples/web/public/scripts")

BIN
examples/web/public/favicon.ico Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -1,11 +1,15 @@
<!DOCTYPE html> <!doctype html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Echo</title> <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> </head>
<body> <body>
Echo! <h1>Echo!</h1>
<script src="/scripts/main.js"></script> <script src="/scripts/main.js"></script>
</body> </body>
</html> </html>

View File

@ -36,8 +36,12 @@ func (t *Template) Render(w io.Writer, name string, data interface{}) *echo.HTTP
return nil 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 { func createUser(c *echo.Context) *echo.HTTPError {
@ -74,13 +78,16 @@ func main() {
s := stats.New() s := stats.New()
e.Use(s.Handler) e.Use(s.Handler)
// Route // Route
e.Get("/stats", func(c *echo.Context) { e.Get("/stats", func(c *echo.Context) *echo.HTTPError {
c.JSON(http.StatusOK, s.Data()) return c.JSON(http.StatusOK, s.Data())
}) })
// Serve index file // Serve index file
e.Index("public/index.html") e.Index("public/index.html")
// Serve favicon
e.Favicon("public/favicon.ico")
// Serve static files // Serve static files
e.Static("/scripts", "public/scripts") e.Static("/scripts", "public/scripts")
@ -109,19 +116,21 @@ func main() {
// Group with parent middleware // Group with parent middleware
a := e.Group("/admin") a := e.Group("/admin")
a.Use(func(c *echo.Context) { a.Use(func(c *echo.Context) *echo.HTTPError {
// Security middleware // Security middleware
return nil
}) })
a.Get("", func(c *echo.Context) { a.Get("", func(c *echo.Context) *echo.HTTPError {
c.String(http.StatusOK, "Welcome admin!") return c.String(http.StatusOK, "Welcome admin!")
}) })
// Group with no parent middleware // 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 // Security middleware
return nil
}) })
g.Get("", func(c *echo.Context) { g.Get("", func(c *echo.Context) *echo.HTTPError {
c.String(http.StatusOK, "Your files!") return c.String(http.StatusOK, "Your files!")
}) })
// Start server // Start server

View File

@ -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{ return &node{
typ: t, typ: t,
label: pfx[0], label: pre[0],
prefix: pfx, prefix: pre,
parent: p, parent: p,
children: c, children: c,
handler: h, handler: h,