the Steam configuration now accepts to pass the properties of an adapter

did committed Mar 15, 2015
commit 9777fab0ec2c75695b84bb4483d762432e9f7ef4
Showing 17 changed files with 98 additions and 40 deletions
Rakefile +0 -1
@@ @@ -31,7 +31,6 @@ end
RSpec::Core::RakeTask.new('spec:integration') do |spec|
spec.pattern = 'spec/integration/**/*_spec.rb'
- # spec.pattern = 'spec/integration/{mongodb,repositories}/**/*_spec.rb'
end
RSpec::Core::RakeTask.new('spec:unit') do |spec|
bin/steam.rb +51 -6
@@ @@ -6,22 +6,68 @@ require 'bundler/setup'
Bundler.require
require 'thin'
+ require 'optparse'
+
+ options = {
+ adapter: {
+ name: :filesystem,
+ path: ENV['SITE_PATH'] || File.join(File.dirname(__FILE__), '../spec/fixtures/default')
+ },
+ log_file: nil
+ }
+
+ OptionParser.new do |opts|
+ opts.banner = 'Usage: steam.rb [options]'
+
+ # Filesystem adapter
+ opts.on('-p', '--path PATH', 'Serve a Wagon site from a path in your filesystem') do |path|
+ options[:adapter][:path] = File.expand_path(path)
+ options[:assets_path] = File.expand_path(File.join(path, 'public'))
+ options[:database] = options[:hosts] = nil
+ end
+
+ # MongoDB adapter
+ opts.on('-d', '--database DATABASE', 'Serve a Wagon site from a MongoDB database') do |database|
+ options[:adapter].merge!(name: :'mongoDB', database: database)
+ options[:adapter][:hosts] ||= ['127.0.0.1']
+ options[:adapter].delete(:path)
+ end
+ opts.on('--hosts x,y,z', Array, 'Specify the MongoDB hosts') do |hosts|
+ options[:adapter][:hosts] = hosts
+ end
+
+ # Assets path
+ opts.on('-a', '--assets-path ASSETS_PATH', 'Tell Steam where to find the assets (if local)') do |path|
+ options[:assets_path] = path
+ end
+
+ # Logger
+ opts.on('-l', '--log-file LOG_FILE', 'Log file of the Steam server') do |file|
+ options[:log_file] = File.expand_path(file)
+ end
+
+ # Help
+ opts.on_tail('-h', '--help', 'Show this message') do
+ puts opts
+ exit
+ end
+
+ end.parse!
require_relative '../lib/locomotive/steam'
require_relative '../lib/locomotive/steam/server'
- path = File.expand_path(ARGV[0] || ENV['SITE_PATH'] || File.join(File.dirname(__FILE__), '../spec/fixtures/default'))
-
Locomotive::Steam.configure do |config|
config.mode = :test
- config.site_path = path
- config.serve_assets = true
+ config.adapter = options[:adapter]
+ config.serve_assets = options[:assets_path].present?
+ config.assets_path = options[:assets_path]
config.minify_assets = false
end
Locomotive::Common.reset
Locomotive::Common.configure do |config|
- config.notifier = Locomotive::Common::Logger.setup(File.join(path, 'log/steam.log'))
+ config.notifier = Locomotive::Common::Logger.setup(options[:log_file])
end
app = Locomotive::Steam::Server.to_app
@@ @@ -36,4 +82,3 @@ server.start
# Rack::Handler::WEBrick.run app
Locomotive::Common::Logger.info 'Server started...'
-
locomotive/steam/adapters/filesystem.rb b/lib/locomotive/steam/adapters/filesystem.rb +5 -1
@@ @@ -10,7 +10,7 @@ require_relative_all 'filesystem/sanitizers'
module Locomotive::Steam
- class FilesystemAdapter < Struct.new(:site_path)
+ class FilesystemAdapter < Struct.new(:options)
include Morphine
include Locomotive::Steam::Adapters::Concerns::Key
@@ @@ -109,6 +109,10 @@ module Locomotive::Steam
"Locomotive::Steam::Adapters::Filesystem::#{type}::#{_name}".constantize
end
+ def site_path
+ options.respond_to?(:has_key?) ? options[:path] : options
+ end
+
end
end
locomotive/steam/adapters/mongodb.rb b/lib/locomotive/steam/adapters/mongodb.rb +9 -1
@@ @@ -7,7 +7,7 @@ require_relative 'mongodb/dataset'
module Locomotive::Steam
- class MongoDBAdapter < Struct.new(:database, :hosts)
+ class MongoDBAdapter < Struct.new(:options)
def all(mapper, query)
dataset(mapper, query)
@@ @@ -50,6 +50,14 @@ module Locomotive::Steam
end
end
+ def database
+ options[:database]
+ end
+
+ def hosts
+ options[:hosts]
+ end
+
end
end
locomotive/steam/configuration.rb b/lib/locomotive/steam/configuration.rb +8 -12
@@ @@ -13,12 +13,14 @@ module Locomotive
attr_accessor :mode
def mode; @mode || :production; end
- # Steam is also able to serve a local Wagon site. The following property
- # should point to the root path of the site.
+ # Steam needs an adapter in order to fetch the site content (site itself, pages, content entries, ...etc).
+ # By default, Steam is shipped with 2 adapters:
+ # - Filesystem (options: path). the path option should point to the root path of the site
+ # - MongoDB (options: database, hosts)
#
- # default: nil
+ # default: { name: :filesytem, path: nil }
#
- attr_accessor :site_path
+ attr_accessor :adapter
# Manage the list of middlewares used by the rack stack.
#
@@ @@ -59,16 +61,10 @@ module Locomotive
def serve_assets; @serve_assets.nil? ? true : @serve_assets; end
# Path to the assets (if Steam serves the assets).
- # If the site_path property is not nil, the assets_path
- # will point to the "public" sub folder of the site.
#
# default: nil
#
attr_accessor :assets_path
- def assets_path
- return @assets_path if @assets_path
- site_path ? File.join(site_path, 'public') : nil
- end
# If java is installed and if this option is enabled,
# then YUI::JavaScriptCompressor and YUI::CssCompressor are used to minify the css and the javascript.
@@ @@ -107,8 +103,8 @@ module Locomotive
# Locomotive::Steam.configure do |config|
#
# config.services_hook = -> (services) {
- # require 'my_repositories'
- # services.repositories = MyRepositories.new
+ # require 'my_custom_site_finder'
+ # services.site_finder = MyCustomSiteFinder.new
# }
#
# end
locomotive/steam/repositories.rb b/lib/locomotive/steam/repositories.rb +8 -2
@@ @@ -8,8 +8,7 @@ module Locomotive
include Morphine
register :adapter do
- require_relative 'adapters/filesystem'
- Steam::FilesystemAdapter.new(configuration.site_path)
+ build_adapter(configuration.adapter)
end
register :site do
@@ @@ -40,6 +39,13 @@ module Locomotive
TranslationRepository.new(adapter, current_site, locale)
end
+ def build_adapter(options)
+ name = ((options || {})[:name] || :filesystem).to_s
+ require_relative "adapters/#{name}"
+ klass = "Locomotive::Steam::#{name.camelize}Adapter".constantize
+ klass.new(options)
+ end
+
end
end
end
locomotive/steam/repositories/site_repository.rb b/lib/locomotive/steam/repositories/site_repository.rb +2 -1
@@ @@ -11,7 +11,8 @@ module Locomotive
end
def by_domain(domain)
- first { where('domains.in' => domain) }
+ conditions = { k(:domains, :in) => [*domain] }
+ first { where(conditions) }
end
def by_handle_or_domain(handle, domain)
spec/fixtures/default/config/site.yml +1 -1
@@ @@ -2,7 +2,7 @@ name: Sample website
subdomain: sample
- domains: ['example.org', 'sample.example.com']
+ domains: ['example.org', 'sample.example.com', 'sample.lvh.me']
locales: ['en', 'fr', 'nb']
spec/integration/repositories/content_entry_repository_spec.rb +1 -1
@@ @@ -58,7 +58,7 @@ describe Locomotive::Steam::ContentEntryRepository do
it_should_behave_like 'a repository' do
let(:site_id) { BSON::ObjectId.from_string('54eb49c12475804b2b000002') }
- let(:adapter) { Locomotive::Steam::MongoDBAdapter.new('steam_test', ['127.0.0.1:27017']) }
+ let(:adapter) { Locomotive::Steam::MongoDBAdapter.new(database: 'steam_test', hosts: ['127.0.0.1:27017']) }
let(:entry_id) { BSON::ObjectId.from_string('54eb4bbc2475804b2b00003f') }
end
spec/integration/repositories/content_type_repository_spec.rb +1 -1
@@ @@ -47,7 +47,7 @@ describe Locomotive::Steam::ContentTypeRepository do
it_should_behave_like 'a repository' do
let(:site_id) { BSON::ObjectId.from_string('54eb49c12475804b2b000002') }
- let(:adapter) { Locomotive::Steam::MongoDBAdapter.new('steam_test', ['127.0.0.1:27017']) }
+ let(:adapter) { Locomotive::Steam::MongoDBAdapter.new(database: 'steam_test', hosts: ['127.0.0.1:27017']) }
end
spec/integration/repositories/page_repository_spec.rb +1 -1
@@ @@ -74,7 +74,7 @@ describe Locomotive::Steam::PageRepository do
it_should_behave_like 'a repository' do
let(:site_id) { BSON::ObjectId.from_string('54eb49c12475804b2b000002') }
- let(:adapter) { Locomotive::Steam::MongoDBAdapter.new('steam_test', ['127.0.0.1:27017']) }
+ let(:adapter) { Locomotive::Steam::MongoDBAdapter.new(database: 'steam_test', hosts: ['127.0.0.1:27017']) }
end
spec/integration/repositories/site_repository_spec.rb +6 -1
@@ @@ -19,11 +19,16 @@ describe Locomotive::Steam::SiteRepository do
it { expect(subject.name).to eq 'Sample website' }
end
+ describe '#by_domain' do
+ subject { repository.by_domain('sample.lvh.me') }
+ it { expect(subject).not_to eq nil }
+ end
+
end
context 'MongoDB' do
- let(:adapter) { Locomotive::Steam::MongoDBAdapter.new('steam_test', ['127.0.0.1:27017']) }
+ let(:adapter) { Locomotive::Steam::MongoDBAdapter.new(database: 'steam_test', hosts: ['127.0.0.1:27017']) }
it_behaves_like 'a repository'
spec/integration/repositories/snippet_repository_spec.rb +1 -1
@@ @@ -28,7 +28,7 @@ describe Locomotive::Steam::SnippetRepository do
it_should_behave_like 'a repository' do
let(:site_id) { BSON::ObjectId.from_string('54eb49c12475804b2b000002') }
- let(:adapter) { Locomotive::Steam::MongoDBAdapter.new('steam_test', ['127.0.0.1:27017']) }
+ let(:adapter) { Locomotive::Steam::MongoDBAdapter.new(database: 'steam_test', hosts: ['127.0.0.1:27017']) }
end
spec/integration/repositories/theme_asset_repository_spec.rb +1 -1
@@ @@ -11,7 +11,7 @@ describe Locomotive::Steam::ThemeAssetRepository do
context 'MongoDB' do
let(:site_id) { BSON::ObjectId.from_string('54eb49c12475804b2b000002') }
- let(:adapter) { Locomotive::Steam::MongoDBAdapter.new('steam_test', ['127.0.0.1:27017']) }
+ let(:adapter) { Locomotive::Steam::MongoDBAdapter.new(database: 'steam_test', hosts: ['127.0.0.1:27017']) }
describe '#url_for' do
subject { repository.url_for('stylesheets/application.css') }
spec/integration/repositories/translation_repository_spec.rb +1 -1
@@ @@ -28,7 +28,7 @@ describe Locomotive::Steam::TranslationRepository do
it_should_behave_like 'a repository' do
let(:site_id) { BSON::ObjectId.from_string('54eb49c12475804b2b000002') }
- let(:adapter) { Locomotive::Steam::MongoDBAdapter.new('steam_test', ['127.0.0.1:27017']) }
+ let(:adapter) { Locomotive::Steam::MongoDBAdapter.new(database: 'steam_test', hosts: ['127.0.0.1:27017']) }
end
spec/support/helpers.rb +2 -1
@@ @@ -24,7 +24,8 @@ module Spec
Locomotive::Steam.configure do |config|
config.mode = :test
- config.site_path = default_fixture_site_path
+ config.adapter = { name: :filesystem, path: default_fixture_site_path }
+ config.assets_path = File.expand_path(File.join(default_fixture_site_path, 'public'))
config.serve_assets = true
config.minify_assets = true
end
spec/unit/configuration_spec.rb +0 -7
@@ @@ -26,11 +26,4 @@ describe Locomotive::Steam::Configuration do
end
- describe 'overriding a method' do
-
- before { subject.site_path = '/42' }
- it { expect(subject.assets_path).to eq('/42/public') }
-
- end
-
end