This commit is contained in:
tengge1 2020-03-28 14:40:50 +08:00
parent 9b3b89d361
commit d068e4bb7e
2 changed files with 30 additions and 1 deletions

View File

@ -8,7 +8,7 @@ require (
github.com/cosiner/argv v0.0.1 // indirect
github.com/cweill/gotests v1.5.3 // indirect
github.com/davidrjenni/reftools v0.0.0-20191222082827-65925cf01315 // indirect
github.com/dimfeld/httptreemux v5.0.1+incompatible // indirect
github.com/dimfeld/httptreemux v5.0.1+incompatible
github.com/elazarl/go-bindata-assetfs v1.0.0 // indirect
github.com/fatih/gomodifytags v1.3.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect

View File

@ -0,0 +1,29 @@
package server
import (
"fmt"
"net/http"
"github.com/dimfeld/httptreemux"
)
// Start 启动服务端
func Start(port int) {
router := httptreemux.New()
group := router.NewGroup("/api")
group.GET("/v1/:id", func(w http.ResponseWriter, r *http.Request, params map[string]string) {
id := params["id"]
fmt.Fprintf(w, "GET /api/v1/%s", id)
})
// UsingContext returns a version of the router or group with context support.
ctxGroup := group.UsingContext() // sibling to 'group' node in tree
ctxGroup.GET("/v2/:id", func(w http.ResponseWriter, r *http.Request) {
params := httptreemux.ContextParams(r.Context())
id := params["id"]
fmt.Fprintf(w, "GET /api/v2/%s", id)
})
http.ListenAndServe(":8080", router)
}