DB-Registry 🏬
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! 📚