Iris介绍

  • Iris以简单而强大的api而闻名。 除了Iris为您提供的低级访问权限。 Iris同样擅长MVC。 它是唯一一个拥有MVC架构模式丰富支持的Go Web框架,性能成本接近于零。
  • Iris为您提供构建面向服务的应用程序的结构。 用Iris构建微服务很容易。

Iris 框架的特性

  • 专注于高性能
  • 简单流畅的API
  • 高扩展性
  • 强大的路由和中间件生态系统
    • 使用iris独特的表达主义路径解释器构建RESTful API
    • 动态路径参数化或通配符路由与静态路由不冲突
    • 使用重定向选项从URL中删除尾部斜杠
    • 使用虚拟主机和子域名变得容易
    • 分组API和静态或甚至动态子域名
    • net / http和negroni-like处理程序通过iris.FromStd兼容
    • 针对任意Http请求错误 定义处理函数
    • 支持事务和回滚
    • 支持响应缓存
    • 使用简单的函数嵌入资源并与go-bindata 保持兼容
    • mvc
  • 上下文
    • 高度可扩展的试图渲染(目前支持markdown,json,xml,jsonp等等)
    • 正文绑定器和发送HTTP响应的便捷功能
    • 限制请求正文
    • 提供静态资源或嵌入式资产
    • 本地化i18N
    • 压缩(Gzip是内置的)
  • 身份验证
    • Basic Authentication
    • OAuth, OAuth2 (支持27个以上的热门网站)
    • JWT *服务器
    • 通过TLS提供服务时,自动安装和提供来自https://letsencrypt.org的证书
    • 默认为关闭状态
    • 在关闭,错误或中断事件时注册
    • 连接多个服务器,完全兼容 net/http#Server
  • 视图系统.支持五种模板隐隐 完全兼容 html/template
  • Websocket库,其API类似于socket.io [如果你愿意,你仍然可以使用你最喜欢的]
  • 热重启
  • Typescript集成 + Web IDE

入门使用

1
go get -u github.com/kataras/iris
1
2
3
4
5
6
7
8
9
10
package main
import "github.com/kataras/iris"

func main() {
// 创建app结构体对象
app := iris.New()
app.Get("/", func(ctx iris.Context){})
// 端口监听
app.Run(iris.Addr(":8080"))
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main

import (
"github.com/kataras/iris"
"github.com/kataras/iris/middleware/logger"
"github.com/kataras/iris/middleware/recover"
)

func main() {
app := iris.New()
app.Use(recover.New())
app.Use(logger.New())

// Method: GET
// Resource: http://localhost:8080
app.Handle("GET", "/", func(ctx iris.Context) {
ctx.HTML("<h1>Welcome</h1>")
})

// same as app.Handle("GET", "/ping", [...])
// Method: GET
// Resource: http://localhost:8080/ping
app.Get("/ping", func(ctx iris.Context) {
ctx.WriteString("pong")
})

// Method: GET
// Resource: http://localhost:8080/hello
app.Get("/hello", func(ctx iris.Context) {
ctx.JSON(iris.Map{"message": "Hello Iris!"})
})

// http://localhost:8080
// http://localhost:8080/ping
// http://localhost:8080/hello
app.Run(iris.Addr(":8080"))
}