Skip to content

Commit

Permalink
unhardcode host config & use yml file
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienVoreiter committed Sep 8, 2022
1 parent de0ef2e commit eed7f40
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 28 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ To generate config you can use `rails generate couchbase_orm:config`:

It will generate this `config/couchbase.yml` for you:

```yaml
common: &common
hosts: localhost
connection_string: couchbase://localhost
username: dev_user
password: dev_password

Expand All @@ -26,10 +27,23 @@ It will generate this `config/couchbase.yml` for you:

# set these environment variables on your production server
production:
hosts: <%= ENV['COUCHBASE_HOST'] || ENV['COUCHBASE_HOSTS'] %>
bucket: <%= ENV['COUCHBASE_BUCKET'] %>
username: <%= ENV['COUCHBASE_USER'] %>
password: <%= ENV['COUCHBASE_PASSWORD'] %>
connection_string: <%= ENV['COUCHBASE_CONNECTION_STRING'] %>
bucket: <%= ENV['COUCHBASE_BUCKET'] %>
username: <%= ENV['COUCHBASE_USER'] %>
password: <%= ENV['COUCHBASE_PASSWORD'] %>
```
## Setup without Rails
If you are not using Rails, you can configure couchbase-orm with an initializer:
```ruby
# config/initializers/couchbase_orm.rb
CouchbaseOrm::Connection.config = {
connection_string: "couchbase://localhost"
username: "dev_user"
password: "dev_password"
bucket: "dev_bucket"
}
```

Views are generated on application load if they don't exist or mismatch.
This works fine in production however by default in development models are lazy loaded.
Expand Down
28 changes: 18 additions & 10 deletions lib/couchbase-orm/connection.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@

require 'couchbase'

module CouchbaseOrm
class Connection
@options = {}
class << self
attr_accessor :options

def self.config
@@config || raise('Missing CouchbaseOrm connection config')
end

def self.config=(config)
@@config = config
end

def self.cluster
@cluster ||= begin
options = Couchbase::Cluster::ClusterOptions.new
options.authenticate(ENV["COUCHBASE_USER"], ENV["COUCHBASE_PASSWORD"])
cluster = Couchbase::Cluster.connect('couchbase://127.0.0.1', options)
end
cb_config = Couchbase::Configuration.new
cb_config.connection_string = config[:connection_string] || raise('Missing CouchbaseOrm connection string')
cb_config.username = config[:username] || raise('Missing CouchbaseOrm username')
cb_config.password = config[:password] || raise('Missing CouchbaseOrm password')
Couchbase::Cluster.connect(cb_config)
end
end

def self.bucket
@bucket ||= cluster.bucket(ENV["COUCHBASE_BUCKET"])
@bucket ||= begin
bucket_name = config[:bucket] || raise('Missing CouchbaseOrm bucket name')
cluster.bucket(bucket_name)
end
end
end
end
end
16 changes: 5 additions & 11 deletions lib/couchbase-orm/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ module Rails #:nodoc:
module Couchbase #:nodoc:
class Railtie < Rails::Railtie #:nodoc:

config.couchbase2 = ActiveSupport::OrderedOptions.new
config.couchbase2.ensure_design_documents = true
config.couchbase_orm = ActiveSupport::OrderedOptions.new
config.couchbase_orm.ensure_design_documents = true

# Maping of rescued exceptions to HTTP responses
#
Expand All @@ -44,14 +44,8 @@ def self.rescue_responses
config.action_dispatch.rescue_responses.merge!(rescue_responses)
end

# Initialize Couchbase Mode. This will look for a couchbase.yml in the
# config directory and configure Couchbase connection appropriately.
initializer 'couchbase.setup_connection' do
config_file = Rails.root.join('config', 'couchbase.yml')
if config_file.file? &&
config = YAML.load(ERB.new(File.read(config_file)).result)[Rails.env]
::CouchbaseOrm::Connection.options = config.deep_symbolize_keys
end
initializer 'couchbase_orm.setup_connection_config' do
CouchbaseOrm::Connection.config = Rails.application.config_for(:couchbase)
end

# After initialization we will warn the user if we can't find a couchbase.yml and
Expand Down Expand Up @@ -79,7 +73,7 @@ def self.rescue_responses

# Check (and upgrade if needed) all design documents
config.after_initialize do |app|
if config.couchbase2.ensure_design_documents
if config.couchbase_orm.ensure_design_documents
begin
::CouchbaseOrm::Base.descendants.each do |model|
model.ensure_design_document!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
common: &common
hosts: localhost
connection_string: couchbase://localhost
bucket: <%= bucket_name || app_name %>
username: <%= username || bucket_name || app_name %>
password: <%= password %>

Expand All @@ -13,7 +14,7 @@ test:

# set these environment variables on your production server
production:
hosts: <%%= ENV['COUCHBASE_HOST'] || ENV['COUCHBASE_HOSTS'] %>
connection_string: <%%= ENV['COUCHBASE_CONNECTION_STRING'] %>
bucket: <%%= ENV['COUCHBASE_BUCKET'] %>
username: <%%= ENV['COUCHBASE_USER'] %>
password: <%%= ENV['COUCHBASE_PASSWORD'] %>

0 comments on commit eed7f40

Please sign in to comment.