https://github.com/donatj/jqmux
Go HTTP Multiplexer routing on JSON http request bodies via jq queries
https://github.com/donatj/jqmux
go http jq json mux router
Last synced: about 1 month ago
JSON representation
Go HTTP Multiplexer routing on JSON http request bodies via jq queries
- Host: GitHub
- URL: https://github.com/donatj/jqmux
- Owner: donatj
- License: mit
- Created: 2019-06-11T21:53:55.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-10-07T15:35:47.000Z (over 4 years ago)
- Last Synced: 2025-02-03T06:38:43.175Z (over 1 year ago)
- Topics: go, http, jq, json, mux, router
- Language: Go
- Homepage:
- Size: 22.5 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# jqmux
[](https://github.com/donatj/jqmux/actions/workflows/ci.yml)
[](https://godoc.org/github.com/donatj/jqmux)
[](https://goreportcard.com/report/github.com/donatj/jqmux)
An HTTP multiplexer which routes based on the incoming requests JSON body using the [jq syntax](https://stedolan.github.io/jq/manual/) of JSON value filtering.
A particularly fruitful usecase for this is webhook routing.
## Limitations
This utilizes the library [github.com/savaki/jq](https://github.com/savaki/jq) for it's jq parsing. While it is very fast it is not fully featured, so more complicated jq queries might not work. More information about it's workings and expected values can be found there.
## Example
The first handler is executed if the body matches `{"action": "opened"}` whereas the second is executed if the body matches `{"action": "synchronize"}`
```go
package main
import (
"net/http"
"github.com/donatj/jqmux"
)
func main() {
mux := jqmux.NewMux()
mux.HandleFunc(`.action`, `"opened"`, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`body "action" was "opened"`))
})
mux.HandleFunc(`.action`, `"synchronize"`, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`body "action" was "synchronize"`))
})
http.ListenAndServe(":80", mux)
}
```