Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/twiny/sigma
a small wrapper around go-chi HTTP router.
https://github.com/twiny/sigma
go-chi golang http-router rest-api
Last synced: 21 days ago
JSON representation
a small wrapper around go-chi HTTP router.
- Host: GitHub
- URL: https://github.com/twiny/sigma
- Owner: twiny
- License: mit
- Created: 2021-12-10T18:11:54.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-10-14T11:50:52.000Z (about 1 year ago)
- Last Synced: 2024-06-20T00:41:11.916Z (7 months ago)
- Topics: go-chi, golang, http-router, rest-api
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 8
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sigma
a small wrapper around [go-chi](https://github.com/go-chi/chi) HTTP router.
**NOTE**: This package is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please [create a new issue](https://github.com/twiny/sigma/issues/new).
## API
Router methods.
```go
// Router
type Router interface {
Endpoint(method, pattern string, handler http.HandlerFunc)
Use(middlewares ...func(next http.Handler) http.Handler)
Group(pattern string, fn func(r Router))
Static(pattern, path string)
NotFound(handler http.HandlerFunc)
NotAllowed(handler http.HandlerFunc)
ServeHTTP(w http.ResponseWriter, r *http.Request)
}
```
Param returns the url parameter from a http.Request object.
```go
Param(r *http.Request, key string) string
```## Example
```go
package mainimport (
"log"
"net/http""github.com/twiny/sigma"
)func main() {
srv := sigma.NewServer(":1234")
defer srv.Stop()router := srv.NewRouter()
// custom 404 not found handler
router.NotFound(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 - Not Found"))
})// custom 405 not allowed handler
router.NotAllowed(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("405 - Method Not Allowed"))
})// middleware
router.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Add("x-key-1", "main router")
next.ServeHTTP(w, r)
})
})// endpoints
router.Endpoint(http.MethodGet, "/hello/{name}", func(w http.ResponseWriter, r *http.Request) {
name := sigma.Param(r, "name")
w.Write([]byte("Hello World " + name))
})// sub router
router.Group("/v1", func(r sigma.Router) {
// sub router middleware
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Add("x-key-2", "sub router")
next.ServeHTTP(w, r)
})
})
//
r.Endpoint(http.MethodGet, "/hello/{name}", func(w http.ResponseWriter, r *http.Request) {
name := sigma.Param(r, "name")
w.Write([]byte("Hello World v1 " + name))
})
})// static files
router.Static("/web", "./static")log.Fatal(srv.Start())
}
```