mirror of
https://github.com/kataras/iris.git
synced 2025-12-19 02:47:04 +00:00
New Community Board, have fun
Former-commit-id: 84bdd3893da5ad28e918e4dc32d2657d27b0ed32
This commit is contained in:
@@ -17,7 +17,7 @@ import (
|
||||
// Create your own custom Context, put any fields you wanna need.
|
||||
type MyContext struct {
|
||||
// Optional Part 1: embed (optional but required if you don't want to override all context's methods)
|
||||
context.Context // it's the internal/Context.go but you don't need to know it.
|
||||
context.Context // it's the context/context.go#context struct but you don't need to know it.
|
||||
}
|
||||
|
||||
var _ context.Context = &MyContext{} // optionally: validate on compile-time if MyContext implements context.Context.
|
||||
@@ -60,14 +60,14 @@ func main() {
|
||||
})
|
||||
|
||||
// register your route, as you normally do
|
||||
app.Handle("GET", "/", recordWhichContetsJustForProofOfConcept, func(ctx context.Context) {
|
||||
app.Handle("GET", "/", recordWhichContextJustForProofOfConcept, func(ctx context.Context) {
|
||||
// use the context's overridden HTML method.
|
||||
ctx.HTML("<h1> Hello from my custom context's HTML! </h1>")
|
||||
})
|
||||
|
||||
// this will be executed by the MyContext.Context
|
||||
// if MyContext is not directly define the View function by itself.
|
||||
app.Handle("GET", "/hi/{firstname:alphabetical}", recordWhichContetsJustForProofOfConcept, func(ctx context.Context) {
|
||||
app.Handle("GET", "/hi/{firstname:alphabetical}", recordWhichContextJustForProofOfConcept, func(ctx context.Context) {
|
||||
firstname := ctx.Values().GetString("firstname")
|
||||
|
||||
ctx.ViewData("firstname", firstname)
|
||||
@@ -80,7 +80,7 @@ func main() {
|
||||
}
|
||||
|
||||
// should always print "($PATH) Handler is executing from 'MyContext'"
|
||||
func recordWhichContetsJustForProofOfConcept(ctx context.Context) {
|
||||
func recordWhichContextJustForProofOfConcept(ctx context.Context) {
|
||||
ctx.Application().Log("(%s) Handler is executing from: '%s'", ctx.Path(), reflect.TypeOf(ctx).Elem().Name())
|
||||
ctx.Next()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user