mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
❤️ awesome and unique features for end-developers are coming...
total refactor of the hero and mvc packages, see README#Next (it's not completed yet) Former-commit-id: b85ae99cbfe5965ba919c1e15cf4989e787982c0
This commit is contained in:
@@ -8,8 +8,11 @@ import (
|
||||
)
|
||||
|
||||
// See https://github.com/kataras/iris/issues/1449
|
||||
// for more details but in-short you can convert Iris MVC to gRPC methods by
|
||||
// binding the `context.Context` from `iris.Context.Request().Context()` and gRPC input and output data.
|
||||
// Iris automatically binds the standard "context" context.Context to `iris.Context.Request().Context()`
|
||||
// and any other structure that is not mapping to a registered dependency
|
||||
// as a payload depends on the request, e.g XML, YAML, Query, Form, JSON.
|
||||
//
|
||||
// Useful to use gRPC services as Iris controllers fast and without wrappers.
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
@@ -24,27 +27,7 @@ func main() {
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
|
||||
mvc.New(app).
|
||||
// Request-scope binding for context.Context-type controller's method or field.
|
||||
// (or import github.com/kataras/iris/v12/hero and hero.Register(...))
|
||||
Register(func(ctx iris.Context) context.Context {
|
||||
return ctx.Request().Context()
|
||||
}).
|
||||
// Bind loginRequest.
|
||||
// Register(func(ctx iris.Context) loginRequest {
|
||||
// var req loginRequest
|
||||
// ctx.ReadJSON(&req)
|
||||
// return req
|
||||
// }).
|
||||
// OR
|
||||
// Bind any other structure or pointer to a structure from request's
|
||||
// XML
|
||||
// YAML
|
||||
// Query
|
||||
// Form
|
||||
// JSON (default, if not client's "Content-Type" specified otherwise)
|
||||
Register(mvc.AutoBinding).
|
||||
Handle(&myController{})
|
||||
mvc.New(app).Handle(&myController{})
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ func main() {
|
||||
type myController struct{}
|
||||
|
||||
func (m *myController) BeforeActivation(b mvc.BeforeActivation) {
|
||||
// b.Dependencies().Add/Remove
|
||||
// b.Dependencies().Register
|
||||
// b.Router().Use/UseGlobal/Done // and any standard API call you already know
|
||||
|
||||
// 1-> Method
|
||||
|
||||
@@ -11,16 +11,14 @@ import (
|
||||
|
||||
// VisitController handles the root route.
|
||||
type VisitController struct {
|
||||
// the current request session,
|
||||
// its initialization happens by the dependency function that we've added to the `visitApp`.
|
||||
// the current request session, automatically binded.
|
||||
Session *sessions.Session
|
||||
|
||||
// A time.time which is binded from the MVC,
|
||||
// order of binded fields doesn't matter.
|
||||
// A time.time which is binded from the MVC application manually.
|
||||
StartTime time.Time
|
||||
}
|
||||
|
||||
// Get handles
|
||||
// Get handles index
|
||||
// Method: GET
|
||||
// Path: http://localhost:8080
|
||||
func (c *VisitController) Get() string {
|
||||
@@ -36,8 +34,9 @@ func (c *VisitController) Get() string {
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
sess := sessions.New(sessions.Config{Cookie: "mysession_cookie_name"})
|
||||
app.Use(sess.Handler())
|
||||
|
||||
visitApp := mvc.New(app.Party("/"))
|
||||
visitApp := mvc.New(app)
|
||||
// bind the current *session.Session, which is required, to the `VisitController.Session`
|
||||
// and the time.Now() to the `VisitController.StartTime`.
|
||||
visitApp.Register(
|
||||
@@ -50,7 +49,7 @@ func newApp() *iris.Application {
|
||||
// If dependencies are registered without field or function's input arguments as
|
||||
// consumers then those dependencies are being ignored before the server ran,
|
||||
// so you can bind many dependecies and use them in different controllers.
|
||||
sess.Start,
|
||||
// sess.Start, // However after version 12.2 sessions are automatically binded.
|
||||
time.Now(),
|
||||
)
|
||||
visitApp.Handle(new(VisitController))
|
||||
|
||||
Reference in New Issue
Block a user