package module
v2.0.0-...-cabcc6b Latest Latest

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

Go to latest
Published: Aug 26, 2020 License: Apache-2.0 Imports: 15 Imported by: 0


Router Plugin

The router plugin is a HTTP handler plugin for the Micro API which enables you to define routes via Go Config. This is dynamic configuration that can then be leveraged via anything that implements the go-config interface e.g file, etcd, consul or the config service.


  • Request Matching
  • Weighted Routing
  • Reverse Proxying
  • Priority Rules
  • Configurable via Go Config
  • Pluggable via micro/plugins


  • Regex Matching Host/Path


Register the plugin before building Micro

package main

import (

func init() {


Configuring the router is done via a go-Config source. Here's an example using the File source.

// Create Config Source
f := file.NewSource(
	// Use routes.json file

// Create Config
c := config.NewConfig(
	// With Source

// Create Router
r := router.NewRouter(
	// With Config


Routes are used to config request to match and the response to return. Here's an example.

	"api": {
		"routes": [
				"request": {
					"method": "GET",
					"host": "",
					"path": "/"
				"response": {
					"status_code": 302,
					"header": {
						"location": ""
				"weight": 1.0
				"request": {
					"method": "POST",
					"host": "",
					"path": "/foo"
				"response": {
					"status_code": 301,
					"header": {
						"location": ""
				"weight": 1.0



Package router is a micro plugin for defining HTTP routes



This section is empty.


View Source
var (
	// Default config source file
	DefaultFile   = "routes.json"
	DefaultPath   = []string{"api"}
	DefaultLogger = logrus.New()


func NewRouter

func NewRouter(opts ...Option) plugin.Plugin


type Option

type Option func(o *Options)

func Config

func Config(c config.Config) Option

type Options

type Options struct {
	Config config.Config

type Request

type Request struct {
	Method string            `json:"method"`
	Header map[string]string `json:"header"`
	Host   string            `json:"host"`
	Path   string            `json:"path"`
	Query  map[string]string `json:"query"`

Request describes the expected request and will attempt to match all fields specified

type Response

type Response struct {
	Status     string            `json:"status"`
	StatusCode int               `json:"status_code"`
	Header     map[string]string `json:"header"`
	Body       []byte            `json:"body"`

Response is put into the http.Response for a Request

type Route

type Route struct {
	Request  Request  `json:"request"`
	Response Response `json:"response"`
	ProxyURL URL      `json:"proxy_url"`
	Priority int      `json:"priority"` // 0 is highest. Used for ordering routes
	Weight   float64  `json:"weight"`   // percentage weight between 0 and 1.0
	Type     string   `json:"type"`     // proxy or response. Response is default
	Insecure bool     `json:"bool"`     // allow insecure certificates

Route describes a single route which is matched on Request and if so, will return the Response

func (Route) Match

func (r Route) Match(req *http.Request) bool

func (Route) Write

func (r Route) Write(w http.ResponseWriter, req *http.Request)

type Routes

type Routes struct {
	Routes []Route `json:"routes"`

Routes is the config expected to be loaded

type URL

type URL struct {
	Scheme string `json:"scheme"`
	Host   string `json:"host"`
	Path   string `json:"path"`

Jump to

Keyboard shortcuts

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