mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 18:37:05 +00:00
Update to 8.3.0 | MVC Models and Bindings and fix of #723 , read HISTORY.md
Former-commit-id: d8f66d8d370c583a288333df2a14c6ee2dc56466
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
// +build !go1.9
|
||||
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/core/router"
|
||||
)
|
||||
|
||||
// Index is our index example controller.
|
||||
type Index struct {
|
||||
router.Controller
|
||||
}
|
||||
|
||||
func (c *Index) Get() {
|
||||
c.Tmpl = "index.html"
|
||||
c.Data["title"] = "Index page"
|
||||
c.Data["message"] = "Hello world!"
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
// +build go1.9
|
||||
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
// Index is our index example controller.
|
||||
type Index struct {
|
||||
iris.Controller
|
||||
}
|
||||
|
||||
func (c *Index) Get() {
|
||||
c.Tmpl = "index.html"
|
||||
c.Data["title"] = "Index page"
|
||||
c.Data["message"] = "Hello world!"
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
// +build !go1.9
|
||||
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/_examples/routing/mvc/persistence"
|
||||
|
||||
"github.com/kataras/iris/core/router"
|
||||
)
|
||||
|
||||
// User is our user example controller.
|
||||
type User struct {
|
||||
router.Controller
|
||||
|
||||
// All fields with pointers(*) that are not nil
|
||||
// and all fields that are tagged with iris:"persistence"`
|
||||
// are being persistence and kept between the different requests,
|
||||
// meaning that these data will not be reset-ed on each new request,
|
||||
// they will be the same for all requests.
|
||||
CreatedAt time.Time `iris:"persistence"`
|
||||
Title string `iris:"persistence"`
|
||||
DB *persistence.Database `iris:"persistence"`
|
||||
}
|
||||
|
||||
func NewUserController(db *persistence.Database) *User {
|
||||
return &User{
|
||||
CreatedAt: time.Now(),
|
||||
Title: "User page",
|
||||
DB: db,
|
||||
}
|
||||
}
|
||||
|
||||
// Get serves using the User controller when HTTP Method is "GET".
|
||||
func (c *User) Get() {
|
||||
c.Tmpl = "user/index.html"
|
||||
c.Data["title"] = c.Title
|
||||
c.Data["username"] = "kataras " + c.Params.Get("userid")
|
||||
c.Data["connstring"] = c.DB.Connstring
|
||||
c.Data["uptime"] = time.Now().Sub(c.CreatedAt).Seconds()
|
||||
}
|
||||
|
||||
/* Can use more than one, the factory will make sure
|
||||
that the correct http methods are being registered for this
|
||||
controller, uncommend these if you want:
|
||||
|
||||
func (c *User) Post() {}
|
||||
func (c *User) Put() {}
|
||||
func (c *User) Delete() {}
|
||||
func (c *User) Connect() {}
|
||||
func (c *User) Head() {}
|
||||
func (c *User) Patch() {}
|
||||
func (c *User) Options() {}
|
||||
func (c *User) Trace() {}
|
||||
*/
|
||||
|
||||
/*
|
||||
func (c *User) All() {}
|
||||
// OR
|
||||
func (c *User) Any() {}
|
||||
*/
|
||||
@@ -1,81 +0,0 @@
|
||||
// +build go1.9
|
||||
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/sessions"
|
||||
)
|
||||
|
||||
// User is our user example controller.
|
||||
type User struct {
|
||||
iris.Controller
|
||||
|
||||
// All fields with pointers(*) that are not nil
|
||||
// and all fields that are tagged with iris:"persistence"`
|
||||
// are being persistence and kept between the different requests,
|
||||
// meaning that these data will not be reset-ed on each new request,
|
||||
// they will be the same for all requests.
|
||||
CreatedAt time.Time `iris:"persistence"`
|
||||
Title string `iris:"persistence"`
|
||||
SessionManager *sessions.Sessions `iris:"persistence"`
|
||||
|
||||
Session *sessions.Session // not persistence
|
||||
}
|
||||
|
||||
func NewUserController(sess *sessions.Sessions) *User {
|
||||
return &User{
|
||||
SessionManager: sess,
|
||||
CreatedAt: time.Now(),
|
||||
Title: "User page",
|
||||
}
|
||||
}
|
||||
|
||||
// Init can be used as a custom function
|
||||
// to init the new instance of controller
|
||||
// that is created on each new request.
|
||||
//
|
||||
// Useful when more than one methods are using the same
|
||||
// request data.
|
||||
func (c *User) Init(ctx iris.Context) {
|
||||
c.Session = c.SessionManager.Start(ctx)
|
||||
// println("session id: " + c.Session.ID())
|
||||
}
|
||||
|
||||
// Get serves using the User controller when HTTP Method is "GET".
|
||||
func (c *User) Get() {
|
||||
c.Tmpl = "user/index.html"
|
||||
c.Data["title"] = c.Title
|
||||
c.Data["username"] = "kataras " + c.Params.Get("userid")
|
||||
c.Data["uptime"] = time.Now().Sub(c.CreatedAt).Seconds()
|
||||
|
||||
visits, err := c.Session.GetInt("visit_count")
|
||||
if err != nil {
|
||||
visits = 0
|
||||
}
|
||||
visits++
|
||||
c.Session.Set("visit_count", visits)
|
||||
c.Data["visit_count"] = visits
|
||||
}
|
||||
|
||||
/* Can use more than one, the factory will make sure
|
||||
that the correct http methods are being registered for this
|
||||
controller, uncommend these if you want:
|
||||
|
||||
func (c *User) Post() {}
|
||||
func (c *User) Put() {}
|
||||
func (c *User) Delete() {}
|
||||
func (c *User) Connect() {}
|
||||
func (c *User) Head() {}
|
||||
func (c *User) Patch() {}
|
||||
func (c *User) Options() {}
|
||||
func (c *User) Trace() {}
|
||||
*/
|
||||
|
||||
/*
|
||||
func (c *User) All() {}
|
||||
// OR
|
||||
func (c *User) Any() {}
|
||||
*/
|
||||
@@ -1,25 +0,0 @@
|
||||
// +build !go1.9
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/_examples/routing/mvc/controllers"
|
||||
"github.com/kataras/iris/_examples/routing/mvc/persistence"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.RegisterView(iris.HTML("./views", ".html"))
|
||||
|
||||
db := persistence.OpenDatabase("a fake db")
|
||||
|
||||
app.Controller("/", new(controllers.Index))
|
||||
|
||||
app.Controller("/user/{userid:int}", controllers.NewUserController(db))
|
||||
|
||||
// http://localhost:8080/
|
||||
// http://localhost:8080/user/42
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
// +build go1.9
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/_examples/routing/mvc/controllers"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/sessions"
|
||||
"github.com/kataras/iris/sessions/sessiondb/boltdb"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.RegisterView(iris.HTML("./views", ".html"))
|
||||
|
||||
sessionDb, _ := boltdb.New("./sessions/sessions.db", 0666, "users")
|
||||
sess := sessions.New(sessions.Config{Cookie: "sessionscookieid"})
|
||||
sess.UseDatabase(sessionDb.Async(true))
|
||||
|
||||
app.Controller("/", new(controllers.Index))
|
||||
|
||||
app.Controller("/user/{userid:int}", controllers.NewUserController(sess))
|
||||
|
||||
// http://localhost:8080/
|
||||
// http://localhost:8080/user/42
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// User is an example model.
|
||||
type User struct {
|
||||
ID int64
|
||||
Username string
|
||||
Firstname string
|
||||
Lastname string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package persistence
|
||||
|
||||
// Database is our imaginary storage.
|
||||
type Database struct {
|
||||
Connstring string
|
||||
}
|
||||
|
||||
func OpenDatabase(connstring string) *Database {
|
||||
return &Database{Connstring: connstring}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>{{.title}}</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>{{.message}}</h1>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -1,18 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>{{.title}}</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1> Hello {{.username}} </h1>
|
||||
|
||||
|
||||
All fields inside a controller that are pointers or they tagged as `iris:"persistence"` are marked as persistence data, meaning
|
||||
that they will not be reset-ed on each new request.
|
||||
<h3>Persistence data from *DB.Connstring: {{.connstring}} </h3>
|
||||
<h3>Persistence data from CreatedAt `iris:"persistence"`: {{.uptime}} seconds </h3>
|
||||
<h3>{{.visit_count}}
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user