2 Star 0 Fork 0

bits-chen / go-air

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

GoAir

LOGO

Go-Air是一个基于 net/http 包实现的极简Http路由库,旨在提供简洁易用、原生一致的HTTP路由功能。

GoAir不打算提供传统Web框架大而全的工具库,GoAir的目标是为物联网领域,尤其在边缘计算上,提供一个性能优秀、节省内存的API Web开发环境。

Goals - 目标

  1. 极快的路由性能、极低的内存占用;
  2. 极小的代码量,代码力图最简;
  3. 提供Golang原生http包一致的接口;

Features

  1. GET/POST/DELETE/PUT/OPTIONS/HEAD 方法的注册API;
  2. 全局Middleware 中间件;
  3. Handler作用域Middleware 中间件;
  4. 路径动态参数: /users/:userid
  5. TODO正则参数: /users/:id{[0-9]+}

Example

Install:

go get github.com/yoojia/go-air

Demo:


func main() {
	srv := air.New()

	// 全局Middleware
	srv.Use(func(next http.HandlerFunc) http.HandlerFunc {
		return func(w http.ResponseWriter, req *http.Request) {
			w.Header().Set("Server", "Air v0.0.1")
			next.ServeHTTP(w, req)
		}
	})
	srv.GET("/hello",
		// Handler
		func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte("World"))
		},
		// 当前Handler作用域的Middleware
		func(next http.HandlerFunc) http.HandlerFunc {
			return func(w http.ResponseWriter, r *http.Request) {
				w.Header().Set("Scoped", "Hello here")
				next.ServeHTTP(w, r)
			}
		})
	
	// 路径动态参数,从 axle.Vars 中获取
    srv.GET("/say/:word", func(w http.ResponseWriter, req *http.Request) {
        vars := axle.Vars(req)
        word := vars["word"]
        w.Write([]byte(word))
    })

	// Post
	srv.POST("/hi", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("HI~~"))
	})
	srv.Start(":9090")
}

Performance

  1. 常用的框架:Mux
  2. 极速HttpRouter:HttpRouter

在与对比测试中详见benchmark,GoAir有极好的路由性能表现; HttpRouter干不过,真是太快了。我不喜欢它的一点是Handler不是原生http.HandleFunc,而是自定义加上params。 包括echo等在内的GoWeb框架,我都不喜欢;它们都实现一套自己的Handler,这种深度绑定某个框架会让我非常纠结,而我只需要一个简单的API路由而已。

测试环境:

  • Mem: 8G
  • CPU: Intel® Core™ i5-4460 CPU @ 3.20GHz × 4
  • OS: Ubuntu 18.04 LTS

单API性能对比

名称 性能数据
HttpRouter 86 ns/op
Alex 396 ns/op
Mux 2052 ns/op

GithubAPI性能对比

Router注册Github API数量:203

名称 性能数据
HttpRouter 229 ns/op
Alex 1170 ns/op
Mux 35057 ns/op

原始测试数据:

goos: linux
goarch: amd64
pkg: github.com/yoojia/go-axle/benchmark
BenchmarkSimpleGoAir-4         	 3000000	       396 ns/op
BenchmarkSimpleMux-4          	 1000000	      2052 ns/op
BenchmarkSimpleHttpRouter-4   	20000000	        86.4 ns/op
BenchmarkGithubApiGoAir-4      	 1000000	      1170 ns/op
BenchmarkGithubApiMux-4       	   50000	     35057 ns/op
BenchmarkGithubHttpRouter-4   	10000000	       229 ns/op
PASS

LICENSE

Copyright (c) 2019 Yoojia Chen yoojiachen@gmail.com

Under MIT, see: LICENSE

The MIT License (MIT) Copyright (c) 2019 Yoojia Chen <yoojiachen@gmail.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Golang超轻量级Web路由框架,面向物联网边缘计算领域设计 - A vvvery lightweight http router for golang 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/bitschen/go-air.git
git@gitee.com:bitschen/go-air.git
bitschen
go-air
go-air
master

搜索帮助