静态文件
服务文件和目录时,应使用 `StaticWeb(reqPath string, systemPath string)`。
// Favicon serves static favicon
// accepts 2 parameters, second is optional
// favPath (string), declare the system directory path of the __.ico
// requestPath (string), it's the route's path, by default this is the "/favicon.ico" because some browsers tries to get this by default first,
// you can declare your own path if you have more than one favicon (desktop, mobile and so on)
//
// this func will add a route for you which will static serve the /yuorpath/yourfile.ico to the /yourfile.ico (nothing special that you can't handle by yourself)
// Note that you have to call it on every favicon you have to serve automatically (dekstop, mobile and so on)
//
// panics on error
Favicon(favPath string, requestPath ...string) RouteNameFunc
// StaticHandler returns a new Handler which serves static files
StaticHandler(reqPath string, systemPath string, showList bool, enableGzip bool) HandlerFunc
// StaticWeb same as Static but if index.html e
// xists and request uri is '/' then display the index.html's contents
// accepts three parameters
// first parameter is the request url path (string)
// second parameter is the system directory (string)
StaticWeb(reqPath string, systemPath string) RouteNameFunc
// StaticEmbedded used when files are distrubuted inside the app executable, using go-bindata mostly
// First parameter is the request path, the path which the files in the vdir will be served to, for example "/static"
// Second parameter is the (virtual) directory path, for example "./assets"
// Third parameter is the Asset function
// Forth parameter is the AssetNames function
//
// For more take a look at the
// example: https://github.com/iris-contrib/examples/tree/master/static_files_embedded
StaticEmbedded(requestPath string, vdir string, assetFn func(name string) ([]byte, error), namesFn func() []string) RouteNameFunc
// 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
StaticContent(reqPath string, cType string, content []byte) RouteNameFunc
// StaticServe serves a directory as web resource
// it's the simpliest form of the Static* functions
// Almost same usage as StaticWeb
// accepts only one required parameter which is the systemPath
// (the same path will be used to register the GET&HEAD routes)
// if the second parameter is empty, otherwise the requestPath is the second parameter
// it uses gzip compression (compression on each request, no file cache)
StaticServe(systemPath string, requestPath ...string)
iris.Static("/public", "./static/assets/", 1)
//-> /public/assets/favicon.ico
iris.StaticFS("/ftp", "./myfiles/public", 1)
iris.StaticWeb("/","./my_static_html_website", 1)
StaticServe(systemPath string, requestPath ...string)
上下文语境中的静态文件服务
// ServeFile serves a view file, to send a file
// to the client you should use the SendFile(serverfilename,clientfilename)
// receives two parameters
// filename/path (string)
// gzipCompression (bool)
//
// You can define your own "Content-Type" header also, after this function call
ServeFile(filename string, gzip bool) error
单个静态文件
iris.Get("/txt", func(ctx *iris.Context) {
ctx.ServeFile("./myfolder/staticfile.txt", false)
}
For example if you want manual serve static individual files dynamically you can do something like that:
例如:如果你想手工地提供动态的独立的静态文件服务,你可以这么做:
package main
import (
"strings"
"github.com/kataras/iris"
"github.com/kataras/iris/utils"
)
func main() {
iris.Get("/*file", func(ctx *iris.Context) {
requestpath := ctx.Param("file")
path := strings.Replace(requestpath, "/", utils.PathSeperator, -1)
if !utils.DirectoryExists(path) {
ctx.NotFound()
return
}
ctx.ServeFile(path, false) // make this true to use gzip compression
})
iris.Listen(":8080")
}
上个示例几乎等同于:
StaticWeb(requestPath string, systemPath string) RouteNameFunc
func main() {
iris.StaticWeb("/public", "./mywebpage")
// Serves all files inside this directory to the GET&HEAD route: 0.0.0.0:8080/public
// use iris.StaticHandler for more options like gzip.
iris.Listen(":8080")
}
func main() {
iris.StaticServe("./static/mywebpage","/webpage")
// Serves all files inside filesystem path ./static/mywebpage to the GET&HEAD route: 0.0.0.0:8080/webpage
iris.Listen(":8080")
}
禁用缓存
可在 github.com/kataras/iris/config
中 把 StaticCacheDuration
设置为 time.Duration(1)
以在调用任何命名函数之前禁用缓存,把 StaticCacheDuration
设置为 time.Duration(0)
将会把缓存时间重置为 10秒。
Favicon
假如我们有个 static
目录,里面又有个 favicons
目录,favicons
目录中有个文件 iris_favicon_32_32.ico
:
// ./main.go
package main
import "github.com/kataras/iris"
func main() {
iris.Favicon("./static/favicons/iris_favicon_32_32.ico")
iris.Get("/", func(ctx *iris.Context) {
ctx.HTML(iris.StatusOK, "You should see the favicon now at the side of your browser.")
})
iris.Listen(":8080")
}