Skip to content

Instantly share code, notes, and snippets.

@joaodrp
Created January 7, 2012 20:01
Show Gist options
  • Save joaodrp/1575846 to your computer and use it in GitHub Desktop.
Save joaodrp/1575846 to your computer and use it in GitHub Desktop.
#
# Inside Config file
#
backend_map = {'mongodb' => MyProject::Backends::MongoDB,
'mysql' => MyProject::Backends::MySQL}
conf = load_some_yaml_file_with_a_backend_URI_parameter()
uri = conf[:backend]
backend_name = uri.split(':').first
config['backend_class'] = backend_map[backend_name]
config['uri'] = uri
config['db'] =
if backend_name == 'mysql'
EventMachine::Synchrony::ConnectionPool.new(size: 10) do
Mysql2::EM::Client.new(...)
end
elsif backend_name == 'mongodb'
EventMachine::Synchrony::ConnectionPool.new(size: 10) do
# setup mongo connection
end
end
# -----------------------------
#
# Inside Goliath server file
#
# after modify my backend classes to accept a 'conn' instance variable and use that inside of its methods instead
# of create a connection at each call
DB = backend_class.connect(uri: uri, conn: db) # and use it inside of routes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment