💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
File: routing/http-errors/main.go ~~~ package main import ( "github.com/kataras/iris" ) func main() { app := iris.New() app.OnErrorCode(iris.StatusInternalServerError, func(ctx iris.Context) { ctx.HTML("Message: <b>" + ctx.Values().GetString("message") + "</b>") }) app.Get("/", func(ctx iris.Context) { ctx.HTML(`Click <a href="/my500">here</a> to fire the 500 status code`) }) app.Get("/my500", func(ctx iris.Context) { ctx.Values().Set("message", "this is the error message") ctx.StatusCode(500) }) app.Get("/u/{firstname:alphabetical}", func(ctx iris.Context) { ctx.Writef("Hello %s", ctx.Params().Get("firstname")) }) app.Run(iris.Addr(":8080")) } Writing A Middleware ~~~ File: routing/writing-a-middleware/globally/main.go ~~~ package main import "github.com/kataras/iris" func main() { app := iris.New() // 将“before”处理程序注册为将要执行的第一个处理程序 // 在所有域名的路线上. //或者使用`UseGlobal`注册一个将跨子域触发的中间件. // app.Use(before) // 将“after”处理程序注册为将要执行的最后一个处理程序 // 在所有域名的路线'处理程序之后. // // 或者使用`DoneGlobal`来追加将被全局触发的处理程序. // app.Done(after) // 注册我们的路线. app.Get("/", indexHandler) app.Get("/contact", contactHandler) // 这些电话的顺序无关紧要,`UseGlobal`和`DoneGlobal` // 适用于现有路线和未来路线 // // 请记住: the `Use` and `Done` 适用于当前党派及其, //所以,如果我们使用了 `app.Use/Don`e 所以,如果我们使用了 // 它会像UseGlobal/DoneGlobal 在这种情况下,因为 `app` 是个 root party. // // 有关更多信息,请参阅`app.Party / PartyFunc` app.UseGlobal(before) app.DoneGlobal(after) app.Run(iris.Addr(":8080")) } func before(ctx iris.Context) { shareInformation := "this is a sharable information between handlers" requestPath := ctx.Path() println("Before the indexHandler or contactHandler: " + requestPath) ctx.Values().Set("info", shareInformation) ctx.Next() } func after(ctx iris.Context) { println("After the indexHandler or contactHandler") } func indexHandler(ctx iris.Context) { println("Inside indexHandler") // 从中获取信息 "before" handler. info := ctx.Values().GetString("info") // 写一些东西给客户作为回应. ctx.HTML("<h1>Response</h1>") ctx.HTML("<br/> Info: " + info) ctx.Next() // xecute通过注册的“after”处理程序 `DoneGlobal`. } func contactHandler(ctx iris.Context) { println("Inside contactHandler") // 写一些东西给客户作为回应. ctx.HTML("<h1>Contact</h1>") ctx.Next() // 执行通过`DoneGlobal`注册的“after”处理程序。 } ~~~ File: routing/writing-a-middleware/per-route/main.go ~~~ package main import "github.com/kataras/iris" func main() { app := iris.New() // 要么 app.Use(before) 和 app.Done(after). app.Get("/", before, mainHandler, after) // 使用注册中间件(前置处理程序)到所有方及其将要注册的子项 // 后. // // (`app`是根子所以那些使用和完成处理程序将在任何地方注册) app.Use(func(ctx iris.Context) { println(`在党的路线及其子女之前, 但这不适用于 '/' 路线 因为它是在中间件之前注册的,所以订单很重要.`) ctx.Next() }) app.Done(func(ctx iris.Context) { println("如果前一个处理程序调用,则总是最后执行 `ctx.Next()`,这是相反的 `.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", "这是家信息, ip: "+ctx.RemoteAddr()) ctx.Next() // call the done handlers. }) child := app.Party("/child") child.Get("/", func(ctx iris.Context) { ctx.Writef(`这是 localhost:8080/child route. 注册到父方的所有使用和完成处理程序, 这里也适用.`) ctx.Next() // 调用完成的处理程序. }) app.Run(iris.Addr(":8080")) } func before(ctx iris.Context) { shareInformation := "this is a sharable information between handlers" requestPath := ctx.Path() println("Before the mainHandler: " + requestPath) ctx.Values().Set("info", shareInformation) ctx.Next() //执行下一个处理程序,在本例中是主处理程序. } func after(ctx iris.Context) { println("After the mainHandler") } func mainHandler(ctx iris.Context) { println("Inside mainHandler") // t从中获取信息 "before" handler. info := ctx.Values().GetString("info") // 一些东西给客户作为回应 ctx.HTML("<h1>Response</h1>") ctx.HTML("<br/> Info: " + info) ctx.Next() // 执行“之后” "after". } ~~~