diff --git a/app.go b/app.go index 9a6c7a6..49eb295 100644 --- a/app.go +++ b/app.go @@ -6,21 +6,31 @@ import ( ) type App struct { + *RouterGroup router *router - server *http.Server - logger Logger + groups []*RouterGroup } func (a *App) ServeHTTP(writer http.ResponseWriter, request *http.Request) { - c := newCtx(a, writer, request) + c := newContext(writer, request) if err := a.router.handle(c); err != nil { writer.WriteHeader(500) _, _ = writer.Write([]byte(err.Error())) } } -func (a *App) Get(path string, handlerFunc HandlerFunc) { - a.router.addRoute(http.MethodGet, path, handlerFunc) +func (a *App) Get(path string, handlers ...HandlerFunc) { + if len(handlers) == 0 { + panic(fmt.Sprintf("missing handler in route: %s", path)) + } + + for _, handler := range handlers { + if handler == nil { + panic(fmt.Sprintf("nil handler found in route: %s", path)) + } + } + + a.router.addRoute(http.MethodGet, path, handlers...) } func (a *App) Run(address string) error { diff --git a/ctx.go b/ctx.go index fe0ccd2..f40145a 100644 --- a/ctx.go +++ b/ctx.go @@ -1,49 +1,96 @@ package nf import ( - "errors" + "encoding/json" + "fmt" "net/http" ) +type Map map[string]interface{} + type Ctx struct { - app *App - Response http.ResponseWriter - Request *http.Request - Path string - Method string - Params map[string]string + // origin objects + Writer http.ResponseWriter + Request *http.Request + // request info + Path string + Method string + // response info StatusCode int + + Params map[string]string + index int + handlers []HandlerFunc } -func newCtx(a *App, w http.ResponseWriter, req *http.Request) *Ctx { +func newContext(writer http.ResponseWriter, request *http.Request) *Ctx { return &Ctx{ - app: a, - Response: w, - Request: req, - Path: req.URL.Path, - Method: req.Method, + Writer: writer, + Request: request, + Path: request.URL.Path, + Method: request.Method, + index: -1, } } -func (c *Ctx) Param(key string) string { - value, _ := c.Params[key] - return value +func (c *Ctx) Form(key string) string { + return c.Request.FormValue(key) } -func (c *Ctx) Status(status int) *Ctx { - if c.StatusCode != 0 { - c.app.logger.Warn() +func (c *Ctx) Query(key string) string { + return c.Request.URL.Query().Get(key) +} + +func (c *Ctx) Status(code int) { + c.StatusCode = code + c.Writer.WriteHeader(code) +} + +func (c *Ctx) SetHeader(key string, value string) { + c.Writer.Header().Set(key, value) +} + +func (c *Ctx) SendString(data string) error { + c.SetHeader("Content-Type", "text/plain") + _, err := c.Write([]byte(data)) + return err +} + +func (c *Ctx) Writef(format string, values ...interface{}) (int, error) { + c.SetHeader("Content-Type", "text/plain") + return c.Writer.Write([]byte(fmt.Sprintf(format, values...))) +} + +func (c *Ctx) JSON(data interface{}) error { + c.SetHeader("Content-Type", "application/json") + + encoder := json.NewEncoder(c.Writer) + + if err := encoder.Encode(data); err != nil { + return err } - c.StatusCode = status - return c + + return nil } -func (c *Ctx) SendStatus(status int) error { - panic("impl this") +func (c *Ctx) Write(data []byte) (int, error) { + return c.Writer.Write(data) } -func (c *Ctx) SendString(value string) error { - _, err := c.Response.Write([]byte(value)) - _ = err - return errors.New(value) +func (c *Ctx) HTML(html string) error { + c.SetHeader("Content-Type", "text/html") + _, err := c.Writer.Write([]byte(html)) + return err +} + +func (c *Ctx) Next() error { + c.index++ + s := len(c.handlers) + for ; c.index < s; c.index++ { + if err := c.handlers[c.index](c); err != nil { + return err + } + } + + return nil } diff --git a/example/simple/main.go b/example/simple/main.go index be1dcfa..e74dd94 100644 --- a/example/simple/main.go +++ b/example/simple/main.go @@ -2,8 +2,10 @@ package main import ( "errors" + "fmt" "log" "nf" + "time" ) func main() { @@ -17,5 +19,21 @@ func main() { return errors.New("nice") }) + v1 := app.Group("/v1") + v1.Get("/hello", func(c *nf.Ctx) error { + return c.JSON(nf.Map{"version": "v1", "date": time.Now()}) + }) + + v1.Get( + "/name", + func(c *nf.Ctx) error { + c.Params["name"] = "zyp" + return c.Next() + }, + func(c *nf.Ctx) error { + return c.SendString(fmt.Sprintf("hi, %s", c.Params["name"])) + }, + ) + log.Fatal(app.Run(":7733")) } diff --git a/go.mod b/go.mod index d8f3307..1ae6e70 100644 --- a/go.mod +++ b/go.mod @@ -3,33 +3,21 @@ module nf go 1.20 require ( - github.com/gin-gonic/gin v1.9.1 + github.com/gofiber/fiber/v2 v2.52.0 github.com/sirupsen/logrus v1.9.3 ) require ( - github.com/bytedance/sonic v1.9.1 // indirect - github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect - github.com/gabriel-vasile/mimetype v1.4.2 // indirect - github.com/gin-contrib/sse v0.1.0 // indirect - github.com/go-playground/locales v0.14.1 // indirect - github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/go-playground/validator/v10 v10.14.0 // indirect - github.com/goccy/go-json v0.10.2 // indirect - github.com/json-iterator/go v1.1.12 // indirect - github.com/klauspost/cpuid/v2 v2.2.4 // indirect - github.com/leodido/go-urn v1.2.4 // indirect - github.com/mattn/go-isatty v0.0.19 // indirect - github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect - github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/pelletier/go-toml/v2 v2.0.8 // indirect - github.com/twitchyliquid64/golang-asm v0.15.1 // indirect - github.com/ugorji/go/codec v1.2.11 // indirect - golang.org/x/arch v0.3.0 // indirect - golang.org/x/crypto v0.9.0 // indirect - golang.org/x/net v0.10.0 // indirect - golang.org/x/sys v0.8.0 // indirect - golang.org/x/text v0.9.0 // indirect - google.golang.org/protobuf v1.30.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect + github.com/andybalholm/brotli v1.0.5 // indirect + github.com/google/uuid v1.5.0 // indirect + github.com/klauspost/compress v1.17.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect + github.com/rivo/uniseg v0.2.0 // indirect + github.com/stretchr/testify v1.8.3 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.51.0 // indirect + github.com/valyala/tcplisten v1.0.0 // indirect + golang.org/x/sys v0.15.0 // indirect ) diff --git a/go.sum b/go.sum index 9f794af..c4aa757 100644 --- a/go.sum +++ b/go.sum @@ -1,88 +1,42 @@ -github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= -github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s= -github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= -github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= -github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams= -github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= +github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= +github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= -github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= -github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= -github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= -github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= -github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= -github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= -github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= -github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= -github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= -github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= -github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js= -github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= -github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= -github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= -github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= -github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= -github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= -github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= -github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= -github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= -github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= -github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= -github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= -github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= +github.com/gofiber/fiber/v2 v2.52.0 h1:S+qXi7y+/Pgvqq4DrSmREGiFwtB7Bu6+QFLuIHYw/UE= +github.com/gofiber/fiber/v2 v2.52.0/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= +github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= +github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM= +github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= -github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= -github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= -github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= -golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= -golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= -golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= -golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g= -golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0= -golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= -golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= -golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= +github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= +github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= +github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= -golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= -golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= -google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/group.go b/group.go new file mode 100644 index 0000000..774f75c --- /dev/null +++ b/group.go @@ -0,0 +1,40 @@ +package nf + +import ( + "log" + "net/http" +) + +type RouterGroup struct { + prefix string + middlewares []HandlerFunc // support middleware + parent *RouterGroup // support nesting + app *App // all groups share a Engine instance +} + +// Group is defined to create a new RouterGroup +// remember all groups share the same Engine instance +func (group *RouterGroup) Group(prefix string) *RouterGroup { + app := group.app + newGroup := &RouterGroup{ + prefix: group.prefix + prefix, + parent: group, + app: app, + } + app.groups = append(app.groups, newGroup) + return newGroup +} + +func (group *RouterGroup) addRoute(method string, comp string, handlers ...HandlerFunc) { + pattern := group.prefix + comp + log.Printf("Route %4s - %s", method, pattern) + group.app.router.addRoute(method, pattern, handlers...) +} + +func (group *RouterGroup) Get(pattern string, handlers ...HandlerFunc) { + group.addRoute(http.MethodGet, pattern, handlers...) +} + +func (group *RouterGroup) Post(pattern string, handlers ...HandlerFunc) { + group.addRoute(http.MethodPost, pattern, handlers...) +} diff --git a/handler.go b/handler.go index 64f3a2a..e85abcb 100644 --- a/handler.go +++ b/handler.go @@ -1,3 +1,5 @@ package nf type HandlerFunc func(*Ctx) error + +type HandlerFuncs []HandlerFunc diff --git a/logger.go b/logger.go deleted file mode 100644 index 35f20a9..0000000 --- a/logger.go +++ /dev/null @@ -1,32 +0,0 @@ -package nf - -import ( - "github.com/sirupsen/logrus" -) - -type Logger interface { - Trace(...any) - Tracef(string, ...any) - - Debug(...any) - Debugf(string, ...any) - - Info(...any) - Infof(string, ...any) - - Warn(...any) - Warnf(string, ...any) - - Error(...any) - Errorf(string, ...any) - - Panic(...any) - Panicf(string, ...any) - - Fatal(...any) - Fatalf(string, ...any) -} - -var ( - defaultLogger = logrus.New() -) diff --git a/nf.go b/nf.go index e2ad0fc..d4f0051 100644 --- a/nf.go +++ b/nf.go @@ -5,14 +5,15 @@ const ( ) type Config struct { - Logger Logger } func New(config ...Config) *App { app := &App{ router: newRouter(), - logger: defaultLogger, } + app.RouterGroup = &RouterGroup{app: app} + app.groups = []*RouterGroup{app.RouterGroup} + return app } diff --git a/response.go b/response.go deleted file mode 100644 index 5f35995..0000000 --- a/response.go +++ /dev/null @@ -1,3 +0,0 @@ -package nf - -type Response struct{} diff --git a/router.go b/router.go index e5034c4..fd7e924 100644 --- a/router.go +++ b/router.go @@ -1,21 +1,15 @@ package nf -import ( - "net/http" - "strings" -) +import "strings" type router struct { - roots map[string]*node + roots map[string]*_node handlers map[string][]HandlerFunc } -// roots key eg, roots['GET'] roots['POST'] -// handlers key eg, handlers['GET-/p/:lang/doc'], handlers['POST-/p/book'] - func newRouter() *router { return &router{ - roots: make(map[string]*node), + roots: make(map[string]*_node), handlers: make(map[string][]HandlerFunc), } } @@ -42,13 +36,13 @@ func (r *router) addRoute(method string, pattern string, handlers ...HandlerFunc key := method + "-" + pattern _, ok := r.roots[method] if !ok { - r.roots[method] = &node{} + r.roots[method] = &_node{} } r.roots[method].insert(pattern, parts, 0) r.handlers[key] = handlers } -func (r *router) getRoute(method string, path string) (*node, map[string]string) { +func (r *router) getRoute(method string, path string) (*_node, map[string]string) { searchParts := parsePattern(path) params := make(map[string]string) root, ok := r.roots[method] @@ -76,21 +70,26 @@ func (r *router) getRoute(method string, path string) (*node, map[string]string) return nil, nil } +func (r *router) getRoutes(method string) []*_node { + root, ok := r.roots[method] + if !ok { + return nil + } + nodes := make([]*_node, 0) + root.travel(&nodes) + return nodes +} + func (r *router) handle(c *Ctx) error { - n, params := r.getRoute(c.Method, c.Path) - if n != nil { + node, params := r.getRoute(c.Method, c.Path) + if node != nil { c.Params = params - key := c.Method + "-" + n.pattern - for _, handler := range r.handlers[key] { - err := handler(c) - if err != nil { - return err - } - } + key := c.Method + "-" + node.pattern + c.handlers = append(c.handlers, r.handlers[key]...) } else { - c.Status(http.StatusNotFound) - return c.SendString("Not Found") + _, err := c.Writef("404 NOT FOUND: %s\n", c.Path) + return err } - return nil + return c.Next() } diff --git a/tree.go b/tree.go index 2096243..f350e1c 100644 --- a/tree.go +++ b/tree.go @@ -1,36 +1,22 @@ package nf -import "strings" +import ( + "fmt" + "strings" +) -type node struct { - pattern string // 待匹配路由,例如 /p/:lang - part string // 路由中的一部分,例如 :lang - children []*node // 子节点,例如 [doc, tutorial, intro] - isWild bool // 是否精确匹配,part 含有 : 或 * 时为true +type _node struct { + pattern string + part string + children []*_node + isWild bool } -// 第一个匹配成功的节点,用于插入 -func (n *node) matchChild(part string) *node { - for _, child := range n.children { - if child.part == part || child.isWild { - return child - } - } - return nil +func (n *_node) String() string { + return fmt.Sprintf("_node{pattern=%s, part=%s, isWild=%t}", n.pattern, n.part, n.isWild) } -// 所有匹配成功的节点,用于查找 -func (n *node) matchChildren(part string) []*node { - nodes := make([]*node, 0) - for _, child := range n.children { - if child.part == part || child.isWild { - nodes = append(nodes, child) - } - } - return nodes -} - -func (n *node) insert(pattern string, parts []string, height int) { +func (n *_node) insert(pattern string, parts []string, height int) { if len(parts) == height { n.pattern = pattern return @@ -39,13 +25,13 @@ func (n *node) insert(pattern string, parts []string, height int) { part := parts[height] child := n.matchChild(part) if child == nil { - child = &node{part: part, isWild: part[0] == ':' || part[0] == '*'} + child = &_node{part: part, isWild: part[0] == ':' || part[0] == '*'} n.children = append(n.children, child) } child.insert(pattern, parts, height+1) } -func (n *node) search(parts []string, height int) *node { +func (n *_node) search(parts []string, height int) *_node { if len(parts) == height || strings.HasPrefix(n.part, "*") { if n.pattern == "" { return nil @@ -65,3 +51,31 @@ func (n *node) search(parts []string, height int) *node { return nil } + +func (n *_node) travel(list *([]*_node)) { + if n.pattern != "" { + *list = append(*list, n) + } + for _, child := range n.children { + child.travel(list) + } +} + +func (n *_node) matchChild(part string) *_node { + for _, child := range n.children { + if child.part == part || child.isWild { + return child + } + } + return nil +} + +func (n *_node) matchChildren(part string) []*_node { + nodes := make([]*_node, 0) + for _, child := range n.children { + if child.part == part || child.isWild { + nodes = append(nodes, child) + } + } + return nodes +} diff --git a/tree2.txt b/tree2.txt deleted file mode 100644 index 44520e3..0000000 --- a/tree2.txt +++ /dev/null @@ -1,899 +0,0 @@ -package nf - -import ( - "bytes" - "net/url" - "strings" - "unicode" - "unicode/utf8" - "unsafe" -) - -// StringToBytes converts string to byte slice without a memory allocation. -// For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077. -func stringToBytes(s string) []byte { - return unsafe.Slice(unsafe.StringData(s), len(s)) -} - -// BytesToString converts byte slice to string without a memory allocation. -// For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077. -func bytesToString(b []byte) string { - return unsafe.String(unsafe.SliceData(b), len(b)) -} - -var ( - strColon = []byte(":") - strStar = []byte("*") - strSlash = []byte("/") -) - -// Param is a single URL parameter, consisting of a key and a value. -type Param struct { - Key string - Value string -} - -// Params is a Param-slice, as returned by the router. -// The slice is ordered, the first URL parameter is also the first slice value. -// It is therefore safe to read values by the index. -type Params []Param - -// Get returns the value of the first Param which key matches the given name and a boolean true. -// If no matching Param is found, an empty string is returned and a boolean false . -func (ps Params) Get(name string) (string, bool) { - for _, entry := range ps { - if entry.Key == name { - return entry.Value, true - } - } - return "", false -} - -// ByName returns the value of the first Param which key matches the given name. -// If no matching Param is found, an empty string is returned. -func (ps Params) ByName(name string) (va string) { - va, _ = ps.Get(name) - return -} - -type methodTree struct { - method string - root *node -} - -type methodTrees []methodTree - -func (trees methodTrees) get(method string) *node { - for _, tree := range trees { - if tree.method == method { - return tree.root - } - } - return nil -} - -func min(a, b int) int { - if a <= b { - return a - } - return b -} - -func longestCommonPrefix(a, b string) int { - i := 0 - max := min(len(a), len(b)) - for i < max && a[i] == b[i] { - i++ - } - return i -} - -// addChild will add a child node, keeping wildcardChild at the end -func (n *node) addChild(child *node) { - if n.wildChild && len(n.children) > 0 { - wildcardChild := n.children[len(n.children)-1] - n.children = append(n.children[:len(n.children)-1], child, wildcardChild) - } else { - n.children = append(n.children, child) - } -} - -func countParams(path string) uint16 { - var n uint16 - s := stringToBytes(path) - n += uint16(bytes.Count(s, strColon)) - n += uint16(bytes.Count(s, strStar)) - return n -} - -func countSections(path string) uint16 { - s := stringToBytes(path) - return uint16(bytes.Count(s, strSlash)) -} - -type nodeType uint8 - -const ( - static nodeType = iota - root - param - catchAll -) - -type node struct { - path string - indices string - wildChild bool - nType nodeType - priority uint32 - children []*node // child nodes, at most 1 :param style node at the end of the array - handlers []HandlerFunc - fullPath string -} - -// Increments priority of the given child and reorders if necessary -func (n *node) incrementChildPrio(pos int) int { - cs := n.children - cs[pos].priority++ - prio := cs[pos].priority - - // Adjust position (move to front) - newPos := pos - for ; newPos > 0 && cs[newPos-1].priority < prio; newPos-- { - // Swap node positions - cs[newPos-1], cs[newPos] = cs[newPos], cs[newPos-1] - } - - // Build new index char string - if newPos != pos { - n.indices = n.indices[:newPos] + // Unchanged prefix, might be empty - n.indices[pos:pos+1] + // The index char we move - n.indices[newPos:pos] + n.indices[pos+1:] // Rest without char at 'pos' - } - - return newPos -} - -// addRoute adds a node with the given handle to the path. -// Not concurrency-safe! -func (n *node) addRoute(path string, handlers []HandlerFunc) { - fullPath := path - n.priority++ - - // Empty tree - if len(n.path) == 0 && len(n.children) == 0 { - n.insertChild(path, fullPath, handlers) - n.nType = root - return - } - - parentFullPathIndex := 0 - -walk: - for { - // Find the longest common prefix. - // This also implies that the common prefix contains no ':' or '*' - // since the existing key can't contain those chars. - i := longestCommonPrefix(path, n.path) - - // Split edge - if i < len(n.path) { - child := node{ - path: n.path[i:], - wildChild: n.wildChild, - nType: static, - indices: n.indices, - children: n.children, - handlers: n.handlers, - priority: n.priority - 1, - fullPath: n.fullPath, - } - - n.children = []*node{&child} - // []byte for proper unicode char conversion, see #65 - n.indices = bytesToString([]byte{n.path[i]}) - n.path = path[:i] - n.handlers = nil - n.wildChild = false - n.fullPath = fullPath[:parentFullPathIndex+i] - } - - // Make new node a child of this node - if i < len(path) { - path = path[i:] - c := path[0] - - // '/' after param - if n.nType == param && c == '/' && len(n.children) == 1 { - parentFullPathIndex += len(n.path) - n = n.children[0] - n.priority++ - continue walk - } - - // Check if a child with the next path byte exists - for i, max := 0, len(n.indices); i < max; i++ { - if c == n.indices[i] { - parentFullPathIndex += len(n.path) - i = n.incrementChildPrio(i) - n = n.children[i] - continue walk - } - } - - // Otherwise insert it - if c != ':' && c != '*' && n.nType != catchAll { - // []byte for proper unicode char conversion, see #65 - n.indices += bytesToString([]byte{c}) - child := &node{ - fullPath: fullPath, - } - n.addChild(child) - n.incrementChildPrio(len(n.indices) - 1) - n = child - } else if n.wildChild { - // inserting a wildcard node, need to check if it conflicts with the existing wildcard - n = n.children[len(n.children)-1] - n.priority++ - - // Check if the wildcard matches - if len(path) >= len(n.path) && n.path == path[:len(n.path)] && - // Adding a child to a catchAll is not possible - n.nType != catchAll && - // Check for longer wildcard, e.g. :name and :names - (len(n.path) >= len(path) || path[len(n.path)] == '/') { - continue walk - } - - // Wildcard conflict - pathSeg := path - if n.nType != catchAll { - pathSeg = strings.SplitN(pathSeg, "/", 2)[0] - } - prefix := fullPath[:strings.Index(fullPath, pathSeg)] + n.path - panic("'" + pathSeg + - "' in new path '" + fullPath + - "' conflicts with existing wildcard '" + n.path + - "' in existing prefix '" + prefix + - "'") - } - - n.insertChild(path, fullPath, handlers) - return - } - - // Otherwise add handle to current node - if n.handlers != nil { - panic("handlers are already registered for path '" + fullPath + "'") - } - n.handlers = handlers - n.fullPath = fullPath - return - } -} - -// Search for a wildcard segment and check the name for invalid characters. -// Returns -1 as index, if no wildcard was found. -func findWildcard(path string) (wildcard string, i int, valid bool) { - // Find start - for start, c := range []byte(path) { - // A wildcard starts with ':' (param) or '*' (catch-all) - if c != ':' && c != '*' { - continue - } - - // Find end and check for invalid characters - valid = true - for end, c := range []byte(path[start+1:]) { - switch c { - case '/': - return path[start : start+1+end], start, valid - case ':', '*': - valid = false - } - } - return path[start:], start, valid - } - return "", -1, false -} - -func (n *node) insertChild(path string, fullPath string, handlers []HandlerFunc) { - for { - // Find prefix until first wildcard - wildcard, i, valid := findWildcard(path) - if i < 0 { // No wildcard found - break - } - - // The wildcard name must only contain one ':' or '*' character - if !valid { - panic("only one wildcard per path segment is allowed, has: '" + - wildcard + "' in path '" + fullPath + "'") - } - - // check if the wildcard has a name - if len(wildcard) < 2 { - panic("wildcards must be named with a non-empty name in path '" + fullPath + "'") - } - - if wildcard[0] == ':' { // param - if i > 0 { - // Insert prefix before the current wildcard - n.path = path[:i] - path = path[i:] - } - - child := &node{ - nType: param, - path: wildcard, - fullPath: fullPath, - } - n.addChild(child) - n.wildChild = true - n = child - n.priority++ - - // if the path doesn't end with the wildcard, then there - // will be another subpath starting with '/' - if len(wildcard) < len(path) { - path = path[len(wildcard):] - - child := &node{ - priority: 1, - fullPath: fullPath, - } - n.addChild(child) - n = child - continue - } - - // Otherwise we're done. Insert the handle in the new leaf - n.handlers = handlers - return - } - - // catchAll - if i+len(wildcard) != len(path) { - panic("catch-all routes are only allowed at the end of the path in path '" + fullPath + "'") - } - - if len(n.path) > 0 && n.path[len(n.path)-1] == '/' { - pathSeg := strings.SplitN(n.children[0].path, "/", 2)[0] - panic("catch-all wildcard '" + path + - "' in new path '" + fullPath + - "' conflicts with existing path segment '" + pathSeg + - "' in existing prefix '" + n.path + pathSeg + - "'") - } - - // currently fixed width 1 for '/' - i-- - if path[i] != '/' { - panic("no / before catch-all in path '" + fullPath + "'") - } - - n.path = path[:i] - - // First node: catchAll node with empty path - child := &node{ - wildChild: true, - nType: catchAll, - fullPath: fullPath, - } - - n.addChild(child) - n.indices = string('/') - n = child - n.priority++ - - // second node: node holding the variable - child = &node{ - path: path[i:], - nType: catchAll, - handlers: handlers, - priority: 1, - fullPath: fullPath, - } - n.children = []*node{child} - - return - } - - // If no wildcard was found, simply insert the path and handle - n.path = path - n.handlers = handlers - n.fullPath = fullPath -} - -// nodeValue holds return values of (*Node).getValue method -type nodeValue struct { - handlers []HandlerFunc - params *Params - tsr bool - fullPath string -} - -type skippedNode struct { - path string - node *node - paramsCount int16 -} - -// Returns the handle registered with the given path (key). The values of -// wildcards are saved to a map. -// If no handle can be found, a TSR (trailing slash redirect) recommendation is -// made if a handle exists with an extra (without the) trailing slash for the -// given path. -func (n *node) getValue(path string, params *Params, skippedNodes *[]skippedNode, unescape bool) (value nodeValue) { - var globalParamsCount int16 - -walk: // Outer loop for walking the tree - for { - prefix := n.path - if len(path) > len(prefix) { - if path[:len(prefix)] == prefix { - path = path[len(prefix):] - - // Try all the non-wildcard children first by matching the indices - idxc := path[0] - for i, c := range []byte(n.indices) { - if c == idxc { - // strings.HasPrefix(n.children[len(n.children)-1].path, ":") == n.wildChild - if n.wildChild { - index := len(*skippedNodes) - *skippedNodes = (*skippedNodes)[:index+1] - (*skippedNodes)[index] = skippedNode{ - path: prefix + path, - node: &node{ - path: n.path, - wildChild: n.wildChild, - nType: n.nType, - priority: n.priority, - children: n.children, - handlers: n.handlers, - fullPath: n.fullPath, - }, - paramsCount: globalParamsCount, - } - } - - n = n.children[i] - continue walk - } - } - - if !n.wildChild { - // If the path at the end of the loop is not equal to '/' and the current node has no child nodes - // the current node needs to roll back to last valid skippedNode - if path != "/" { - for length := len(*skippedNodes); length > 0; length-- { - skippedNode := (*skippedNodes)[length-1] - *skippedNodes = (*skippedNodes)[:length-1] - if strings.HasSuffix(skippedNode.path, path) { - path = skippedNode.path - n = skippedNode.node - if value.params != nil { - *value.params = (*value.params)[:skippedNode.paramsCount] - } - globalParamsCount = skippedNode.paramsCount - continue walk - } - } - } - - // Nothing found. - // We can recommend to redirect to the same URL without a - // trailing slash if a leaf exists for that path. - value.tsr = path == "/" && n.handlers != nil - return - } - - // Handle wildcard child, which is always at the end of the array - n = n.children[len(n.children)-1] - globalParamsCount++ - - switch n.nType { - case param: - // fix truncate the parameter - // tree_test.go line: 204 - - // Find param end (either '/' or path end) - end := 0 - for end < len(path) && path[end] != '/' { - end++ - } - - // Save param value - if params != nil { - // Preallocate capacity if necessary - if cap(*params) < int(globalParamsCount) { - newParams := make(Params, len(*params), globalParamsCount) - copy(newParams, *params) - *params = newParams - } - - if value.params == nil { - value.params = params - } - // Expand slice within preallocated capacity - i := len(*value.params) - *value.params = (*value.params)[:i+1] - val := path[:end] - if unescape { - if v, err := url.QueryUnescape(val); err == nil { - val = v - } - } - (*value.params)[i] = Param{ - Key: n.path[1:], - Value: val, - } - } - - // we need to go deeper! - if end < len(path) { - if len(n.children) > 0 { - path = path[end:] - n = n.children[0] - continue walk - } - - // ... but we can't - value.tsr = len(path) == end+1 - return - } - - if value.handlers = n.handlers; value.handlers != nil { - value.fullPath = n.fullPath - return - } - if len(n.children) == 1 { - // No handle found. Check if a handle for this path + a - // trailing slash exists for TSR recommendation - n = n.children[0] - value.tsr = (n.path == "/" && n.handlers != nil) || (n.path == "" && n.indices == "/") - } - return - - case catchAll: - // Save param value - if params != nil { - // Preallocate capacity if necessary - if cap(*params) < int(globalParamsCount) { - newParams := make(Params, len(*params), globalParamsCount) - copy(newParams, *params) - *params = newParams - } - - if value.params == nil { - value.params = params - } - // Expand slice within preallocated capacity - i := len(*value.params) - *value.params = (*value.params)[:i+1] - val := path - if unescape { - if v, err := url.QueryUnescape(path); err == nil { - val = v - } - } - (*value.params)[i] = Param{ - Key: n.path[2:], - Value: val, - } - } - - value.handlers = n.handlers - value.fullPath = n.fullPath - return - - default: - panic("invalid node type") - } - } - } - - if path == prefix { - // If the current path does not equal '/' and the node does not have a registered handle and the most recently matched node has a child node - // the current node needs to roll back to last valid skippedNode - if n.handlers == nil && path != "/" { - for length := len(*skippedNodes); length > 0; length-- { - skippedNode := (*skippedNodes)[length-1] - *skippedNodes = (*skippedNodes)[:length-1] - if strings.HasSuffix(skippedNode.path, path) { - path = skippedNode.path - n = skippedNode.node - if value.params != nil { - *value.params = (*value.params)[:skippedNode.paramsCount] - } - globalParamsCount = skippedNode.paramsCount - continue walk - } - } - // n = latestNode.children[len(latestNode.children)-1] - } - // We should have reached the node containing the handle. - // Check if this node has a handle registered. - if value.handlers = n.handlers; value.handlers != nil { - value.fullPath = n.fullPath - return - } - - // If there is no handle for this route, but this route has a - // wildcard child, there must be a handle for this path with an - // additional trailing slash - if path == "/" && n.wildChild && n.nType != root { - value.tsr = true - return - } - - if path == "/" && n.nType == static { - value.tsr = true - return - } - - // No handle found. Check if a handle for this path + a - // trailing slash exists for trailing slash recommendation - for i, c := range []byte(n.indices) { - if c == '/' { - n = n.children[i] - value.tsr = (len(n.path) == 1 && n.handlers != nil) || - (n.nType == catchAll && n.children[0].handlers != nil) - return - } - } - - return - } - - // Nothing found. We can recommend to redirect to the same URL with an - // extra trailing slash if a leaf exists for that path - value.tsr = path == "/" || - (len(prefix) == len(path)+1 && prefix[len(path)] == '/' && - path == prefix[:len(prefix)-1] && n.handlers != nil) - - // roll back to last valid skippedNode - if !value.tsr && path != "/" { - for length := len(*skippedNodes); length > 0; length-- { - skippedNode := (*skippedNodes)[length-1] - *skippedNodes = (*skippedNodes)[:length-1] - if strings.HasSuffix(skippedNode.path, path) { - path = skippedNode.path - n = skippedNode.node - if value.params != nil { - *value.params = (*value.params)[:skippedNode.paramsCount] - } - globalParamsCount = skippedNode.paramsCount - continue walk - } - } - } - - return - } -} - -// Makes a case-insensitive lookup of the given path and tries to find a handler. -// It can optionally also fix trailing slashes. -// It returns the case-corrected path and a bool indicating whether the lookup -// was successful. -func (n *node) findCaseInsensitivePath(path string, fixTrailingSlash bool) ([]byte, bool) { - const stackBufSize = 128 - - // Use a static sized buffer on the stack in the common case. - // If the path is too long, allocate a buffer on the heap instead. - buf := make([]byte, 0, stackBufSize) - if length := len(path) + 1; length > stackBufSize { - buf = make([]byte, 0, length) - } - - ciPath := n.findCaseInsensitivePathRec( - path, - buf, // Preallocate enough memory for new path - [4]byte{}, // Empty rune buffer - fixTrailingSlash, - ) - - return ciPath, ciPath != nil -} - -// Shift bytes in array by n bytes left -func shiftNRuneBytes(rb [4]byte, n int) [4]byte { - switch n { - case 0: - return rb - case 1: - return [4]byte{rb[1], rb[2], rb[3], 0} - case 2: - return [4]byte{rb[2], rb[3]} - case 3: - return [4]byte{rb[3]} - default: - return [4]byte{} - } -} - -// Recursive case-insensitive lookup function used by n.findCaseInsensitivePath -func (n *node) findCaseInsensitivePathRec(path string, ciPath []byte, rb [4]byte, fixTrailingSlash bool) []byte { - npLen := len(n.path) - -walk: // Outer loop for walking the tree - for len(path) >= npLen && (npLen == 0 || strings.EqualFold(path[1:npLen], n.path[1:])) { - // Add common prefix to result - oldPath := path - path = path[npLen:] - ciPath = append(ciPath, n.path...) - - if len(path) == 0 { - // We should have reached the node containing the handle. - // Check if this node has a handle registered. - if n.handlers != nil { - return ciPath - } - - // No handle found. - // Try to fix the path by adding a trailing slash - if fixTrailingSlash { - for i, c := range []byte(n.indices) { - if c == '/' { - n = n.children[i] - if (len(n.path) == 1 && n.handlers != nil) || - (n.nType == catchAll && n.children[0].handlers != nil) { - return append(ciPath, '/') - } - return nil - } - } - } - return nil - } - - // If this node does not have a wildcard (param or catchAll) child, - // we can just look up the next child node and continue to walk down - // the tree - if !n.wildChild { - // Skip rune bytes already processed - rb = shiftNRuneBytes(rb, npLen) - - if rb[0] != 0 { - // Old rune not finished - idxc := rb[0] - for i, c := range []byte(n.indices) { - if c == idxc { - // continue with child node - n = n.children[i] - npLen = len(n.path) - continue walk - } - } - } else { - // Process a new rune - var rv rune - - // Find rune start. - // Runes are up to 4 byte long, - // -4 would definitely be another rune. - var off int - for max := min(npLen, 3); off < max; off++ { - if i := npLen - off; utf8.RuneStart(oldPath[i]) { - // read rune from cached path - rv, _ = utf8.DecodeRuneInString(oldPath[i:]) - break - } - } - - // Calculate lowercase bytes of current rune - lo := unicode.ToLower(rv) - utf8.EncodeRune(rb[:], lo) - - // Skip already processed bytes - rb = shiftNRuneBytes(rb, off) - - idxc := rb[0] - for i, c := range []byte(n.indices) { - // Lowercase matches - if c == idxc { - // must use a recursive approach since both the - // uppercase byte and the lowercase byte might exist - // as an index - if out := n.children[i].findCaseInsensitivePathRec( - path, ciPath, rb, fixTrailingSlash, - ); out != nil { - return out - } - break - } - } - - // If we found no match, the same for the uppercase rune, - // if it differs - if up := unicode.ToUpper(rv); up != lo { - utf8.EncodeRune(rb[:], up) - rb = shiftNRuneBytes(rb, off) - - idxc := rb[0] - for i, c := range []byte(n.indices) { - // Uppercase matches - if c == idxc { - // Continue with child node - n = n.children[i] - npLen = len(n.path) - continue walk - } - } - } - } - - // Nothing found. We can recommend to redirect to the same URL - // without a trailing slash if a leaf exists for that path - if fixTrailingSlash && path == "/" && n.handlers != nil { - return ciPath - } - return nil - } - - n = n.children[0] - switch n.nType { - case param: - // Find param end (either '/' or path end) - end := 0 - for end < len(path) && path[end] != '/' { - end++ - } - - // Add param value to case insensitive path - ciPath = append(ciPath, path[:end]...) - - // We need to go deeper! - if end < len(path) { - if len(n.children) > 0 { - // Continue with child node - n = n.children[0] - npLen = len(n.path) - path = path[end:] - continue - } - - // ... but we can't - if fixTrailingSlash && len(path) == end+1 { - return ciPath - } - return nil - } - - if n.handlers != nil { - return ciPath - } - - if fixTrailingSlash && len(n.children) == 1 { - // No handle found. Check if a handle for this path + a - // trailing slash exists - n = n.children[0] - if n.path == "/" && n.handlers != nil { - return append(ciPath, '/') - } - } - - return nil - - case catchAll: - return append(ciPath, path...) - - default: - panic("invalid node type") - } - } - - // Nothing found. - // Try to fix the path by adding / removing a trailing slash - if fixTrailingSlash { - if path == "/" { - return ciPath - } - if len(path)+1 == npLen && n.path[len(path)] == '/' && - strings.EqualFold(path[1:], n.path[1:len(path)]) && n.handlers != nil { - return append(ciPath, n.path...) - } - } - return nil -} diff --git a/xtest/main.go b/xtest/main.go index 5ea48bb..47e62f1 100644 --- a/xtest/main.go +++ b/xtest/main.go @@ -1,39 +1,16 @@ package main import ( - "github.com/gin-gonic/gin" + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/logger" "log" - "net/http/httputil" - "net/url" ) func main() { - app := gin.Default() + app := fiber.New() + app.Use(logger.New()) - app.Any("/*any", func() gin.HandlerFunc { - link := "http://dev.pro.bifrost.com" - host := "dev.pro.bifrost.com" - url, oe := url.Parse(link) - if oe != nil { - log.Fatal("url parse err:", oe) - } + app.Get("/hello", nil) - //ps := httputil.NewSingleHostReverseProxy(url) - //ps.Transport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}} - - ps := httputil.ReverseProxy{ - Rewrite: func(r *httputil.ProxyRequest) { - r.SetURL(url) - r.Out.Header.Set("Host", host) - r.In.Header.Set("Host", host) - }, - } - - return func(c *gin.Context) { - c.Request.Header.Set("Host", host) - ps.ServeHTTP(c.Writer, c.Request) - } - }()) - - log.Fatal(app.Run(":9091")) + log.Fatal(app.Listen(":9989")) }