https://github.com/covrom/stdchi
go 1.22+ standard http router wrapper like chi router
https://github.com/covrom/stdchi
api chi framework go golang-library golang-package rest router
Last synced: 2 months ago
JSON representation
go 1.22+ standard http router wrapper like chi router
- Host: GitHub
- URL: https://github.com/covrom/stdchi
- Owner: covrom
- License: mit
- Created: 2024-08-24T05:40:15.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-27T06:16:53.000Z (over 1 year ago)
- Last Synced: 2024-08-27T10:21:51.270Z (over 1 year ago)
- Topics: api, chi, framework, go, golang-library, golang-package, rest, router
- Language: Go
- Homepage:
- Size: 101 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# stdchi
Go 1.22+ standard http router wrapper with API like [chi](https://github.com/go-chi/chi) router.
It uses a [new syntax](https://go.dev/blog/routing-enhancements) for path values within groups and subroutes.
All of 'chi' routing syntax is supported. The middleware stack and path values providing is more efficient than chi.
It supports lazy mounting. You can create an independent API and then mount it to another router.
Example:
```go
import "github.com/covrom/stdchi"
// ...
r := stdchi.NewRouter()
r.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Route /sharing")
r.Write(os.Stdout)
h.ServeHTTP(w, r)
})
})
r.Get("/{hash}", func(w http.ResponseWriter, r *http.Request) {
v := r.PathValue("hash")
w.Write([]byte(fmt.Sprintf("/%s", v)))
fmt.Println("Done GET /{hash}")
})
r.Route("/{hash}/share", func(r Router) {
r.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Route /{hash}/share/")
r.Write(os.Stdout)
h.ServeHTTP(w, r)
})
})
r.Get("/{$}", func(w http.ResponseWriter, r *http.Request) {
v := r.PathValue("hash")
w.Write([]byte(fmt.Sprintf("/%s/share", v)))
fmt.Println("Done GET /{hash}/share/")
})
r.Get("/{network}", func(w http.ResponseWriter, r *http.Request) {
v := r.PathValue("hash")
n := r.PathValue("network")
w.Write([]byte(fmt.Sprintf("/%s/share/%s", v, n)))
fmt.Println("Done GET /{hash}/share/{network}")
})
})
m := stdchi.NewRouter()
m.Mount("/sharing", r)
m.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Route /")
r.Write(os.Stdout)
h.ServeHTTP(w, r)
})
})
// ...
http.ListenAndServe(":8080", m)
```