Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetHeaderName ¶
func SetHeaderName(name string)
Changes the expected header or query param for the API key.
Types ¶
type ContextValue ¶
type ContextValue string
Use custom type as context key to avoid collisions.
var ConnectionContextKey ContextValue = "connection"
The context key used to access each database connection from req context.
var DatabaseContextKey ContextValue = "database"
The context key used to access each database name from req context.
type DBResolver ¶
type DBResolver struct {
// contains filtered or unexported fields
}
DBResolver stores all database connections and config. Call ResolveConnection to get the underlying database connection for an API key.
func New ¶
func New(c DatabaseConfig, options ...Option) (*DBResolver, error)
Initialize a new DBResolver with a database config, driver, and *gorm.Config. The driver argument should be one of "sqlite", "mysql", or "postgres". Default ApiKey header/query expected is x-api-key. call dbresolver.SetHeaderName to change it.
func (*DBResolver) AutoMigrate ¶
func (resolver *DBResolver) AutoMigrate(models []interface{}, errorCallback func(error) bool)
Runs *gorm.DB.AutoMigrate(...) on all databases, creating all the tables. The error callback is only called if error is not nil. The error callback allows you to ignore certain errors and return true. If the error callback returns false this function will panic.
func (*DBResolver) DB ¶
func (resolver *DBResolver) DB(r *http.Request) *gorm.DB
DB retrieves the current database connection from the request context. This assumes that the handler was run after the middleware. Otherwise, this will panic.
func (*DBResolver) DBName ¶
func (resolver *DBResolver) DBName(r *http.Request) string
ResolveDatabase resolves the database name from the request APIKey.
func (*DBResolver) Middleware ¶
func (resolver *DBResolver) Middleware(next http.Handler) http.Handler
type DatabaseConfig ¶
DatabaseConfig links API keys to database objects in a map data structure.
func ConfigFromYAMLFile ¶
func ConfigFromYAMLFile(filename string) (DatabaseConfig, error)
DatabaseConfigFromYAML parses a yaml file and returns a DatabaseConfig.
func ConfigFromYAMLString ¶
func ConfigFromYAMLString(yamlStr string) (DatabaseConfig, error)
DatabaseConfigFromYAML parses a YAML-formatted string and returns a DatabaseConfig.
func (DatabaseConfig) DatabaseDrivers ¶
func (dbconfig DatabaseConfig) DatabaseDrivers() []DBDriver
Returns a struct of DBDrivers with each having a Database and Driver field.