DB-Registry 🏬

GoDoc Go Report Card Build Status Codecov

Simple Middleware for Multi-Tennant Web Servers

dbregistry is a simple middleware for the Echo router that uses a configuration file to identify the correct datasource to use for any given hostname. This is an experiment in multi-tennant architecture, using a completely separate database connection for every tennant instead of using keys in the database itself.

The middleware adds a data.DataStore object into the echo request context, so that the rest of the handler pipeline can access the database without any further configuration.

Example Usage


// Initialize the middleware with the filename of your configuration file.
middleware := dbregistry.New(configFilename)

// Use this middleware in Echo.


Use separate keys for every unique hostname. Supports all datastores from

    "localhost": {"type": "mongodb", "url":"mongo-connection-string", "database":"db-name"},
    "": {"type": "memory"}

To-Do List

This is a very early-stage prototype for this concept, with many more enhancements in the works, once I get some real-world usage with it.

  • Real load and stress-testing. The goal is for this to work on very large systems.
  • Default connection that matches all hostnames.
  • Better connection pooling - for instance, different physical databases on the same mongodb server might be able to share connections.
  • Maximum number of connections in a pool, with LRU eviction, if necessary.

Pull Requests Welcome

This library is a work in progress, and will benefit from your experience reports, use cases, and contributions. If you have an idea for making this library better, send in a pull request. We're all in this together! 📚

Expand ▾ Collapse ▴



Package dbrouter provides a middleware for the echo router that uses the hostname from an HTTP request to load the correct database driver for that doamin.

Configuration files can be anything that works with Viper (json is recommended). The configuration file should be a map of key/value pairs. The keys are the individual host names that you want to configure. The values should contain the TYPE of database connection you want to use, along with additional required connection information.



This section is empty.


This section is empty.


func New

func New(filename string) echo.MiddlewareFunc

New generates an Echo middleware that locates the appropriate datasource to use for the current request.


type Context

type Context struct {
	Datastore data.Datastore

Context is an extension of the echo.Context interface that includes a Datastore value. The DBRouter middleware populates this based on the configuration file.

Source Files