Static files

Serve files or directories, use the correct for your case, if you don't know which one, just use the 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:
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)

Context static file serving

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

Serve static individual file

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 (

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.ServeFile(path, false) // make this true to use gzip compression


The previous example is almost identical with:

StaticWeb(requestPath string, systemPath string) RouteNameFunc
func main() {
  iris.StaticWeb("/public", "./mywebpage")
  // Serves all files inside this directory to the GET&HEAD route:
  // use iris.StaticHandler for more options like gzip.
func main() {
  // Serves all files inside filesystem path ./static/mywebpage to the GET&HEAD route:

Disabling caching

Caching can be disabled by setting's StaticCacheDuration to time.Duration(1) before calling any of the named functions. Setting StaticCacheDuration to time.Duration(0) will reset the cache time to 10 seconds.


Imagine that we have a folder named static which has subfolder favicons and this folder contains a favicon, for example iris_favicon_32_32.ico.

// ./main.go
package main

import ""

func main() {

    iris.Get("/", func(ctx *iris.Context) {
        ctx.HTML(iris.StatusOK, "You should see the favicon now at the side of your browser.")



results matching ""

    No results matching ""