From 10e8b9c964a0980d12883229c9c6bef78ac04e45 Mon Sep 17 00:00:00 2001 From: Makis Maropoulos Date: Tue, 21 Jun 2016 16:07:49 +0300 Subject: [PATCH] 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. --- README.md | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0fef13de..bb6aedaf 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ the fastest web framework for Go while providing robust set of features for building modern web applications. -[![Benchmark Wizzard Processing Time Horizontal Graph](https://raw.githubusercontent.com/iris-contrib/website/cf71811e6acb2f9bf1e715e25660392bf090b923/assets/benchmark_horizontal_transparent.png)](#benchmarks) +[![Benchmark Wizzard Processing Time Horizontal Graph](https://raw.githubusercontent.com/iris-contrib/website/cf71811e6acb2f9bf1e715e25660392bf090b923/assets/benchmark_horizontal_transparent.png)](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).