RivetHTTP 路由管理器
專注路由.簡潔, 貪心匹配, 支持注入, 可定制, 深度解耦的 http 路由管理器.
examples 目錄中有幾個例子, 方便您了解 Rivet.這里有個路由專項評測 go-http-routing-benchmark.
示例代碼:
package main
import (
"io"
"net/http"
"github.com/typepress/rivet"
)
// 常規(guī)風格 handler
func HelloWord(rw http.ResponseWriter, req *http.Request) {
io.WriteString(rw, "Hello Word")
}
/**
帶參數(shù)的 handler.
params 是從 URL.Path 中提取到的參數(shù).
params 的另一種風格是 PathParams/Scene. 參見 Scene.
*/
func Hi(params rivet.Params, rw http.ResponseWriter) {
io.WriteString(rw, "Hi "+params.Get("who")) // 提取參數(shù) who
}
func main() {
// 新建路由管理器
mux := rivet.NewRouter(nil) // 下文解釋參數(shù) nil
// 注冊路由
mux.Get("/", HelloWord)
mux.Get("/:who", Hi) // 參數(shù)名設定為 "who"
// rivet.Router 符合 http.Handler 接口
http.ListenAndServe(":3000", mux)
}評論
圖片
表情
