From b3bc8e71fbeaf465a50c3439daa9a0d961992d76 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Thu, 16 Feb 2017 22:19:44 +0200 Subject: [PATCH] Nothing special here Former-commit-id: af61c47c0462ec4b8d3699e3798c215a3feceb92 --- doc.go | 5 ++--- fs.go | 13 ++++++++++++- router.go | 6 ++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/doc.go b/doc.go index f45a1d92..2d47dbcb 100644 --- a/doc.go +++ b/doc.go @@ -385,6 +385,7 @@ Static Files Example code: + package main import ( @@ -392,8 +393,6 @@ Example code: "gopkg.in/kataras/iris.v6/adaptors/httprouter" ) - // if your ide cannot find the ./static folder try to build that program and after execute it - // or try to download & run this example via LiteIDE. func main() { app := iris.New() @@ -407,7 +406,7 @@ Example code: // This will serve the ./static/favicons/iris_favicon_32_32.ico to: 127.0.0.1:8080/favicon_32_32.ico app.Get("/", func(ctx *iris.Context) { - ctx.HTML(iris.StatusOK, "You should see the favicon now at the side of your browser, if not please refresh or clear the browser's cache.") + ctx.HTML(iris.StatusOK, "You should see the favicon now at the side of your browser.") }) app.Listen(":8080") diff --git a/fs.go b/fs.go index 20bd9292..a7a8f2f5 100644 --- a/fs.go +++ b/fs.go @@ -3,6 +3,7 @@ package iris import ( "net/http" "os" + "path/filepath" "strings" "sync" ) @@ -42,6 +43,16 @@ func toWebPath(systemPath string) string { return webpath } +// abs calls filepath.Abs but ignores the error and +// returns the original value if any error occured. +func abs(path string) string { + absPath, err := filepath.Abs(path) + if err != nil { + return path + } + return absPath +} + // NewStaticHandlerBuilder returns a new Handler which serves static files // supports gzip, no listing and much more // Note that, this static builder returns a Handler @@ -52,7 +63,7 @@ func toWebPath(systemPath string) string { // structure and want a fluent api to work on. func NewStaticHandlerBuilder(dir string) StaticHandlerBuilder { return &fsHandler{ - directory: http.Dir(dir), + directory: http.Dir(abs(dir)), // default route path is the same as the directory requestPath: toWebPath(dir), // enable strip path by-default diff --git a/router.go b/router.go index 52971347..fb9e33ce 100644 --- a/router.go +++ b/router.go @@ -82,7 +82,7 @@ Edit your main .go source file to adapt one of these routers and restart your ap // the rest of your source code... // ... - + app.Listen("%s") } @@ -405,7 +405,7 @@ func (router *Router) StaticServe(systemPath string, requestPath ...string) Rout // StaticContent serves bytes, memory cached, on the reqPath // a good example of this is how the websocket server uses that to auto-register the /iris-ws.js -func (router *Router) StaticContent(reqPath string, cType string, content []byte) RouteInfo { // func(string) because we use that on websockets +func (router *Router) StaticContent(reqPath string, cType string, content []byte) RouteInfo { modtime := time.Now() h := func(ctx *Context) { if err := ctx.SetClientCachedBody(StatusOK, content, cType, modtime); err != nil { @@ -504,6 +504,8 @@ func (router *Router) StaticEmbedded(requestPath string, vdir string, assetFn fu // // panics on error func (router *Router) Favicon(favPath string, requestPath ...string) RouteInfo { + favPath = abs(favPath) + f, err := os.Open(favPath) if err != nil { panic(errDirectoryFileNotFound.Format(favPath, err.Error()))