Esempio n. 1
0
File: iris.go Progetto: toksea/iris
// StaticHandler returns a Handler to serve static system directory
// Accepts 5 parameters
//
// first is the systemPath (string)
// Path to the root directory to serve files from.
//
// second is the  stripSlashes (int) level
// * stripSlashes = 0, original path: "/foo/bar", result: "/foo/bar"
// * stripSlashes = 1, original path: "/foo/bar", result: "/bar"
// * stripSlashes = 2, original path: "/foo/bar", result: ""
//
// third is the compress (bool)
// Transparently compresses responses if set to true.
//
// The server tries minimizing CPU usage by caching compressed files.
// It adds fasthttp.FSCompressedFileSuffix suffix to the original file name and
// tries saving the resulting compressed file under the new file name.
// So it is advisable to give the server write access to Root
// and to all inner folders in order to minimze CPU usage when serving
// compressed responses.
//
// fourth is the generateIndexPages (bool)
// Index pages for directories without files matching IndexNames
// are automatically generated if set.
//
// Directory index generation may be quite slow for directories
// with many files (more than 1K), so it is discouraged enabling
// index pages' generation for such directories.
//
// fifth is the indexNames ([]string)
// List of index file names to try opening during directory access.
//
// For example:
//
//     * index.html
//     * index.htm
//     * my-super-index.xml
//
func (api *muxAPI) StaticHandler(systemPath string, stripSlashes int, compress bool, generateIndexPages bool, indexNames []string) HandlerFunc {
	if indexNames == nil {
		indexNames = []string{}
	}
	fs := &fasthttp.FS{
		// Path to directory to serve.
		Root:       systemPath,
		IndexNames: indexNames,
		// Generate index pages if client requests directory contents.
		GenerateIndexPages: generateIndexPages,

		// Enable transparent compression to save network traffic.
		Compress:             compress,
		CacheDuration:        config.StaticCacheDuration,
		CompressedFileSuffix: config.CompressedFileSuffix,
	}

	if stripSlashes > 0 {
		fs.PathRewrite = fasthttp.NewPathSlashesStripper(stripSlashes)
	}

	// Create request handler for serving static files.
	h := fs.NewRequestHandler()
	return HandlerFunc(func(ctx *Context) {
		h(ctx.RequestCtx)
		errCode := ctx.RequestCtx.Response.StatusCode()
		if errCode == StatusNotFound || errCode == StatusBadRequest || errCode == StatusInternalServerError {
			api.mux.fireError(errCode, ctx)
		}
		if ctx.pos < uint8(len(ctx.middleware))-1 {
			ctx.Next() // for any case
		}

	})
}
Esempio n. 2
0
func (r *Router) StaticPath(dir string) {

	//fileServer := http.FileServer(http.Dir(dir))

	//r.hr.GET("/"+dir+"/*filepath", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	//	w.Header().Set("Vary", "Accept-Encoding")
	//	w.Header().Set("Cache-Control", "public, max-age=7776000")
	//	r.URL.Path = p.ByName("filepath")
	//	fileServer.ServeHTTP(w, r)
	//})
	//r.Router.ServeFiles("/"+dir+"/*filepath", dir)

	path := "/" + dir + "/*filepath"
	//absFilePath, _ := filepath.Abs(dir)

	//println(absFilePath)

	fs := &fasthttp.FS{
		Root: dir,
		//IndexNames:         []string{"index.html"},
		GenerateIndexPages: false,
		Compress:           true,
		AcceptByteRange:    true,
	}
	prefix := path[:len(path)-10]
	stripSlashes := strings.Count(prefix, "/")
	fs.PathRewrite = fasthttp.NewPathSlashesStripper(stripSlashes)

	fsHandler := fs.NewRequestHandler()

	r.GET(path, func(ctx *fasthttp.RequestCtx, _ fasthttprouter.Params) {
		fsHandler(ctx)
	})
}