mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 18:07:01 +00:00
Update to version 10.2.0. Read: https://github.com/kataras/iris/blob/master/HISTORY.md#th-08-february-2018--v1020
Former-commit-id: 2eb94ec0f565b80790784ac55af024429384b3d3
This commit is contained in:
@@ -4,7 +4,40 @@ import "github.com/kataras/iris"
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
// or app.Use(before) and app.Done(after).
|
||||
app.Get("/", before, mainHandler, after)
|
||||
|
||||
// Use registers a middleware(prepend handlers) to all party's, and its children that will be registered
|
||||
// after.
|
||||
//
|
||||
// (`app` is the root children so those use and done handlers will be registered everywhere)
|
||||
app.Use(func(ctx iris.Context) {
|
||||
println(`before the party's routes and its children,
|
||||
but this is not applied to the '/' route
|
||||
because it's registered before the middleware, order matters.`)
|
||||
ctx.Next()
|
||||
})
|
||||
|
||||
app.Done(func(ctx iris.Context) {
|
||||
println("this is executed always last, if the previous handler calls the `ctx.Next()`, it's the reverse of `.Use`")
|
||||
message := ctx.Values().GetString("message")
|
||||
println("message: " + message)
|
||||
})
|
||||
|
||||
app.Get("/home", func(ctx iris.Context) {
|
||||
ctx.HTML("<h1> Home </h1>")
|
||||
ctx.Values().Set("message", "this is the home message, ip: "+ctx.RemoteAddr())
|
||||
ctx.Next() // call the done handlers.
|
||||
})
|
||||
|
||||
child := app.Party("/child")
|
||||
child.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef(`this is the localhost:8080/child route.
|
||||
All Use and Done handlers that are registered to the parent party,
|
||||
are applied here as well.`)
|
||||
ctx.Next() // call the done handlers.
|
||||
})
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user