All Projects → alphagov → Router

alphagov / Router

Licence: mit
Router in front on GOV.UK to proxy to backend servers on the single domain

Programming Languages

go
31211 projects - #10 most used programming language

Labels

Projects that are alternatives of or similar to Router

govuk-taxonomy-supervised-learning
Auto-tag govuk content to the collated legacy taxonomies
Stars: ✭ 22 (-87.85%)
Mutual labels:  govuk
Calendars
Serves /bank-holidays and /when-do-the-clocks-change on GOV.UK
Stars: ✭ 39 (-78.45%)
Mutual labels:  govuk
Static
GOV.UK static files and resources
Stars: ✭ 100 (-44.75%)
Mutual labels:  govuk
govuk-docker
GOV.UK development environment using Docker 🐳
Stars: ✭ 37 (-79.56%)
Mutual labels:  govuk
Blinken
RETIRED. Superseded by https://github.com/alphagov/blinkenjs
Stars: ✭ 13 (-92.82%)
Mutual labels:  govuk
Design Principles
Serves /design-principles on GOV.UK
Stars: ✭ 55 (-69.61%)
Mutual labels:  govuk
panopticon
Retired. App that holds some of the content on GOV.UK
Stars: ✭ 17 (-90.61%)
Mutual labels:  govuk
Magna Charta
Accessible, useful, beautiful barcharts from HTML tables.
Stars: ✭ 151 (-16.57%)
Mutual labels:  govuk
Gds Sso
OmniAuth adapter to allow apps to sign in via GOV.UK signon
Stars: ✭ 21 (-88.4%)
Mutual labels:  govuk
Govspeak
Markdown extension library for Government editors
Stars: ✭ 65 (-64.09%)
Mutual labels:  govuk
Govuk Aws
The GOV.UK repository for our Migration to AWS
Stars: ✭ 334 (+84.53%)
Mutual labels:  govuk
Govuk Component Guide
A style guide for GOV.UK Components shared between applications
Stars: ✭ 16 (-91.16%)
Mutual labels:  govuk
Fabric Scripts
GOV.UK Fabric scripts
Stars: ✭ 56 (-69.06%)
Mutual labels:  govuk
need-o-tron
No description or website provided.
Stars: ✭ 35 (-80.66%)
Mutual labels:  govuk
Govuk Puppet
Puppet manifests used to provision the main GOV.UK web stack
Stars: ✭ 109 (-39.78%)
Mutual labels:  govuk
govuk publishing components
A gem to document and distribute frontend components for GOV.UK applications
Stars: ✭ 45 (-75.14%)
Mutual labels:  govuk
Cdn Acceptance Tests
CDN Acceptance Tests
Stars: ✭ 46 (-74.59%)
Mutual labels:  govuk
Styleguides
GOV.UK coding standards and guidelines for other tools we use
Stars: ✭ 179 (-1.1%)
Mutual labels:  govuk
Smart Answers
Serves smart answers on GOV.UK
Stars: ✭ 148 (-18.23%)
Mutual labels:  govuk
Govuk Guix
Package, service and system definitions using GNU Guix for software and systems related to GOV.UK.
Stars: ✭ 58 (-67.96%)
Mutual labels:  govuk

router

This is a HTTP reverse proxy router built on top of triemux. It loads a routing table into memory from a MongoDB database and acts as a:

  • Reverse proxy, forwarding requests to and serving responses from multiple backend servers on a single domain.
  • Redirector, serving HTTP 301 and 302 redirects to new URLs.
  • Gone responder, serving HTTP 410 responses for resources that used to but no longer exist.

The sister project router-api provides a read/write interface to the underlying database and route reloading.

Some of the thinking behind the router is documented in this 2013 blog post.

Environment assumptions

Our usage of router places it behind and in front of Nginx and/or Varnish.

As such, there are some things that we are guarded against:

  • Response buffering for slow clients
  • Basic request sanitisation

And some features that we have no need to implement:

  • Access logging (but error logging is implemented)
  • SSL
  • Health check probes
  • Custom header mangling
  • Response rewriting
  • Authentication

Local Setup & Build

The router needs to be built from within a Go workspace for it to correctly identify the vendored dependencies. Assuming you already have Go and a $GOPATH set up, you can simply run go get github.com/alphagov/router to pull the source into your workspace.

Once you have the code set up correctly, building and running the application is simple:

make
./router -h

Tests

You can run all tests by running:

make test

The trie and triemux sub-packages have unit tests and benchmarks written in Go's own testing framework. To run them individually:

go test -bench=. ./trie ./triemux

The router itself doesn't really benefit from having unit tests around individual functions. Instead it has a comprehensive set of integration tests to exercise it's HTTP handling, error reporting, and performance.

These require a local MongoDB instance and can be run with:

go test ./integration_tests

Some of the integration tests are optional because they have certain environment requirements that make them unfeasible to run within CI.

Dependencies

This project uses Go Modules to vendor its dependencies. If you have a working [Go][go] development setup, you should be able to update the dependencies via:

go mod vendor

Data structure

The Router requires two MongoDB collections: routes and backends.

Routes

The routes collection uses the following data structure:

{
  "_id"           : ObjectId(),
  "route_type"    : ["prefix","exact"],
  "incoming_path" : "/url-path/here",
  "handler"       : ["backend", "redirect", "gone"],
  "disabled"      : false
}

Incoming paths with special characters must be in their % encoded form in the database (eg spaces must be stored as %20).

The behaviour of an enabled route is determined by handler. See below for extra fields corresponding to handler types.

If a route is disabled, the router will return a 503 for all matching requests. This is typically used if a service needs to be taken offline for maintenance etc.

backend handler

The backend handler causes the Router to reverse proxy to a named backend. The following extra fields are supported:

{
  "backend_id" : "backend-id-corresponding-to-backends-collection"
}

redirect handler

The redirect handler causes the Router to redirect the given incoming_path to the path stored in redirect_to. The following extra fields are supported:

{
  "redirect_to"   : "/target-of-redirect",
  "redirect_type" : ["permanent", "temporary"]
}

gone handler

The gone handler causes the Router to return a 410 response.

Backends

The backends collection uses the following data structure:

{
  "_id"         : ObjectId(),
  "backend_id"  : "arbitrary-slug-or-name",
  "backend_url" : "https://example.com:port/"
}

Error logging

The logger package stores errors in logfiles and reports them to Sentry.

Metrics

Router exposes metrics in Prometheus format using the Go prometheus client library.

See commit b443d3dd9cf776143eed270d01bd98d2233caea6 as an example for how to add a metric.

License

router is released under the MIT license, a copy of which can be found in LICENSE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].