# go-air **Repository Path**: bitschen/go-air ## Basic Information - **Project Name**: go-air - **Description**: Golang超轻量级Web路由框架,面向物联网边缘计算领域设计 - A vvvery lightweight http router for golang - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2019-02-01 - **Last Updated**: 2025-01-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # GoAir ![LOGO](GoAir-Logo.png) Go-Air是一个基于 [net/http](https://golang.org/pkg/net/http/) 包实现的极简Http路由库,旨在提供简洁易用、原生一致的HTTP路由功能。 GoAir不打算提供传统Web框架大而全的工具库,GoAir的目标是为物联网领域,尤其在边缘计算上,提供一个性能优秀、节省内存的API Web开发环境。 ## Goals - 目标 1. 极快的路由性能、极低的内存占用; 1. 极小的代码量,代码力图最简; 1. 提供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: ```go 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](https://github.com/gorilla/mux) 1. 极速HttpRouter:[HttpRouter](https://github.com/julienschmidt/httprouter) 在与对比测试中[详见benchmark](benchmark),GoAir有极好的路由性能表现; HttpRouter干不过,真是太快了。我不喜欢它的一点是Handler不是原生`http.HandleFunc`,而是自定义加上params。 包括[echo](https://github.com/labstack/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 Under MIT, see: [LICENSE](LICENSE)