mirror of
https://github.com/kataras/iris.git
synced 2025-01-24 03:01:03 +01:00
5e4b63acb2
# FAQ ### Looking for free support? http://support.iris-go.com https://kataras.rocket.chat/channel/iris ### Looking for previous versions? https://github.com/kataras/iris#version ### Should I upgrade my Iris? Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready. > Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes. **How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`. For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework). ### About our new home page http://iris-go.com Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome! [Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him. The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please! Read more at https://github.com/kataras/iris/blob/master/HISTORY.md Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
87 lines
2.8 KiB
Go
87 lines
2.8 KiB
Go
package main
|
|
|
|
// In this package I'll show you how to override the existing Context's functions and methods.
|
|
// You can easly navigate to the advanced/custom-context to see how you can add new functions
|
|
// to your own context (need a custom handler).
|
|
//
|
|
// This way is far easier to understand and it's faster when you want to override existing methods:
|
|
import (
|
|
"reflect"
|
|
|
|
"github.com/kataras/iris"
|
|
"github.com/kataras/iris/context"
|
|
"github.com/kataras/iris/sessions"
|
|
"github.com/kataras/iris/view"
|
|
)
|
|
|
|
// 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.
|
|
}
|
|
|
|
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.
|
|
func (ctx *MyContext) Next() {
|
|
context.Next(ctx)
|
|
}
|
|
|
|
// Override any context's method you want...
|
|
// [...]
|
|
|
|
func (ctx *MyContext) HTML(htmlContents string) (int, error) {
|
|
ctx.Application().Log("Executing .HTML function from MyContext")
|
|
|
|
ctx.ContentType("text/html")
|
|
return ctx.WriteString(htmlContents)
|
|
}
|
|
|
|
func main() {
|
|
app := iris.New()
|
|
// Register a view engine on .html files inside the ./view/** directory.
|
|
viewEngine := view.HTML("./view", ".html")
|
|
app.AttachView(viewEngine)
|
|
|
|
// Register the session manager.
|
|
sessionManager := sessions.New(sessions.Config{
|
|
Cookie: "myappcookieid",
|
|
})
|
|
app.AttachSessionManager(sessionManager)
|
|
|
|
// The only one Required:
|
|
// 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:
|
|
Context: context.NewContext(app),
|
|
}
|
|
})
|
|
|
|
// register your route, as you normally do
|
|
app.Handle("GET", "/", recordWhichContetsJustForProofOfConcept, 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) {
|
|
firstname := ctx.Values().GetString("firstname")
|
|
|
|
ctx.ViewData("firstname", firstname)
|
|
ctx.Gzip(true)
|
|
|
|
ctx.View("hi.html")
|
|
})
|
|
|
|
app.Run(iris.Addr(":8080"))
|
|
}
|
|
|
|
// should always print "($PATH) Handler is executing from 'MyContext'"
|
|
func recordWhichContetsJustForProofOfConcept(ctx context.Context) {
|
|
ctx.Application().Log("(%s) Handler is executing from: '%s'", ctx.Path(), reflect.TypeOf(ctx).Elem().Name())
|
|
ctx.Next()
|
|
}
|