本文最后更新于 2020年05月27日 已经是 1100天前了 ,文章可能具有时效性,若有错误或已失效,请在下方留言。
注意
请不要转载本文章内容,这只是我在学习过程中所记录的一些笔记!
IRIS框架
The fastest community-driven web framework for Go. gRPC, Automatic HTTPS with Public Domain, MVC, Sessions, Caching, Versioning API, Problem API, Websocket, Dependency Injection and more. Fully compatible with the standard library and 3rd-party middleware packages.
Go最快的社区驱动的Web框架。gRPC,带有公共域的自动HTTPS,MVC,会话,缓存,版本控制API,问题API,Websocket,依赖注入等等。与标准库和第三方中间件软件包完全兼容。
开始
安装
$ go get github.com/kataras/iris/[email protected]
引用
import "github.com/kataras/iris/v12"
入门
package main
import "github.com/kataras/iris/v12"
func main() {
app := iris.Default()
app.Use(myMiddleware)
app.Handle("GET", "/ping", func(ctx iris.Context) {
ctx.JSON(iris.Map{"message": "pong"})
})
// Listens and serves incoming http requests
// on http://localhost:8080.
app.Listen(":8080")
}
func myMiddleware(ctx iris.Context) {
ctx.Application().Logger().Infof("Runs before %s", ctx.Path())
ctx.Next()
}
这样我们就写了一个最简单的Web服务器
你可以访问localhost:8080/ping
来查看你的服务器是否正常
模板的使用
package main
import "github.com/kataras/iris/v12"
func main() {
app := iris.New()
// Load all templates from the "./views" folder
// where extension is ".html" and parse them
// using the standard <code>html/template</code> package.
app.RegisterView(iris.HTML("./views", ".html"))
// 当然你这里也可以不用默认的模板引擎,也可以使用Python中Django使用的模板语法 iris.Django()
// Method: GET
// Resource: http://localhost:8080
app.Get("/", func(ctx iris.Context) {
// Bind: {{.message}} with "Hello world!"
ctx.ViewData("message", "Hello world!")
// Render template file: ./views/hello.html
ctx.View("hello.html")
})
// Method: GET
// Resource: http://localhost:8080/user/42
//
// Need to use a custom regexp instead?
// Easy;
// Just mark the parameter's type to 'string'
// which accepts anything and make use of
// its <code>regexp</code> macro function, i.e:
// app.Get("/user/{id:string regexp(^[0-9]+$)}")
app.Get("/user/{id:uint64}", func(ctx iris.Context) {
userID, _ := ctx.Params().GetUint64("id")
ctx.Writef("User ID: %d", userID)
})
// Start the server using a network address.
app.Listen(":8080")
}
<!-- file: ./views/hello.html -->
<html>
<head>
<title>Hello Page</title>
</head>
<body>
<h1>{{.message}}</h1>
</body>
</html>
中间件的使用
什么是中间件?你可以把他当作一个防火墙,他可以判断请求是否安全,不安全可以直接丢弃请求,他可以判断你是否登录 等等
package main
import "github.com/kataras/iris/v12"
func main() {
app := iris.New()
// or app.Use(before) and app.Done(after).
app.Get("/", before, mainHandler, after)
app.Listen(":8080")
}
func before(ctx iris.Context) {
shareInformation := "this is a sharable information between handlers"
requestPath := ctx.Path()
println("Before the mainHandler: " + requestPath)
ctx.Values().Set("info", shareInformation)
ctx.Next() // execute the next handler, in this case the main one.
}
func after(ctx iris.Context) {
println("After the mainHandler")
}
func mainHandler(ctx iris.Context) {
println("Inside mainHandler")
// take the info from the "before" handler.
info := ctx.Values().GetString("info")
// write something to the client as a response.
ctx.HTML("<h1>Response</h1>")
ctx.HTML("<br/> Info: " + info)
ctx.Next() // execute the "after".
}
路由及访问控制
路由
func main(){
...
user.Handle(new(RootController))
...
}
type RootController struct {
Ctx iris.Context
}
//这里就是对 “/” 的get请求
func (c *RootController) Get() mvc.Result {
return mvc.View{
Name: "index.html",
Data: iris.Map{"key": csrf.Token(c.Ctx)},
}
}
//这里就是对 “/login” 的get请求
func (c *RootController) GetLogin() mvc.Result {
...
}
未完待续
评论