<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          BunRouter用于 Go 的快速靈活的 HTTP 路由器

          聯(lián)合創(chuàng)作 · 2023-10-01 14:38

          BunRouter 是一個用于 Go 的極快的 HTTP 路由器,具有獨特的功能組合:

          • 中間件允許將 HTTP 處理程序中的常見操作提取到可重用的函數(shù)中。
          • 錯誤處理允許通過處理中間件中的錯誤來進一步減小 HTTP 處理程序的大小。
          • 路由優(yōu)先 級為路由規(guī)則啟用有意義的匹配優(yōu)先級:首先是靜態(tài)節(jié)點,然后是命名節(jié)點,最后是通配符節(jié)點。
          • net/http 兼容 API,這意味著使用最少的 API,而無需構(gòu)建試圖做所有事情的巨大包裝器:從提供靜態(tài)文件到 XML 生成(例如,gin.Context或 echo.Context)。
          package main
          
          import (
          	"html/template"
          	"log"
          	"net/http"
          
          	"github.com/uptrace/bunrouter"
          	"github.com/uptrace/bunrouter/extra/reqlog"
          )
          
          func main() {
          	router := bunrouter.New(
          		bunrouter.WithMiddleware(reqlog.NewMiddleware()),
          	)
          
          	router.GET("/", indexHandler)
          
          	router.WithGroup("/api", func(g *bunrouter.Group) {
          		g.GET("/users/:id", debugHandler)
          		g.GET("/users/current", debugHandler)
          		g.GET("/users/*path", debugHandler)
          	})
          
          	log.Println("listening on http://localhost:9999")
          	log.Println(http.ListenAndServe(":9999", router))
          }
          
          func indexHandler(w http.ResponseWriter, req bunrouter.Request) error {
          	return indexTemplate().Execute(w, nil)
          }
          
          func debugHandler(w http.ResponseWriter, req bunrouter.Request) error {
          	return bunrouter.JSON(w, bunrouter.H{
          		"route":  req.Route(),
          		"params": req.Params().Map(),
          	})
          }
          
          var indexTmpl = `
          <html>
            <h1>Welcome</h1>
            <ul>
              <li><a href="/api/users/123">/api/users/123</a></li>
              <li><a href="/api/users/current">/api/users/current</a></li>
              <li><a href="/api/users/foo/bar">/api/users/foo/bar</a></li>
            </ul>
          </html>
          `
          
          func indexTemplate() *template.Template {
          	return template.Must(template.New("index").Parse(indexTmpl))
          }
          
          瀏覽 23
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  美女性爱网站 | 免费一区两区三区 | 欧美成人免费夜夜黄啪啪 | 性日熟妇| 大香蕉电影网 |