Fan of the MVC Architectural Pattern?

Former-commit-id: c6e425e8a4b150ea78456b2db88367093c164a38
This commit is contained in:
kataras 2017-08-28 11:40:25 +03:00
parent 42b123975c
commit e2600450f7
7 changed files with 87 additions and 47 deletions

View File

@ -65,6 +65,7 @@ Iris may have reached version 8, but we're not stopping there. We have many feat
* [Dockerize](https://github.com/iris-contrib/cloud-native-go)
* [Community & Support](#-community)
* [Blogs](https://iris-go.com/v8/blogs)
- [Iris Go vs .NET Core Kestrel in terms of HTTP performance](https://hackernoon.com/iris-go-vs-net-core-kestrel-in-terms-of-http-performance-806195dc93d5)
- [Go vs .NET Core in terms of HTTP performance](https://medium.com/@kataras/go-vs-net-core-in-terms-of-http-performance-7535a61b67b8)
- [Iris, a modular web framework](https://medium.com/@corebreaker/iris-web-cd684b4685c7)
- [Deploying a Iris Golang app in hasura](https://docs.hasura.io/0.14/tutorials/deploying-a-go-iris-app.html)
@ -84,33 +85,8 @@ $ go get -u github.com/kataras/iris
> _iris_ takes advantage of the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature. You get truly reproducible builds, as this method guards against upstream renames and deletes.
```go
// file: main.go
package main
import "github.com/kataras/iris"
func main() {
app := iris.New()
// Load all templates from the "./templates" folder
// where extension is ".html" and parse them
// using the standard `html/template` package.
app.RegisterView(iris.HTML("./templates", ".html"))
// Method: GET
// Resource: http://localhost:8080
app.Get("/", func(ctx iris.Context) {
// Bind: {{.message}} with "Hello world!"
ctx.ViewData("message", "Hello world!")
// Render template file: ./templates/hello.html
ctx.View("hello.html")
})
// Start the server using a network address.
app.Run(iris.Addr(":8080"))
}
```
```html
<!-- file: ./templates/hello.html -->
<!-- file: ./views/hello.html -->
<html>
<head>
<title>Hello Page</title>
@ -121,6 +97,75 @@ func main() {
</html>
```
```go
// file: main.go
package main
import "github.com/kataras/iris"
func main() {
app := iris.New()
// Load all templates from the "./views" folder
// where extension is ".html" and parse them
// using the standard `html/template` package.
app.RegisterView(iris.HTML("./views", ".html"))
// Method: GET
// Resource: http://localhost:8080
app.Get("/", func(ctx iris.Context) {
// Bind: {{.message}} with "Hello world!"
ctx.ViewData("message", "Hello world!")
// Render template file: ./views/hello.html
ctx.View("hello.html")
})
// Method: GET
// Resource: http://localhost:8080/user/42
app.Get("/user/{id:long}", func(ctx iris.Context) {
userID, _ := ctx.Params().GetInt64("id")
ctx.Writef("User ID: %d", userID)
})
// Start the server using a network address.
app.Run(iris.Addr(":8080"))
}
```
<details>
<summary>Fan of the MVC Architectural Pattern? Click here</summary>
```go
package main
import "github.com/kataras/iris"
func main() {
app := iris.New()
app.RegisterView(iris.HTML("./views", ".html"))
app.Controller("/", new(Controller))
app.Run(iris.Addr(":8080"))
}
type Controller struct {
iris.Controller
}
// Method: GET
// Resource: http://localhost:8080
func (c *Controller) Get() {
c.Data["message"] = "Hello world!"
c.Tmpl = "hello.html"
}
// Method: GET
// Resource: http://localhost:8080/user/42
func (c *Controller) GetBy(id int64) {
c.Ctx.Writef("User ID: %d", id)
}
```
</details>
```sh
$ go run main.go
> Now listening on: http://localhost:8080

View File

@ -4,7 +4,6 @@ import (
"sync"
"github.com/kataras/iris"
"github.com/kataras/iris/mvc"
)
func main() {
@ -52,7 +51,7 @@ var myDB = &DB{
// ProfileController our example user controller which controls
// the paths of "/profile" "/profile/{id:int}" and "/profile/me".
type ProfileController struct {
mvc.Controller // IMPORTANT
iris.Controller // IMPORTANT
User UserModel `iris:"model"`
// we will bind it but you can also tag it with`iris:"persistence"`

View File

@ -2,7 +2,6 @@ package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/mvc"
"github.com/kataras/iris/middleware/logger"
"github.com/kataras/iris/middleware/recover"
@ -48,9 +47,8 @@ func main() {
// IndexController serves the "/".
type IndexController struct {
// if you build with go1.9 you can omit the import of mvc package
// and just use `iris.Controller` instead.
mvc.Controller
// if you build with go1.8 you have to use the mvc package, `mvc.Controller` instead.
iris.Controller
}
// Get serves
@ -62,7 +60,7 @@ func (c *IndexController) Get() {
// PingController serves the "/ping".
type PingController struct {
mvc.Controller
iris.Controller
}
// Get serves
@ -74,7 +72,7 @@ func (c *PingController) Get() {
// HelloController serves the "/hello".
type HelloController struct {
mvc.Controller
iris.Controller
}
type myJSONData struct {

View File

@ -6,7 +6,6 @@ import (
"strings"
"github.com/kataras/iris"
"github.com/kataras/iris/mvc"
)
// paths
@ -22,7 +21,7 @@ const (
// AuthController is the user authentication controller, a custom shared controller.
type AuthController struct {
mvc.SessionController
iris.SessionController
Source *DataSource
User Model `iris:"model"`

View File

@ -4,15 +4,12 @@ import (
"time"
"github.com/kataras/iris"
"github.com/kataras/iris/mvc"
"github.com/kataras/iris/sessions"
)
type VisitController struct {
// if you build with go1.9 you can omit the import of mvc package
// and just use `iris.Controller` instead.
mvc.SessionController
iris.SessionController
StartTime time.Time
}

View File

@ -122,4 +122,8 @@ type (
// the "github.com/kataras/iris/mvc"
// package for machines that have not installed go1.9 yet.
Controller = mvc.Controller
// SessionController is a simple `Controller` implementation
// which requires a binded session manager in order to give
// direct access to the current client's session via its `Session` field.
SessionController = mvc.SessionController
)

12
doc.go
View File

@ -682,7 +682,6 @@ Example Code
import (
"github.com/kataras/iris"
"github.com/kataras/iris/mvc"
"github.com/kataras/iris/middleware/logger"
"github.com/kataras/iris/middleware/recover"
@ -720,9 +719,8 @@ Example Code
// IndexController serves the "/".
type IndexController struct {
// if you build with go1.9 you can omit the import of mvc package
// and just use `iris.Controller` instead.
mvc.Controller
// if you build with go1.8 you have to use the mvc package, `mvc.Controller` instead.
iris.Controller
}
// Get serves
@ -734,7 +732,7 @@ Example Code
// PingController serves the "/ping".
type PingController struct {
mvc.Controller
iris.Controller
}
// Get serves
@ -746,7 +744,7 @@ Example Code
// HelloController serves the "/hello".
type HelloController struct {
mvc.Controller
iris.Controller
}
// Get serves
@ -811,7 +809,7 @@ useful to call middlewares or when many methods use the same collection of data.
Optional `EndRequest(ctx)` function to perform any finalization after any method executed.
Inheritance, recursively, see for example our `mvc.SessionController`, it has the `mvc.Controller` as an embedded field
Inheritance, recursively, see for example our `mvc.SessionController/iris.SessionController`, it has the `mvc.Controller/iris.Controller` as an embedded field
and it adds its logic to its `BeginRequest`. Source file: https://github.com/kataras/iris/blob/master/mvc/session_controller.go.
Read access to the current route via the `Route` field.