Github:https://github.com/kataras/iris
官方地址:https://iris-go.com/

安装

发布版本记录:https://github.com/kataras/iris/releases

  1. $ go get -u github.com/kataras/iris/v12

使用go mod 方式安装最新版本的

  1. go get github.com/kataras/iris/v12@latest

HelloWorld

创建hello.go

  1. package main
  2. import "github.com/kataras/iris/v12"
  3. func main() {
  4. app := iris.Default()
  5. app.Get("/ping", func(c iris.Context) {
  6. c.JSON(iris.Map{"message":"pong"})
  7. })
  8. app.Run(iris.Addr(":8080"))
  9. }

编译hello.go

  1. $ go run hello.go
  2. Now listening on: http://0.0.0.0:8080
  3. Application started. Press CMD+C to shut down.

curl

  1. $ curl -X GET "http://127.0.0.1:8080/ping"
  2. {"message":"pong"}

Query 参数

  1. package main
  2. import "github.com/kataras/iris/v12"
  3. func main() {
  4. app:= iris.Default()
  5. app.Get("/hello", func(c iris.Context) {
  6. firstName:= c.URLParamDefault("firstname", "guest")
  7. lastName := c.URLParam("lastname")
  8. c.Writef("Hello %s %s\n",firstName,lastName)
  9. })
  10. app.Run(iris.Addr(":8080"))
  11. }

curl 请求

  1. curl -X GET 'http://127.0.0.1:8080/hello?firstname=Jane&lastname=Doe'
  2. Hello Jane Doe

Form

  1. package main
  2. import "github.com/kataras/iris/v12"
  3. func main() {
  4. app := iris.Default()
  5. app.Post("/form", func(ctx iris.Context) {
  6. id := ctx.URLParam("id")
  7. page := ctx.URLParamInt32Default("page", 0)
  8. name := ctx.FormValue("name")
  9. message := ctx.FormValue("message")
  10. ctx.JSON(iris.Map{"id":id,"page":page,"name":name,"message":message})
  11. })
  12. app.Run(iris.Addr(":8080"))
  13. }
  1. $ curl -X POST 'http://127.0.0.1:8080/form?id=123&page=1' -F 'name=tony' -F 'message=hello'
  2. {"id":"123","message":"hello","name":"tony","page":1}

Group

  1. package main
  2. import "github.com/kataras/iris/v12"
  3. func main() {
  4. app := iris.Default()
  5. v1 := app.Party("api/v1")
  6. {
  7. v1.Post("/login",groupHandle)
  8. }
  9. v2 := app.Party("api/v2")
  10. {
  11. v2.Post("/login",groupHandle)
  12. }
  13. app.Run(iris.Addr(":8080"))
  14. }
  15. func groupHandle(c iris.Context) {
  16. addr := c.RemoteAddr()
  17. uri := c.FullRequestURI()
  18. path := c.Path()
  19. method := c.Method()
  20. c.JSON(iris.Map{"addr":addr,"uri":uri,"path":path,"method":method})
  21. }

curl模拟请求测试

  1. curl -X POST 'http://127.0.0.1:8080/api/v1/login'
  2. {"addr":"127.0.0.1","method":"POST","path":"/api/v1/login","uri":"
  3. curl -X POST 'http://127.0.0.1:8080/api/v2/login'
  4. {"addr":"127.0.0.1","method":"POST","path":"/api/v2/login","uri":"http://127.0.0.1:8080/api/v2/login"}

MVC

  1. package main
  2. import (
  3. "github.com/baxiang/go-note/iris/mvc/hello-world/controller"
  4. "github.com/kataras/iris/v12"
  5. "github.com/kataras/iris/v12/mvc"
  6. )
  7. func main() {
  8. app := iris.Default()
  9. mvc.New(app).Handle(&controller.HelloController{})
  10. // http://localhost:8080
  11. // http://localhost:8080/ping
  12. // http://localhost:8080/hello
  13. // http://localhost:8080/custom_path
  14. app.Run(iris.Addr(":8080"))
  15. }

创建controller

  1. package controller
  2. import (
  3. "github.com/kataras/iris/v12"
  4. "github.com/kataras/iris/v12/mvc"
  5. )
  6. type HelloController struct {
  7. }
  8. // Get serves
  9. // Method: GET
  10. // Resource: http://localhost:8080
  11. func (c *HelloController) Get() mvc.Result {
  12. return mvc.Response{
  13. ContentType: "text/html",
  14. Text: "<h1>Welcome</h1>",
  15. }
  16. }
  17. // GetPing serves
  18. // Method: GET
  19. // Resource: http://localhost:8080/ping
  20. func (c *HelloController) GetPing() string {
  21. return "pong"
  22. }
  23. // GetHello serves
  24. // Method: GET
  25. // Resource: http://localhost:8080/hello
  26. func (c *HelloController) GetHello() interface{} {
  27. return map[string]string{"message": "Hello Iris!"}
  28. }
  29. func (c *HelloController) BeforeActivation(b mvc.BeforeActivation) {
  30. anyMiddlewareHere := func(ctx iris.Context) {
  31. ctx.Application().Logger().Warnf("Inside /custom_path")
  32. ctx.Next()
  33. }
  34. b.Handle("GET", "/custom_path", "CustomHandlerWithoutFollowingTheNamingGuide", anyMiddlewareHere)
  35. }
  36. func (c *HelloController) CustomHandlerWithoutFollowingTheNamingGuide() string {
  37. return "hello from the custom handler without following the naming guide"
  38. }

参考

https://github.com/kataras/iris/tree/master/_examples
https://hackernoon.com/a-todo-mvc-application-using-iris-and-vue-js-5019ff870064
https://learnku.com/docs/iris-start