diff --git a/ShadowEditor.Server.Go/go.mod b/ShadowEditor.Server.Go/go.mod index bde85866..0450a405 100644 --- a/ShadowEditor.Server.Go/go.mod +++ b/ShadowEditor.Server.Go/go.mod @@ -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 diff --git a/ShadowEditor.Server.Go/server/server.go b/ShadowEditor.Server.Go/server/server.go new file mode 100644 index 00000000..f5a3cc53 --- /dev/null +++ b/ShadowEditor.Server.Go/server/server.go @@ -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) +}