Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-12-05 02:54:46 +01:00
forgejo/vendor/github.com/go-openapi/runtime/middleware/denco
6543 d1353e1f7c
Vendor Update (#14496)
* update code.gitea.io/sdk/gitea v0.13.1 -> v0.13.2

* update github.com/go-swagger/go-swagger v0.25.0 -> v0.26.0

* update github.com/google/uuid v1.1.2 -> v1.2.0

* update github.com/klauspost/compress v1.11.3 -> v1.11.7

* update github.com/lib/pq 083382b7e6fc -> v1.9.0

* update github.com/markbates/goth v1.65.0 -> v1.66.1

* update github.com/mattn/go-sqlite3 v1.14.4 -> v1.14.6

* update github.com/mgechev/revive 246eac737dc7 -> v1.0.3

* update github.com/minio/minio-go/v7 v7.0.6 -> v7.0.7

* update github.com/niklasfasching/go-org v1.3.2 -> v1.4.0

* update github.com/olivere/elastic/v7 v7.0.21 -> v7.0.22

* update github.com/pquerna/otp v1.2.0 -> v1.3.0

* update github.com/xanzy/go-gitlab v0.39.0 -> v0.42.0

* update github.com/yuin/goldmark v1.2.1 -> v1.3.1
2021-01-28 17:56:38 +01:00
..
LICENSE Use vendored go-swagger (#8087) 2019-09-04 22:53:54 +03:00
README.md Use vendored go-swagger (#8087) 2019-09-04 22:53:54 +03:00
router.go Vendor Update (#14496) 2021-01-28 17:56:38 +01:00
server.go Use vendored go-swagger (#8087) 2019-09-04 22:53:54 +03:00
util.go Use vendored go-swagger (#8087) 2019-09-04 22:53:54 +03:00

Denco Build Status

The fast and flexible HTTP request router for Go.

Denco is based on Double-Array implementation of Kocha-urlrouter. However, Denco is optimized and some features added.

Features

Installation

go get -u github.com/go-openapi/runtime/middleware/denco

Using as HTTP request multiplexer

package main

import (
    "fmt"
    "log"
    "net/http"

    "github.com/go-openapi/runtime/middleware/denco"
)

func Index(w http.ResponseWriter, r *http.Request, params denco.Params) {
    fmt.Fprintf(w, "Welcome to Denco!\n")
}

func User(w http.ResponseWriter, r *http.Request, params denco.Params) {
    fmt.Fprintf(w, "Hello %s!\n", params.Get("name"))
}

func main() {
    mux := denco.NewMux()
    handler, err := mux.Build([]denco.Handler{
        mux.GET("/", Index),
        mux.GET("/user/:name", User),
        mux.POST("/user/:name", User),
    })
    if err != nil {
        panic(err)
    }
    log.Fatal(http.ListenAndServe(":8080", handler))
}

Using as URL router

package main

import (
	"fmt"

	"github.com/go-openapi/runtime/middleware/denco"
)

type route struct {
	name string
}

func main() {
	router := denco.New()
	router.Build([]denco.Record{
		{"/", &route{"root"}},
		{"/user/:id", &route{"user"}},
		{"/user/:name/:id", &route{"username"}},
		{"/static/*filepath", &route{"static"}},
	})

	data, params, found := router.Lookup("/")
	// print `&main.route{name:"root"}, denco.Params(nil), true`.
	fmt.Printf("%#v, %#v, %#v\n", data, params, found)

	data, params, found = router.Lookup("/user/hoge")
	// print `&main.route{name:"user"}, denco.Params{denco.Param{Name:"id", Value:"hoge"}}, true`.
	fmt.Printf("%#v, %#v, %#v\n", data, params, found)

	data, params, found = router.Lookup("/user/hoge/7")
	// print `&main.route{name:"username"}, denco.Params{denco.Param{Name:"name", Value:"hoge"}, denco.Param{Name:"id", Value:"7"}}, true`.
	fmt.Printf("%#v, %#v, %#v\n", data, params, found)

	data, params, found = router.Lookup("/static/path/to/file")
	// print `&main.route{name:"static"}, denco.Params{denco.Param{Name:"filepath", Value:"path/to/file"}}, true`.
	fmt.Printf("%#v, %#v, %#v\n", data, params, found)
}

See Godoc for more details.

Getting the value of path parameter

You can get the value of path parameter by 2 ways.

  1. Using denco.Params.Get method
  2. Find by loop
package main

import (
    "fmt"

    "github.com/go-openapi/runtime/middleware/denco"
)

func main() {
    router := denco.New()
    if err := router.Build([]denco.Record{
        {"/user/:name/:id", "route1"},
    }); err != nil {
        panic(err)
    }

    // 1. Using denco.Params.Get method.
    _, params, _ := router.Lookup("/user/alice/1")
    name := params.Get("name")
    if name != "" {
        fmt.Printf("Hello %s.\n", name) // prints "Hello alice.".
    }

    // 2. Find by loop.
    for _, param := range params {
        if param.Name == "name" {
            fmt.Printf("Hello %s.\n", name) // prints "Hello alice.".
        }
    }
}

URL patterns

Denco's route matching strategy is "most nearly matching".

When routes /:name and /alice have been built, URI /alice matches the route /alice, not /:name. Because URI /alice is more match with the route /alice than /:name.

For more example, when routes below have been built:

/user/alice
/user/:name
/user/:name/:id
/user/alice/:id
/user/:id/bob

Routes matching are:

/user/alice      => "/user/alice" (no match with "/user/:name")
/user/bob        => "/user/:name"
/user/naoina/1   => "/user/:name/1"
/user/alice/1    => "/user/alice/:id" (no match with "/user/:name/:id")
/user/1/bob      => "/user/:id/bob"   (no match with "/user/:name/:id")
/user/alice/bob  => "/user/alice/:id" (no match with "/user/:name/:id" and "/user/:id/bob")

Limitation

Denco has some limitations below.

  • Number of param records (such as /:name) must be less than 2^22
  • Number of elements of internal slice must be less than 2^22

Benchmarks

cd $GOPATH/github.com/go-openapi/runtime/middleware/denco
go test -bench . -benchmem

License

Denco is licensed under the MIT License.