Nothing special here

Former-commit-id: af61c47c0462ec4b8d3699e3798c215a3feceb92
This commit is contained in:
Gerasimos (Makis) Maropoulos 2017-02-16 22:19:44 +02:00
parent 805ba56863
commit b3bc8e71fb
3 changed files with 18 additions and 6 deletions

5
doc.go
View File

@ -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")

13
fs.go
View File

@ -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

View File

@ -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()))