mirror of
https://github.com/kataras/iris.git
synced 2025-12-31 16:57:04 +00:00
remove the old 'mvc' folder - examples are not changed yet - add the 'di' package inside the mvc2 package - which will be renamed to 'mvc' on the next commit - new mvc.Application and some dublications removed - The new version will be version 9 because it will contain breaking changes (not to the end-developer's controllers but to the API they register them) - get ready for 'Christmas Edition' for believers
Former-commit-id: c7114233dee90ee308c0a3e77ec2ad0c361094b8
This commit is contained in:
83
mvc2/ideas/1/main.go
Normal file
83
mvc2/ideas/1/main.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/sessions"
|
||||
|
||||
mvc "github.com/kataras/iris/mvc2"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
mvc.New(app.Party("/todo")).Configure(TodoApp)
|
||||
// no let's have a clear "mvc" package without any conversions and type aliases,
|
||||
// it's one extra import path for a whole new world, it worths it.
|
||||
//
|
||||
// app.UseMVC(app.Party("/todo")).Configure(func(app *iris.MVCApplication))
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
func TodoApp(app *mvc.Application) {
|
||||
// You can use normal middlewares at MVC apps of course.
|
||||
app.Router.Use(func(ctx iris.Context) {
|
||||
ctx.Application().Logger().Infof("Path: %s", ctx.Path())
|
||||
ctx.Next()
|
||||
})
|
||||
|
||||
// Add dependencies which will be binding to the controller(s),
|
||||
// can be either a function which accepts an iris.Context and returns a single value (dynamic binding)
|
||||
// or a static struct value (service).
|
||||
app.AddDependencies(
|
||||
mvc.Session(sessions.New(sessions.Config{})),
|
||||
&prefixedLogger{prefix: "DEV"},
|
||||
)
|
||||
|
||||
app.Register(new(TodoController))
|
||||
|
||||
// All dependencies of the parent *mvc.Application
|
||||
// are cloned to that new child, thefore it has access to the same session as well.
|
||||
app.NewChild(app.Router.Party("/sub")).
|
||||
Register(new(TodoSubController))
|
||||
}
|
||||
|
||||
// If controller's fields (or even its functions) expecting an interface
|
||||
// but a struct value is binded then it will check if that struct value implements
|
||||
// the interface and if true then it will bind it as expected.
|
||||
|
||||
type LoggerService interface {
|
||||
Log(string)
|
||||
}
|
||||
|
||||
type prefixedLogger struct {
|
||||
prefix string
|
||||
}
|
||||
|
||||
func (s *prefixedLogger) Log(msg string) {
|
||||
fmt.Printf("%s: %s\n", s.prefix, msg)
|
||||
}
|
||||
|
||||
type TodoController struct {
|
||||
Logger LoggerService
|
||||
|
||||
Session *sessions.Session
|
||||
}
|
||||
|
||||
func (c *TodoController) Get() string {
|
||||
count := c.Session.Increment("count", 1)
|
||||
|
||||
body := fmt.Sprintf("Hello from TodoController\nTotal visits from you: %d", count)
|
||||
c.Logger.Log(body)
|
||||
return body
|
||||
}
|
||||
|
||||
type TodoSubController struct {
|
||||
Session *sessions.Session
|
||||
}
|
||||
|
||||
func (c *TodoSubController) Get() string {
|
||||
count, _ := c.Session.GetIntDefault("count", 1)
|
||||
return fmt.Sprintf("Hello from TodoSubController.\nRead-only visits count: %d", count)
|
||||
}
|
||||
Reference in New Issue
Block a user