jqmux

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2019 License: MIT Imports: 4 Imported by: 0

README

jqmux

Build Status GoDoc Go Report Card

An HTTP multiplexer which routes based on the incoming requests JSON body using the jq syntax of JSON value filtering.

A particularly fruitful usecase for this is webhook routing.

This utilizes the library github.com/savaki/jq and more information about 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"}

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)
}

Documentation

Overview

Package jqmux offers an HTTP multiplexer which routes based on the incoming requests JSON body using the jq syntax of JSON value filtering

Limitations

* Supports jq syntax to the level of https://github.com/savaki/jq

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultErrorHandler

func DefaultErrorHandler(err error) http.Handler

DefaultErrorHandler is the default error handler when calling NewMux

func DefaultNotFoundHandler

func DefaultNotFoundHandler(w http.ResponseWriter, r *http.Request)

DefaultNotFoundHandler is the default http.Handler when calling NewMux

Types

type JqMux

type JqMux struct {
	// contains filtered or unexported fields
}

JqMux is an HTTP request multiplexer. It matches the body of each incoming request against a list of registered jq patterns and calls the handler for the first pattern that matches given value.

func NewMux

func NewMux(options ...Option) *JqMux

NewMux allocates and returns a new JqMux.

func (*JqMux) Handle

func (mux *JqMux) Handle(pattern, match string, handler http.Handler)

Handle registers the handler for the given pattern and match value. If the given jq pattern does not compile, Handle panics.

func (*JqMux) HandleFunc

func (mux *JqMux) HandleFunc(pattern, match string, handler func(http.ResponseWriter, *http.Request))

HandleFunc is a convenience method which casts the given handler to http.HandlerFunc and registers the casted handler

func (*JqMux) ServeHTTP

func (mux *JqMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Option

type Option func(*JqMux)

Option sets an option of the passed JqMux

func OptionErrorHandler

func OptionErrorHandler(handler func(error) http.Handler) Option

OptionErrorHandler configures a custom error handler

func OptionNotFoundHandler

func OptionNotFoundHandler(handler http.Handler) Option

OptionNotFoundHandler configures the http.Hander called on no matches

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL