Plugins


plugins middleware

Plugins are handlers that satisfy the http.Handler.

They are declared in the routes section of the configuration file for example:

routes:
  foo:
    url: http://foo.example.com
    plugins: # the order of the plugins meters
        - requestid
        - waf
        - cors
        - prometheus

plugins:
  cors: ["github.com/slashquery-plugins/cors", "cors.CORS"]
  csrf: ["github.com/slashquery-plugins/csrf", "csrf.CSRF"]
  prometheus: ["github.com/slashquery-plugins/prometheus", "prometheus.Prometheus"]
  requestid: ["github.com/slashquery-plugins/requestid", "requestid.RequestID"]
  waf: ["github.com/slashquery-plugins/waf", "waf.WAF"]

The plugins section is a list of plugins each of one defined within a list, for example:

- ["github.com/path/to/myplugin", "myplugin.MyPlugin"]

In this case, the first element of the list, should be the path/url where the plugin is located and that could be used as an import path:

github.com/path/to/myplugin

The second part:

myplugin.MyPlugin

Corresponds to the way the plugin should be called.

Example of a plugin

This plugin only adds a header, in practices something more useful would be used.

package myplugin

import (
	"net/http"
)

func MyPlugin(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("my-plugin", "testing slashquery")
		next.ServeHTTP(w, r)
	})
}

make CONF=/path/to/slashquery.yml

When generating the routes, the routes.go file will include the source and Method of the plugin, for example:

// Code generated by genroutes.go at:
// 2017-06-04 18:26:31.466696196 +0200 CEST
// using data from
// examples/slashquery.yml
// DO NOT EDIT!
package slashquery

import (
    "github.com/nbari/violetear"
    "github.com/nbari/violetear/middleware"

    "github.com/path/to/myplugin"
)


// AddRoutes create router routes
func (sq *Slashquery) AddRoutes(router *violetear.Router) {
	router.Handle("test-myplugin/*", middleware.New(myplugin.MyPlugin).Then(sq.Proxy("test-myplugin")))
}

The current Makefile uses goimports to add or remove import lines.

comments powered by Disqus