dbrouter

package module
v0.0.0-...-8f5677a Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2020 License: Apache-2.0 Imports: 5 Imported by: 0

README

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

main.go

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

// Use this middleware in Echo.
echo.Use(middleware)

config.json

Use separate keys for every unique hostname. Supports all datastores from https://github.com/benpate/data

{
    "localhost": {"type": "mongodb", "url":"mongo-connection-string", "database":"db-name"},
    "127.0.0.1": {"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! 📚

Documentation

Overview

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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(filename string) echo.MiddlewareFunc

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

Types

type Context

type Context struct {
	echo.Context
	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.

Jump to

Keyboard shortcuts

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