muxie:一个新兴的兼容net/http 快速路由
muxie 是个简单、快速、轻便的路由,其作者在两年多前开发的颇有争议的基于fasthttp 框架 iris。前不久开源出这个路由库,很赞的是100%兼容net/http。
muxie hello world
package main
import (
"fmt"
"net/http"
"github.com/kataras/muxie"
)
func main() {
mux := muxie.NewMux()
// if it is true the /about/ will be permantly redirected to /about and served from the aboutHandler.
// mux.PathCorrection = true
mux.HandleFunc("/", indexHandler)
mux.HandleFunc("/index", indexHandler)
mux.HandleFunc("/about", aboutHandler)
fmt.Println(`Server started at http://localhost:8080
Open your browser or any other HTTP Client and navigate to:
http://localhost:8080
http://localhost:8080/index and
http://localhost:8080/about`)
http.ListenAndServe(":8080", mux)
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html;charset=utf8")
fmt.Fprintf(w, "This is the <strong>%s</strong>", "index page")
}
func aboutHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Simple example to show how easy is to add routes with static paths.\nVisit the 'parameterized' example folder for more...\n"))
}
muxie 路由风格跟goji 类似,当年比较喜欢goji 是因为其开发者比较成熟,不参与各种路由性能竞赛,以稳定和兼容为主。
goji hello world
package main
import (
"fmt"
"net/http"
"goji.io"
"goji.io/pat"
)
func hello(w http.ResponseWriter, r *http.Request) {
name := pat.Param(r, "name")
fmt.Fprintf(w, "Hello, %s!", name)
}
func main() {
mux := goji.NewMux()
mux.HandleFunc(pat.Get("/hello/:name"), hello)
http.ListenAndServe("localhost:8000", mux)
}
参考:
0
See Also
- [源码探讨]SetCookie和DelCookie为什么要用&http.Cookie这种获取地址的方式啊?
- acme/autocert 库更新,支持 http-01
- net/http + mux 简单示例
- mithril 路由学习笔记
- 让pre 里面的内容自动换行的css 兼容各种浏览器
Nearby
- 上一篇 › objectbox-go 可以直接保存go 对象的数据库
- 下一篇 › GoLand 2018.3 正式版发布