1
- # Iris Web Framework
1
+ [ ![ 黑人的命也是命 ] ( https://iris-go.com/images/blacklivesmatter_banner.png )] ( https://support.eji.org/give/153413/#!/donation/checkout )
2
2
3
- [](https://travis-ci.org/kataras/iris) [](https://app.fossa.io/projects/git%2Bgithub.com%2Fkataras%2Firis?ref=badge_shield)<!--[](https://goreportcard.com/report/github.com/kataras/iris)--><!--[](https://pkg.go.dev/github.com/kataras/iris/
[email protected] )--> [](https://github.com/kataras/iris/tree/master/_examples) [](https://gitter.im/iris_go/community)<!--[](https://iris-go.com/donate)--><!-- [](https://github.com/kataras/iris/releases) -->
3
+ <!-- # News -->
4
4
5
- Iris 是基于 Go 编写的一个快速,简单但功能齐全且非常高效的 Web 框架。 它为您的下一个网站或 API 提供了一个非常富有表现力且易于使用的基础。
5
+ > 这是一个** 开发中的分支** . 敬请关注即将发布的版本 [ v12.2.0] ( HISTORY.md#Next ) . 如果想要一个稳定的版本,请用使用 [ v12.1.8 branch] ( https://github.com/kataras/iris/tree/v12.1.8 ) .
6
+ >
7
+ > ![ ] ( https://iris-go.com/images/cli.png ) 立即尝试官方的[ Iris命令行工具] ( https://github.com/kataras/iris-cli ) !
6
8
7
- 看看 [ 其他人如何评价 Iris ] ( https://iris-go.com/testimonials/ ) ,同时欢迎各位点亮 ** star ** 。
9
+ <!--  Iris version **12.1.8** has been [released](HISTORY.md#su-16-february-2020--v1218)! -->
8
10
9
- [ ![ ] ( https://media.giphy. com/media/j5WLmtvwn98VPrm7li/giphy.gif )] ( https://iris-go.com/testimonials/ )
11
+ # Iris Web Framework < a href = " README_GR.md " >< img width = " 20px " src = " https://iris-go.com/images/flag-greece.svg " /></ a > < a href = " README_FR.md " >< img width = " 20px " src = " https://iris-go. com/images/flag-france.svg " /></ a > < a href = " README_ZH.md " >< img width = " 20px " src = " https://iris-go.com/images/flag-china.svg " /></ a > < a href = " README_ES.md " >< img width = " 20px " src = " https://iris-go.com/images/flag-spain.png " /></ a > < a href = " README_FA.md " >< img width = " 20px " src = " https://iris-go.com/images/flag-iran.svg " /></ a > < a href = " README_RU.md " >< img width = " 20px " src = " https://iris-go.com/images/flag-russia.svg " /></ a > < a href = " README_KO.md " >< img width = " 20px " src = " https://iris-go.com/images/flag-south-korea.svg?v=12 " /></ a >
10
12
11
- [ ![ Benchmarks: Apr 2, 2020 at 12:13pm (UTC)] ( https://iris-go.com/images/benchmarks.svg )] ( https://github.com/kataras/server-benchmarks )
13
+ [](https://travis-ci.org/kataras/iris) [](https://github.com/kataras/iris/tree/master/_examples) [](https://gitter.im/iris_go/community) <!--[](https://app.fossa.io/projects/git%2Bgithub.com%2Fkataras%2Firis?ref=badge_shield)--> [](https://iris-go.com/donate) <!--[](https://goreportcard.com/report/github.com/kataras/iris)--><!--[](https://pkg.go.dev/github.com/kataras/iris/
[email protected] )--> <!-- [](https://github.com/kataras/iris/releases) -->
12
14
13
- ## 学习 Iris
15
+ <!-- <a href="https://iris-go.com"> <img align="right" src="https://iris-go.com/images/logo-w169.png"></a> -->
14
16
15
- <details >
16
- <summary >快速入门</summary >
17
+ Iris 是基于 Go 编写的一个快速,简单但功能齐全且非常高效的 Web 框架。
18
+
19
+ 它为您的下一个网站或 API 提供了一个非常富有表现力且易于使用的基础。
20
+
21
+ 看看 [ 其他人如何评价 Iris] ( https://iris-go.com/testimonials/ ) ,同时欢迎各位为此开源项目点亮 ** [ star] ( https://github.com/kataras/iris/stargazers ) ** 。
22
+
23
+ [ ![ ] ( https://iris-go.com/images/reviews.gif )] ( https://iris-go.com/testimonials/ )
24
+
25
+ [ ![ Benchmarks: Jul 18, 2020 at 10:46am (UTC)] ( https://iris-go.com/images/benchmarks.svg )] ( https://github.com/kataras/server-benchmarks )
26
+
27
+ ## 📖 开始学习 Iris
17
28
18
29
``` sh
19
- # 假设文件已经存在
20
- $ cat example.go
30
+ # 安装Iris:https://github.com/kataras/iris/wiki/Installation
31
+ $ go get github.com/kataras/iris/v12@master
32
+ # 假设main.go文件中已存在以下代码
33
+ $ cat main.go
21
34
```
22
35
23
36
``` go
@@ -26,51 +39,178 @@ package main
26
39
import " github.com/kataras/iris/v12"
27
40
28
41
func main () {
29
- app := iris.Default ()
30
- app.Get (" /ping" , func (ctx iris.Context ) {
31
- ctx.JSON (iris.Map {
32
- " message" : " pong" ,
33
- })
34
- })
35
-
36
- app.Listen (" :8080" )
42
+ app := iris.New ()
43
+
44
+ booksAPI := app.Party (" /books" )
45
+ {
46
+ booksAPI.Use (iris.Compression )
47
+
48
+ // GET: http://localhost:8080/books
49
+ booksAPI.Get (" /" , list)
50
+ // POST: http://localhost:8080/books
51
+ booksAPI.Post (" /" , create)
52
+ }
53
+
54
+ app.Listen (" :8080" )
55
+ }
56
+
57
+ // Book example.
58
+ type Book struct {
59
+ Title string ` json:"title"`
60
+ }
61
+
62
+ func list (ctx iris .Context ) {
63
+ books := []Book{
64
+ {" Mastering Concurrency in Go" },
65
+ {" Go Design Patterns" },
66
+ {" Black Hat Go" },
67
+ }
68
+
69
+ ctx.JSON (books)
70
+ // 提示: 在服务器优先级和客户端请求中进行响应协商,
71
+ // 以此来代替 ctx.JSON:
72
+ // ctx.Negotiation().JSON().MsgPack().Protobuf()
73
+ // ctx.Negotiate(books)
74
+ }
75
+
76
+ func create (ctx iris .Context ) {
77
+ var b Book
78
+ err := ctx.ReadJSON (&b)
79
+ // 提示: 使用 ctx.ReadBody(&b) 代替,来绑定所有类型的入参
80
+ if err != nil {
81
+ ctx.StopWithProblem (iris.StatusBadRequest , iris.NewProblem ().
82
+ Title (" Book creation failure" ).DetailErr (err))
83
+ // 提示: 如果仅有纯文本(plain text)错误响应,
84
+ // 可使用 ctx.StopWithError(code, err)
85
+ return
86
+ }
87
+
88
+ println (" Received Book: " + b.Title )
89
+
90
+ ctx.StatusCode (iris.StatusCreated )
91
+ }
92
+ ```
93
+
94
+ 同样地,在** MVC** 中 :
95
+
96
+ ``` go
97
+ import " github.com/kataras/iris/v12/mvc"
98
+ ```
99
+
100
+ ``` go
101
+ m := mvc.New (booksAPI)
102
+ m.Handle (new (BookController))
103
+ ```
104
+
105
+ ``` go
106
+ type BookController struct {
107
+ /* dependencies */
108
+ }
109
+
110
+ // GET: http://localhost:8080/books
111
+ func (c *BookController ) Get () []Book {
112
+ return []Book{
113
+ {" Mastering Concurrency in Go" },
114
+ {" Go Design Patterns" },
115
+ {" Black Hat Go" },
116
+ }
117
+ }
118
+
119
+ // POST: http://localhost:8080/books
120
+ func (c *BookController ) Post (b Book ) int {
121
+ println (" Received Book: " + b.Title )
122
+
123
+ return iris.StatusCreated
37
124
}
38
125
```
39
126
127
+ ** 启动** 您的 Iris web 服务:
128
+
40
129
``` sh
41
- # 运行 example .go
42
- # 在浏览器中访问 http://localhost:8080/ping
43
- $ go run example.go
130
+ $ go run main .go
131
+ > Now listening on: http://localhost:8080
132
+ > Application started. Press CTRL+C to shut down.
44
133
```
45
134
46
- > 路由由 [ muxie] ( https://github.com/kataras/muxie ) 提供支持,muxie 是基于 Go 编写的最强大最快速的基于 trie 的路由
135
+ Books ** 列表查询** :
136
+
137
+ ``` sh
138
+ $ curl --header ' Accept-Encoding:gzip' http://localhost:8080/books
139
+
140
+ [
141
+ {
142
+ " title" : " Mastering Concurrency in Go"
143
+ },
144
+ {
145
+ " title" : " Go Design Patterns"
146
+ },
147
+ {
148
+ " title" : " Black Hat Go"
149
+ }
150
+ ]
151
+ ```
152
+
153
+ ** 创建** 新的Book:
154
+
155
+ ``` sh
156
+ $ curl -i -X POST \
157
+ --header ' Content-Encoding:gzip' \
158
+ --header ' Content-Type:application/json' \
159
+ --data " {\" title\" :\" Writing An Interpreter In Go\" }" \
160
+ http://localhost:8080/books
161
+
162
+ > HTTP/1.1 201 Created
163
+ ```
164
+
165
+ 这是** 错误** 影响所展示的样子:
166
+
167
+ ``` sh
168
+ $ curl -X POST --data " {\" title\" \" not valid one\" }" \
169
+ http://localhost:8080/books
170
+
171
+ > HTTP/1.1 400 Bad Request
172
+
173
+ {
174
+ " status" : 400,
175
+ " title" : " Book creation failure"
176
+ " detail" : " invalid character '\" ' after object key" ,
177
+ }
178
+ ```
47
179
48
180
</details >
49
181
50
- Iris 包含详细而完整的 ** [ 文档] ( https://github.com/kataras/iris/wiki ) ** ,使你很容易开始使用该框架。
182
+ [ ![ run in the browser] ( https://img.shields.io/badge/Run-in%20the%20Browser-348798.svg?style=for-the-badge&logo=repl.it )] ( https://bit.ly/2YJeSZe )
183
+
184
+ Iris 有一个完整且详尽的 ** [ 使用文档] ( https://github.com/kataras/iris/wiki ) ** ,让您可以轻松地使用此框架。
51
185
52
- 要了解更多详细的技术文档,可以访问我们的 [ godocs ] ( https://pkg.go.dev/github. com/kataras/iris/[email protected] ) 。对于可执行代码,可以随时访问示例代码,在仓库的 [ \_ examples ] ( _examples/ ) 目录下。
186
+ <!--  -->
53
187
54
- ### 你喜欢在旅行中看书吗?
188
+ 要了解更详细的技术文档,请访问我们的 [ godocs ] ( https://godoc.org/github.com/kataras/iris ) 。如果想要寻找代码示例,您可以到仓库的 [ ./ _ examples ] ( _examples ) 子目录下获取。
55
189
56
- 你现在可以 [ 获取 ] ( https://bit.ly/iris-req-book ) PDF 版本和在线访问我们的 ** 电子书 ** 并参与 Iris 的开发。
190
+ ### 你喜欢在旅行时阅读吗?
57
191
58
192
<a href =" https://bit.ly/iris-req-book " > <img alt =" Book cover " src =" https://iris-go.com/images/iris-book-cover-sm.jpg?v=12 " /> </a >
59
193
60
- [ ![ follow author ] ( https://img.shields.io/twitter/follow/makismaropoulos.svg? style=for-the-badge )] ( https://twitter.com/intent/follow?screen_name=makismaropoulos )
194
+ [ ![ follow Iris web framework on twitter ] ( https://img.shields.io/twitter/follow/iris_framework?color=ee7506&logoColor=ee7506& style=for-the-badge )] ( https://twitter.com/intent/follow?screen_name=iris_framework )
61
195
62
- ## 贡献
196
+ 您可以 [ 获取 ] ( https://bit.ly/iris-req-book ) PDF版本或在线访问 ** 电子图书 ** ,并参与到Iris的开发中。
63
197
64
- 我们很高兴看到你对 Iris Web 框架的贡献!有关为 Iris 做出贡献的更多信息,请查看 [ CONTRIBUTING.md ] ( CONTRIBUTING.md ) 。
198
+ ## 🙌 贡献
65
199
66
- [ 所有贡献者名单 ] ( https://github.com/kataras/iris/graphs/contributors )
200
+ 我们欢迎您为Iris框架做出贡献!想要知道如何为Iris项目做贡献,请查看 [ CONTRIBUTING.md ] (CONTRIBUTING.md)。
67
201
68
- ## 安全漏洞
202
+ [ 贡献者名单 ] ( https://github.com/kataras/iris/graphs/contributors )
69
203
70
- 如果你发现在 Iris 存在安全漏洞,请发送电子邮件至 [ [email protected] ] ( mailto:[email protected] ) ,所有安全漏洞都会被及时解决。
204
+ ## 🛡 安全漏洞
71
205
72
- ## 授权协议
206
+ 如果您发现在 Iris 存在安全漏洞,请发送电子邮件至
[ [email protected] ] ( mailto:[email protected] ) 。所有安全漏洞将会得到及时解决。
207
+
208
+ ## 📝 开源协议(License)
209
+
210
+ 就像Go语言的协议一样,此项目也采用 [ BSD 3-clause license] ( LICENSE ) 。
73
211
74
212
项目名称 "Iris" 的灵感来自于希腊神话。
75
213
76
- Iris Web 框架授权基于 [ 3-Clause BSD License] ( LICENSE ) 许可的免费开源软件。
214
+ <!-- ## Stargazers over time
215
+
216
+ [](https://starchart.cc/kataras/iris) -->
0 commit comments