Go之路(三十七):iris框架初识
iris框架以及gorm
首先,先上官方文档
gorm:https://jasperxu.github.io/gorm-zh/associations.html#bt
iris:https://studyiris.com/example/iris.html
先看一个小例子:
package main
import (
"example/demo2/web/controller"
"fmt"
"github.com/kataras/iris"
"github.com/kataras/iris/mvc"
)
func main() {
app := iris.New()
app.Use(func(ctx iris.Context) {
fmt.Println("this is the usual func")
})
app.AllowMethods(iris.MethodGet)
mvc.New(app.Party("/user")).Handle(new(controller.UserController))
app.Run(iris.Addr(":8080"))
}
前面不用说, app.use的意思是满足context.Handler的每一次请求都会执行,括号内的函数,下面是官方解释。
context.Handler 类型 每一个请求都会先执行此方法 app.Use(context.Handler)
AllowMethods的意思是允许的请求的方式。
关键是这个后面这个mvc,里面的app.party代表绑定一个路由集,里面的自定义的方法需要按照下面特定的方法访问。
If `mvc.New(app.Party("/user")).Handle(new(user.Controller))`
- `func(*Controller) Get()` - `GET:/user` , 照常.
- `func(*Controller) Post()` - `POST:/user`, 照常.
- `func(*Controller) GetLogin()` - `GET:/user/login`
- `func(*Controller) PostLogin()` - `POST:/user/login`
- `func(*Controller) GetProfileFollowers()` - `GET:/user/profile/followers`
- `func(*Controller) PostProfileFollowers()` - `POST:/user/profile/followers`
- `func(*Controller) GetBy(id int64)` - `GET:/user/{param:long}`
- `func(*Controller) PostBy(id int64)` - `POST:/user/{param:long}`
If `mvc.New(app.Party("/profile")).Handle(new(profile.Controller))`
- `func(*Controller) GetBy(username string)` - `GET:/profile/{param:string}`
If `mvc.New(app.Party("/assets")).Handle(new(file.Controller))`
- `func(*Controller) GetByWildard(path string)` - `GET:/assets/{param:path}`
If `mvc.New(app.Party("/equality")).Handle(new(profile.Equality))`
- `func(*Controller) GetBy(is bool)` - `GET:/equality/{param:boolean}`
- `func(*Controller) GetByOtherBy(is bool, otherID int64)` - `GET:/equality/{paramfirst:boolean}/other/{paramsecond:long}`

浙公网安备 33010602011771号