Github:https://github.com/kataras/iris
官方地址:https://iris-go.com/
安装
发布版本记录:https://github.com/kataras/iris/releases
$ go get -u github.com/kataras/iris/v12
使用go mod 方式安装最新版本的
go get github.com/kataras/iris/v12@latest
HelloWorld
创建hello.go
package mainimport "github.com/kataras/iris/v12"func main() {app := iris.Default()app.Get("/ping", func(c iris.Context) {c.JSON(iris.Map{"message":"pong"})})app.Run(iris.Addr(":8080"))}
编译hello.go
$ go run hello.goNow listening on: http://0.0.0.0:8080Application started. Press CMD+C to shut down.
curl
$ curl -X GET "http://127.0.0.1:8080/ping"{"message":"pong"}
Query 参数
package mainimport "github.com/kataras/iris/v12"func main() {app:= iris.Default()app.Get("/hello", func(c iris.Context) {firstName:= c.URLParamDefault("firstname", "guest")lastName := c.URLParam("lastname")c.Writef("Hello %s %s\n",firstName,lastName)})app.Run(iris.Addr(":8080"))}
curl 请求
curl -X GET 'http://127.0.0.1:8080/hello?firstname=Jane&lastname=Doe'Hello Jane Doe
Form
package mainimport "github.com/kataras/iris/v12"func main() {app := iris.Default()app.Post("/form", func(ctx iris.Context) {id := ctx.URLParam("id")page := ctx.URLParamInt32Default("page", 0)name := ctx.FormValue("name")message := ctx.FormValue("message")ctx.JSON(iris.Map{"id":id,"page":page,"name":name,"message":message})})app.Run(iris.Addr(":8080"))}
$ curl -X POST 'http://127.0.0.1:8080/form?id=123&page=1' -F 'name=tony' -F 'message=hello'{"id":"123","message":"hello","name":"tony","page":1}
Group
package mainimport "github.com/kataras/iris/v12"func main() {app := iris.Default()v1 := app.Party("api/v1"){v1.Post("/login",groupHandle)}v2 := app.Party("api/v2"){v2.Post("/login",groupHandle)}app.Run(iris.Addr(":8080"))}func groupHandle(c iris.Context) {addr := c.RemoteAddr()uri := c.FullRequestURI()path := c.Path()method := c.Method()c.JSON(iris.Map{"addr":addr,"uri":uri,"path":path,"method":method})}
curl模拟请求测试
curl -X POST 'http://127.0.0.1:8080/api/v1/login'{"addr":"127.0.0.1","method":"POST","path":"/api/v1/login","uri":"curl -X POST 'http://127.0.0.1:8080/api/v2/login'{"addr":"127.0.0.1","method":"POST","path":"/api/v2/login","uri":"http://127.0.0.1:8080/api/v2/login"}
MVC
package mainimport ("github.com/baxiang/go-note/iris/mvc/hello-world/controller""github.com/kataras/iris/v12""github.com/kataras/iris/v12/mvc")func main() {app := iris.Default()mvc.New(app).Handle(&controller.HelloController{})// http://localhost:8080// http://localhost:8080/ping// http://localhost:8080/hello// http://localhost:8080/custom_pathapp.Run(iris.Addr(":8080"))}
创建controller
package controllerimport ("github.com/kataras/iris/v12""github.com/kataras/iris/v12/mvc")type HelloController struct {}// Get serves// Method: GET// Resource: http://localhost:8080func (c *HelloController) Get() mvc.Result {return mvc.Response{ContentType: "text/html",Text: "<h1>Welcome</h1>",}}// GetPing serves// Method: GET// Resource: http://localhost:8080/pingfunc (c *HelloController) GetPing() string {return "pong"}// GetHello serves// Method: GET// Resource: http://localhost:8080/hellofunc (c *HelloController) GetHello() interface{} {return map[string]string{"message": "Hello Iris!"}}func (c *HelloController) BeforeActivation(b mvc.BeforeActivation) {anyMiddlewareHere := func(ctx iris.Context) {ctx.Application().Logger().Warnf("Inside /custom_path")ctx.Next()}b.Handle("GET", "/custom_path", "CustomHandlerWithoutFollowingTheNamingGuide", anyMiddlewareHere)}func (c *HelloController) CustomHandlerWithoutFollowingTheNamingGuide() string {return "hello from the custom handler without following the naming guide"}
参考
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
