1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-07 20:17:05 +00:00

Update to 8.2.5 | Say Hello to Controllers. Read HISTORY.md

Former-commit-id: 70f8619440497d132362da86c5187bcc57f8687b
This commit is contained in:
kataras
2017-08-13 21:58:34 +03:00
parent 2786ca1960
commit 8cd07719a6
32 changed files with 1302 additions and 152 deletions

View File

@@ -13,7 +13,6 @@ It doesn't always contain the "best ways" but it does cover each important featu
- [Tutorial: Online Visitors](tutorial/online-visitors/main.go)
- [Tutorial: URL Shortener using BoltDB](https://medium.com/@kataras/a-url-shortener-service-using-go-iris-and-bolt-4182f0b00ae7)
- [Tutorial: How to turn your Android Device into a fully featured Web Server (**MUST**)](https://twitter.com/ThePracticalDev/status/892022594031017988)
- [Tutorial: Controllers from scratch (**Coming soon as built'n feature, probably at v8.3**)](tutorial/mvc)
### HTTP Listening
@@ -83,6 +82,7 @@ Navigate through examples for a better understanding.
- [Overview](routing/overview/main.go)
- [Basic](routing/basic/main.go)
- [Controllers](routing/mvc)
- [Custom HTTP Errors](routing/http-errors/main.go)
- [Dynamic Path](routing/dynamic-path/main.go)
* [root level wildcard path](routing/dynamic-path/root-wildcard/main.go)

View File

@@ -1,4 +1,4 @@
// +build go.1.8
// +build !go1.9
package main

View File

@@ -0,0 +1,18 @@
// +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!"
}

View File

@@ -0,0 +1,18 @@
// +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!"
}

View File

@@ -0,0 +1,62 @@
// +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 registed 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() {}
*/

View File

@@ -0,0 +1,81 @@
// +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 registed 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() {}
*/

View File

@@ -0,0 +1,25 @@
// +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"))
}

View File

@@ -0,0 +1,28 @@
// +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"))
}

View File

@@ -0,0 +1,18 @@
<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>

View File

@@ -0,0 +1,99 @@
# Controllers from scratch
This example folder shows how I started to develop
the Controller idea inside the Iris web framework itself.
Now it's built'n feature and can be used as:
```go
// +build go1.9
// file main.go
package main
import (
"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("/user/{userid:int}", NewUserController(db))
// http://localhost:8080/
// http://localhost:8080/user/42
app.Run(iris.Addr(":8080"))
}
```
```go
// +build go1.9
// file user_controller.go
package main
import (
"time"
"github.com/kataras/iris/_examples/routing/mvc/persistence"
"github.com/kataras/iris"
)
// User is our user example controller.
type UserController struct {
iris.Controller
// 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 &UserController{
CreatedAt: time.Now(),
Title: "User page",
DB: db,
}
}
// Get serves using the User controller when HTTP Method is "GET".
func (c *UserController) 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 registed 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() {}
*/
```
Example can be found at: [_examples/routing/mvc](https://github.com/kataras/iris/tree/master/_examples/routing/mvc).

View File

@@ -3,7 +3,6 @@ package controllers
import (
"reflect"
"strings"
// "unsafe"
"github.com/kataras/iris"
"github.com/kataras/iris/context"
@@ -75,20 +74,14 @@ func RegisterController(app *iris.Application, path string, c interface{}) {
valF := val.Field(i)
// catch persistence data by tags, i.e:
// MyData string `iris:"persistence"`
// DB *DB `iris:"persistence"`
if t, ok := f.Tag.Lookup("iris"); ok {
if t == "persistence" {
persistenceFields[i] = reflect.ValueOf(val.Field(i).Interface())
persistenceFields[i] = reflect.ValueOf(valF.Interface())
continue
}
}
// catch persistence data by pointer, i.e:
// DB *Database
if f.Type.Kind() == reflect.Ptr {
if !valF.IsNil() {
persistenceFields[i] = reflect.ValueOf(val.Field(i).Interface())
}
}
}
}

View File

@@ -3,24 +3,20 @@ package controllers
import (
"time"
"github.com/kataras/iris/_examples/tutorial/mvc/persistence"
"github.com/kataras/iris/_examples/tutorial/mvc-from-scratch/persistence"
)
// User is our user example controller.
type User struct {
Controller
// all fields with pointers(*)
// that are not nil
// and all fields with
// that are tagged with iris:"persistence"`
// are being persistence and kept
// between the requests, meaning that
// they will not be reset-ed on each new request,
// 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
CreatedAt time.Time `iris:"persistence"`
Title string `iris:"persistence"`
DB *persistence.Database `iris:"persistence"`
}
func NewUserController(db *persistence.Database) *User {

View File

@@ -1,8 +1,8 @@
package main
import (
"github.com/kataras/iris/_examples/tutorial/mvc/controllers"
"github.com/kataras/iris/_examples/tutorial/mvc/persistence"
"github.com/kataras/iris/_examples/tutorial/mvc-from-scratch/controllers"
"github.com/kataras/iris/_examples/tutorial/mvc-from-scratch/persistence"
"github.com/kataras/iris"
)
@@ -18,7 +18,7 @@ func main() {
controllers.RegisterController(app, "/user/{userid:int}",
controllers.NewUserController(db))
// http://localhost/
// http://localhost:8080/
// http://localhost:8080/user/42
app.Run(iris.Addr(":8080"))
}

View File

@@ -0,0 +1,15 @@
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
}

View File

@@ -0,0 +1,10 @@
package persistence
// Database is our imaginary storage.
type Database struct {
Connstring string
}
func OpenDatabase(connstring string) *Database {
return &Database{Connstring: connstring}
}

View File

@@ -0,0 +1,11 @@
<html>
<head>
<title>{{.title}}</title>
</head>
<body>
<h1>{{.message}}</h1>
</body>
</html>

View File

@@ -58,7 +58,7 @@ func main() {
})
// http://localhost:8080
// http://localhost/redirect/my-page1
// http://localhost:8080/redirect/my-page1
app.Run(iris.Addr(":8080"))
}