iris/_examples/file-server/basic/main.go
Gerasimos (Makis) Maropoulos 0f113dfcda (#1554) Add support for all common compressions (write and read)
- Remove the context.Context interface and export the *context, the iris.Context now points to the pointer\nSupport compression and rate limiting in the FileServer\nBit of code organisation


Former-commit-id: ad1c61bf968059510c6be9e7f2cceec7da70ba17
2020-07-10 23:21:09 +03:00

55 lines
1.8 KiB
Go

package main
import (
"github.com/kataras/iris/v12"
)
func newApp() *iris.Application {
app := iris.New()
app.Favicon("./assets/favicon.ico")
// first parameter is the request path
// second is the system directory
//
// app.HandleDir("/css", "./assets/css")
// app.HandleDir("/js", "./assets/js")
v1 := app.Party("/v1")
v1.HandleDir("/static", "./assets", iris.DirOptions{
// Defaults to "/index.html", if request path is ending with **/*/$IndexName
// then it redirects to **/*(/) which another handler is handling it,
// that another handler, called index handler, is auto-registered by the framework
// if end developer does not managed to handle it by hand.
IndexName: "/index.html",
// When files should served under compression.
Compress: false,
// List the files inside the current requested directory if `IndexName` not found.
ShowList: false,
// If `ShowList` is true then this function will be used instead of the default one to show the list of files of a current requested directory(dir).
// DirList: func(ctx iris.Context, dirName string, dir http.File) error { ... }
//
// Optional validator that loops through each requested resource.
// AssetValidator: func(ctx iris.Context, name string) bool { ... }
})
// You can also register any index handler manually, order of registration does not matter:
// v1.Get("/static", [...custom middleware...], func(ctx iris.Context) {
// [...custom code...]
// ctx.ServeFile("./assets/index.html")
// })
// http://localhost:8080/v1/static
// http://localhost:8080/v1/static/css/main.css
// http://localhost:8080/v1/static/js/jquery-2.1.1.js
// http://localhost:8080/v1/static/favicon.ico
return app
}
func main() {
app := newApp()
app.Logger().SetLevel("debug")
app.Listen(":8080")
}