1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-24 05:17:03 +00:00
Former-commit-id: 75b855bee9216c28ce8e1ff46aee467766c37f23
This commit is contained in:
kataras
2017-08-13 01:47:19 +03:00
parent 960ddb9f72
commit 39a24fb7cb
8 changed files with 111 additions and 27 deletions

View File

@@ -20,8 +20,16 @@ type MyContext struct {
var _ context.Context = &MyContext{} // optionally: validate on compile-time if MyContext implements context.Context.
// Optional Part 2:
// The only one important if you will override the Context with an embedded context.Context inside it.
// The only one important if you will override the Context
// with an embedded context.Context inside it.
// Required in order to run the handlers via this "*MyContext".
func (ctx *MyContext) Do(handlers context.Handlers) {
context.Do(ctx, handlers)
}
// The second one important if you will override the Context
// with an embedded context.Context inside it.
// Required in order to run the chain of handlers via this "*MyContext".
func (ctx *MyContext) Next() {
context.Next(ctx)
}
@@ -40,11 +48,9 @@ func main() {
app := iris.New()
// app.Logger().SetLevel("debug")
// Register a view engine on .html files inside the ./view/** directory.
app.RegisterView(iris.HTML("./view", ".html"))
// The only one Required:
// here is how you define how your own context will be created and acquired from the iris' generic context pool.
// here is how you define how your own context will
// be created and acquired from the iris' generic context pool.
app.ContextPool.Attach(func() context.Context {
return &MyContext{
// Optional Part 3:
@@ -52,6 +58,9 @@ func main() {
}
})
// Register a view engine on .html files inside the ./view/** directory.
app.RegisterView(iris.HTML("./view", ".html"))
// register your route, as you normally do
app.Handle("GET", "/", recordWhichContextJustForProofOfConcept, func(ctx context.Context) {
// use the context's overridden HTML method.

View File

@@ -14,12 +14,13 @@ func main() {
// / -> because of app.Get("/", ...)
// /other/anything/here -> because of app.Get("/other/{paramother:path}", ...)
// /other2/anything/here -> because of app.Get("/other2/{paramothersecond:path}", ...)
// /other2/static -> because of app.Get("/other2/static", ...)
// /other2/static2 -> because of app.Get("/other2/static", ...)
//
// It isn't conflicts with the rest of the routes, without routing performance cost!
//
// i.e /something/here/that/cannot/be/found/by/other/registered/routes/order/not/matters
app.Get("/{p:path}", h)
// app.Get("/static/{p:path}", staticWildcardH)
// this will handle only GET /
app.Get("/", staticPath)
@@ -36,7 +37,7 @@ func main() {
app.Get("/other2/{paramothersecond:path}", other2)
// this will handle only GET "/other2/static"
app.Get("/other2/static", staticPath)
app.Get("/other2/static2", staticPathOther2)
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
}
@@ -46,6 +47,11 @@ func h(ctx context.Context) {
ctx.WriteString(param)
}
func staticWildcardH(ctx context.Context) {
param := ctx.Params().Get("p")
ctx.WriteString("from staticWildcardH: param=" + param)
}
func other(ctx context.Context) {
param := ctx.Params().Get("paramother")
ctx.Writef("from other: %s", param)
@@ -57,5 +63,9 @@ func other2(ctx context.Context) {
}
func staticPath(ctx context.Context) {
ctx.Writef("from the static path: %s", ctx.Path())
ctx.Writef("from the static path(/): %s", ctx.Path())
}
func staticPathOther2(ctx context.Context) {
ctx.Writef("from the static path(/other2/static2): %s", ctx.Path())
}