0 Star 0 Fork 0

Robertzi / iris

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
BSD-3-Clause

黑人的命也是命

这是一个开发中的版本。敬请关注即将发布的版本 v12.2.0。如果想使用稳定版本,请查看 v12.1.8 分支

立即尝试官方的Iris命令行工具

Iris Web Framework

build status view examples chat donate

Iris 是基于 Go 编写的一个快速,简单但功能齐全且非常高效的 Web 框架。

它为您的下一个网站或 API 提供了一个非常富有表现力且易于使用的基础。

看看 其他人如何评价 Iris,同时欢迎各位为此开源项目点亮 star

Benchmarks: Jul 18, 2020 at 10:46am (UTC)

📖 开始学习 Iris

# 安装Iris:https://github.com/kataras/iris/wiki/Installation
$ go get github.com/kataras/iris/v12@master
# 假设main.go文件中已存在以下代码
$ cat main.go
package main

import "github.com/kataras/iris/v12"

func main() {
	app := iris.New()

	booksAPI := app.Party("/books")
	{
		booksAPI.Use(iris.Compression)

		// GET: http://localhost:8080/books
		booksAPI.Get("/", list)
		// POST: http://localhost:8080/books
		booksAPI.Post("/", create)
	}

	app.Listen(":8080")
}

// Book example.
type Book struct {
	Title string `json:"title"`
}

func list(ctx iris.Context) {
	books := []Book{
		{"Mastering Concurrency in Go"},
		{"Go Design Patterns"},
		{"Black Hat Go"},
	}

	ctx.JSON(books)
	// 提示: 在服务器优先级和客户端请求中进行响应协商,
	// 以此来代替 ctx.JSON:
	// ctx.Negotiation().JSON().MsgPack().Protobuf()
	// ctx.Negotiate(books)
}

func create(ctx iris.Context) {
	var b Book
	err := ctx.ReadJSON(&b)
	// 提示: 使用 ctx.ReadBody(&b) 代替,来绑定所有类型的入参
	if err != nil {
		ctx.StopWithProblem(iris.StatusBadRequest, iris.NewProblem().
			Title("Book creation failure").DetailErr(err))
		// 提示: 如果仅有纯文本(plain text)错误响应,
        // 可使用 ctx.StopWithError(code, err) 
		return
	}

	println("Received Book: " + b.Title)

	ctx.StatusCode(iris.StatusCreated)
}

同样地,在MVC中 :

import "github.com/kataras/iris/v12/mvc"
m := mvc.New(booksAPI)
m.Handle(new(BookController))
type BookController struct {
	/* dependencies */
}

// GET: http://localhost:8080/books
func (c *BookController) Get() []Book {
	return []Book{
		{"Mastering Concurrency in Go"},
		{"Go Design Patterns"},
		{"Black Hat Go"},
	}
}

// POST: http://localhost:8080/books
func (c *BookController) Post(b Book) int {
	println("Received Book: " + b.Title)

	return iris.StatusCreated
}

启动 您的 Iris web 服务:

$ go run main.go
> Now listening on: http://localhost:8080
> Application started. Press CTRL+C to shut down.

Books 列表查询 :

$ curl --header 'Accept-Encoding:gzip' http://localhost:8080/books

[
  {
    "title": "Mastering Concurrency in Go"
  },
  {
    "title": "Go Design Patterns"
  },
  {
    "title": "Black Hat Go"
  }
]

创建 新的Book:

$ curl -i -X POST \
--header 'Content-Encoding:gzip' \
--header 'Content-Type:application/json' \
--data "{\"title\":\"Writing An Interpreter In Go\"}" \
http://localhost:8080/books

> HTTP/1.1 201 Created

这是错误响应所展示的样子:

$ curl -X POST --data "{\"title\" \"not valid one\"}" \
http://localhost:8080/books

> HTTP/1.1 400 Bad Request

{
  "status": 400,
  "title": "Book creation failure"
  "detail": "invalid character '\"' after object key",
}

run in the browser

Iris 有完整且详尽的 使用文档 ,让您可以轻松地使用此框架。

要了解更详细的技术文档,请访问我们的 godocs。如果想要寻找代码示例,您可以到仓库的 ./_examples 子目录下获取。

你喜欢在旅行时阅读吗?

Book cover

follow Iris web framework on twitter

您可以获取PDF版本或在线访问电子图书,并参与到Iris的开发中。

🙌 贡献

我们欢迎您为Iris框架做出贡献!想要知道如何为Iris项目做贡献,请查看[CONTRIBUTING.md](CONTRIBUTING.md)。

贡献者名单

🛡 安全漏洞

如果您发现在 Iris 存在安全漏洞,请发送电子邮件至 iris-go@outlook.com。所有安全漏洞将会得到及时解决。

📝 开源协议(License)

就像Go语言的协议一样,此项目也采用 BSD 3-clause license

项目名称 "Iris" 的灵感来自于希腊神话。

Copyright (c) 2017-2021 The Iris Authors. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Iris nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

简介

暂无描述 展开 收起
BSD-3-Clause
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/robertzi/iris.git
git@gitee.com:robertzi/iris.git
robertzi
iris
iris
master

搜索帮助