From ba05b6e58e9b88bc45efe00823ab4be4487254be Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Wed, 13 May 2015 15:20:09 -0700 Subject: [PATCH] Added favicon api Signed-off-by: Vishal Rana --- echo.go | 5 +++++ echo_test.go | 11 +++++++++++ examples/web/public/favicon.ico | Bin 0 -> 1150 bytes examples/web/public/index.html | 12 ++++++++---- examples/web/server.go | 29 +++++++++++++++++++---------- router.go | 6 +++--- 6 files changed, 46 insertions(+), 17 deletions(-) create mode 100755 examples/web/public/favicon.ico diff --git a/echo.go b/echo.go index ed68c824..5a74c0bb 100644 --- a/echo.go +++ b/echo.go @@ -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) diff --git a/echo_test.go b/echo_test.go index 839f0248..0ee49837 100644 --- a/echo_test.go +++ b/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") diff --git a/examples/web/public/favicon.ico b/examples/web/public/favicon.ico new file mode 100755 index 0000000000000000000000000000000000000000..d939ddca12aa14a2fa691e082b14436f18719f6b GIT binary patch literal 1150 zcmZQzU<5(|0R|wcz>vYhz#zuJz@P!dKp~(AL>x#lFaYI*xgi+L$0S%eIXJ|4d3e=$ zc=$9q*f})W*w{3AxVg1?dHFTCxVU71TG%nQ!9^s*C1e}r6xHtm@dI&j$+JSjqKD-a z)E)x)52d8#mN7Cgr~_3q!*xIzLZXr&7N@L&MvuI*?teKY?f(+eioe7p3-i zipeOb?_p!-lme;)8iNY#y}}th{6lyR9m5aFStkFNx6S&mWS;@V>Hn3T^ZqMa;8qR43_WoB7nfzbbr}e*9NZ)^>jyEcB5D~<+3`sc1>sGYKwp`v5j zgo^Gt|D*C+{(Hxj{P#>K{U4Ck^gq98>i?R)d1sp^&J}2!I2S|z%ALmmsy{F`jln-ARk5h5aYt9* zj16{yc^j0iVpbHkPBBa^s?yiCa9}le@x`haBp8{T%@C22>snl1`#mEv>3=|I+E;tu z_=n{M)i2vSC%z1cPj!k)%D|~VFfN0^CoYq}ptP + - + + Echo + + + - Echo! - +

Echo!

+ diff --git a/examples/web/server.go b/examples/web/server.go index 7affe32a..a1aaf570 100644 --- a/examples/web/server.go +++ b/examples/web/server.go @@ -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,13 +78,16 @@ 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") @@ -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 diff --git a/router.go b/router.go index 7e68c8f9..75f10e47 100644 --- a/router.go +++ b/router.go @@ -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,