mirror of
https://github.com/kataras/iris.git
synced 2025-03-13 21:36:28 +01:00
Update readme
Update first example giving more information with the first eye-view. also on graph's image's click now it goes to the external benchmark suite project.
This commit is contained in:
parent
f841bd4157
commit
10e8b9c964
74
README.md
74
README.md
|
@ -20,7 +20,7 @@
|
|||
|
||||
the fastest web framework for Go while providing robust set of features for building modern web applications.
|
||||
|
||||
[](#benchmarks)
|
||||
[](https://github.com/smallnest/go-web-framework-benchmark)
|
||||
|
||||
```sh
|
||||
$ cat main.go
|
||||
|
@ -28,17 +28,79 @@ $ cat main.go
|
|||
```go
|
||||
package main
|
||||
|
||||
import "github.com/kataras/iris"
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/middleware/logger"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
// render JSON
|
||||
iris.Get("/hi_json", func(c *iris.Context) {
|
||||
c.JSON(200, iris.Map{
|
||||
"Name": "Iris",
|
||||
"Age": 2,
|
||||
c.JSON(iris.StatusOK, iris.Map{
|
||||
"Name": "Iris",
|
||||
"Born": "13 March 2016",
|
||||
"Stars": 2380,
|
||||
})
|
||||
})
|
||||
iris.Listen(":8080")
|
||||
|
||||
// logger middleware
|
||||
log := logger.New(iris.Logger)
|
||||
|
||||
// group routes by path prefix and middleware sharing
|
||||
group := iris.Party("/users", log)
|
||||
{
|
||||
group.Get("/", func(c *iris.Context) {
|
||||
// return all users or render a template
|
||||
})
|
||||
|
||||
group.Get("/:userID", func(c *iris.Context) {
|
||||
// return a user with ID `c.Param("userID")`
|
||||
})
|
||||
|
||||
group.Delete("/:userID", func(c *iris.Context) {
|
||||
//delete a user with ID `c.Param("userID")`
|
||||
})
|
||||
}
|
||||
|
||||
// using static subdomains
|
||||
subdomain := iris.Party("account.", log, myAuthMiddleware).Layout("layouts/subdomain.html")
|
||||
{
|
||||
subdomain.Get("/", func(c *iris.Context) {
|
||||
// render a template with a context of {username: "myusername"}
|
||||
c.Render("account/index.html", iris.Map{ // we can also use a struct
|
||||
"username": c.Session().GetString("username"),
|
||||
})
|
||||
})
|
||||
|
||||
subdomain.Post("/edit", func(c *iris.Context) {
|
||||
//...
|
||||
})
|
||||
}
|
||||
|
||||
// using dynamic subdomains
|
||||
dynamicSub := iris.Party("*.")
|
||||
{
|
||||
// middleware on route, called before the final handler
|
||||
dynamicSub.Get("/", log, func(c *iris.Context) {
|
||||
c.Write("Hello from subdomain: %s", c.Subdomain())
|
||||
})
|
||||
}
|
||||
|
||||
iris.Listen("127.0.0.1:8080")
|
||||
}
|
||||
|
||||
// using high level sessions inside a custom middleware
|
||||
func myAuthMiddleware(c *iris.Context) {
|
||||
s := c.Session()
|
||||
|
||||
if s.GetString("username") == "myusername" && s.GetString("passowrd") == "mypassword" {
|
||||
c.Next()
|
||||
} else {
|
||||
c.EmitError(iris.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
> Learn about [configuration](https://kataras.gitbooks.io/iris/content/configuration.html) and [render](https://kataras.gitbooks.io/iris/content/render.html).
|
||||
|
|
Loading…
Reference in New Issue
Block a user