()
| 5 | ) |
| 6 | |
| 7 | func newApp() *iris.Application { |
| 8 | app := iris.New() |
| 9 | app.Logger().SetLevel("debug") |
| 10 | |
| 11 | // registers a custom handler for 404 not found http (error) status code, |
| 12 | // fires when route not found or manually by ctx.StatusCode(iris.StatusNotFound). |
| 13 | app.OnErrorCode(iris.StatusNotFound, notFoundHandler) |
| 14 | |
| 15 | // GET -> HTTP Method |
| 16 | // / -> Path |
| 17 | // func(ctx iris.Context) -> The route's handler. |
| 18 | // |
| 19 | // Third receiver should contains the route's handler(s), they are executed by order. |
| 20 | app.Handle("GET", "/", func(ctx iris.Context) { |
| 21 | ctx.HTML("Hello from " + ctx.Path()) // Hello from / |
| 22 | }) |
| 23 | |
| 24 | app.Get("/home", func(ctx iris.Context) { |
| 25 | ctx.Writef(`Same as app.Handle("GET", "/", [...])`) |
| 26 | }) |
| 27 | |
| 28 | // Different path parameters types in the same path. |
| 29 | // Note that: fallback should registered first e.g. {path} {string}, |
| 30 | // because the handler on this case is executing from last to top. |
| 31 | app.Get("/u/{p:path}", func(ctx iris.Context) { |
| 32 | ctx.Writef(":string, :int, :uint, :alphabetical and :path in the same path pattern.") |
| 33 | }) |
| 34 | |
| 35 | app.Get("/u/{username:string}", func(ctx iris.Context) { |
| 36 | ctx.Writef("before username (string), current route name: %s\n", ctx.RouteName()) |
| 37 | ctx.Next() |
| 38 | }, func(ctx iris.Context) { |
| 39 | ctx.Writef("username (string): %s", ctx.Params().Get("username")) |
| 40 | }) |
| 41 | |
| 42 | app.Get("/u/{firstname:alphabetical}", func(ctx iris.Context) { |
| 43 | ctx.Writef("before firstname (alphabetical), current route name: %s\n", ctx.RouteName()) |
| 44 | ctx.Next() |
| 45 | }, func(ctx iris.Context) { |
| 46 | ctx.Writef("firstname (alphabetical): %s", ctx.Params().Get("firstname")) |
| 47 | }) |
| 48 | |
| 49 | app.Get("/u/{id:int}", func(ctx iris.Context) { |
| 50 | ctx.Writef("before id (int), current route name: %s\n", ctx.RouteName()) |
| 51 | ctx.Next() |
| 52 | }, func(ctx iris.Context) { |
| 53 | ctx.Writef("id (int): %d", ctx.Params().GetIntDefault("id", 0)) |
| 54 | }) |
| 55 | |
| 56 | app.Get("/u/{uid:uint}", func(ctx iris.Context) { |
| 57 | ctx.Writef("before uid (uint), current route name: %s\n", ctx.RouteName()) |
| 58 | ctx.Next() |
| 59 | }, func(ctx iris.Context) { |
| 60 | ctx.Writef("uid (uint): %d", ctx.Params().GetUintDefault("uid", 0)) |
| 61 | }) |
| 62 | |
| 63 | /* |
| 64 | /u/some/path/here maps to :path |
searching dependent graphs…