https://github.com/go-http-utils/compose
:musical_score:Compose your http middlewares with happiness
https://github.com/go-http-utils/compose
Last synced: 5 months ago
JSON representation
:musical_score:Compose your http middlewares with happiness
- Host: GitHub
- URL: https://github.com/go-http-utils/compose
- Owner: go-http-utils
- License: mit
- Created: 2016-11-27T02:20:55.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-11-29T02:20:25.000Z (over 9 years ago)
- Last Synced: 2025-08-15T02:57:29.081Z (10 months ago)
- Language: Go
- Homepage: https://godoc.org/github.com/go-http-utils/compose
- Size: 3.91 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# compose
[](https://travis-ci.org/go-http-utils/compose)
[](https://coveralls.io/github/go-http-utils/compose?branch=master)
Compose your http middlewares with happiness.
## Installation
```
go get -u github.com/go-http-utils/compose
```
## Documentation
Documentation can be found here: https://godoc.org/github.com/go-http-utils/compose
## Usage
It transforms:
```go
mux := http.NewServeMux()
mux.HandleFunc("/", middleware1(middleware2(middleware3(handler1, arg3)), arg1, arg2))
mux.HandleFunc("/example", middleware3(middleware4(handler2, arg2, arg3), arg1))
http.ListenAndServe(":8080", middleware4(middleware5(mux)))
```
to:
```go
mux := http.NewServeMux()
mux.HandleFunc("/", compose.New(handler1).
Use(middleware1, arg1, arg2).
Use(middleware2).
Use(middleware3, arg3).
Handler())
mux.HandleFunc("/example", compose.New(handler2).
UseMiddlewares([]Middleware{
Middleware{Func: middleware3, Opts: []interface{arg1}},
Middleware{Func: middleware4, Opts: []interface{arg2, arg3}},
}).
Handler())
http.ListenAndServe(":8080", compose.New(mux).
Use(middleware4).
Use(middleware5).
Handler()
)
```