Skip to content

Commit

Permalink
Merge pull request #82 from ruby-rdf/feature/config
Browse files Browse the repository at this point in the history
Open Lamprey to configuration
  • Loading branch information
Thomas Johnson authored Feb 17, 2017
2 parents a1065ec + e20252a commit 9baacfd
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 38 deletions.
78 changes: 73 additions & 5 deletions app/lamprey.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ class RDF::Lamprey < Sinatra::Base
use Rack::ConditionalGet
use Rack::LDP::Requests

# Set defaults in case user has not configured values
configure do
set :repository, RDF::Repository.new
end

get '/*' do
RDF::LDP::Container.new(RDF::URI(request.url), settings.repository)
.create(StringIO.new, 'text/turtle') if settings.repository.empty?
Expand Down Expand Up @@ -50,5 +45,78 @@ class RDF::Lamprey < Sinatra::Base
RDF::LDP::Resource.find(RDF::URI(request.url), settings.repository)
end

##
# @example Configuring Lamprey server
# Lamprey::Config
# .register_repository!(:my_repo, RDF::Repository)
#
# Lamprey::Config.configure!(repository: :my_repo)
class Config
##
# @see #new
# @see #configure!
def self.configure!(**options)
self.new(**options).configure!
end

##
# Registers a repository for use with the {#build_repository} method.
#
# @example Registering a custom repository
# MyRepository = Class.new(RDF::Repository)
#
# Lamprey::Config.register_repository!(:my_repo, MyRepository)
#
# @param name [Symbol]
# @param klass [Class]
# @return [void]
def self.register_repository!(name, klass)
@@repositories[name] = klass
end

##
# @!attribute [rw] options
attr_accessor :options

@@repositories = { default: RDF::Repository }

##
# @param repository [RDF::Repository]
def initialize(repository: :default)
@options = {}
@options[:repository] = repository
end

##
# Builds the repository as given in the configuration.
#
# @return [RDF::Repository] a repository instance
def build_repository
@@repositories.fetch(options[:repository]) {
warn "#{options[:repository]} is not a configured repository. Use "\
"`Lamprey::Config.register_repository!` to register it before "\
"configuration. Falling back on the default: " \
"#{@@repositories[:default]}."
@@repositories[:default]
}.new
end

##
# Configures {RDF::Lamprey} with {#options}.
#
# @return [void]
def configure!
repository = build_repository
warn "#{repository} is not a persistent repository. "\
"Data will be lost on server shutdown." unless repository.persistent?

RDF::Lamprey.configure { |config| config.set :repository, repository }
end
end

# Set defaults in case user does not configure values
Config.configure!

run! if app_file == $0
end

13 changes: 13 additions & 0 deletions bin/lamprey
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
#!/usr/bin/env ruby
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'app')))
require 'lamprey'
require 'optparse'

options = {}

OptionParser.new do |opts|
opts.banner = 'Usage: lamprey [options]'

opts.on("-rREPO", "--repository=REPO", 'Set the repository') do |repo|
options[:repository] = repo.to_sym
end
end.parse!

RDF::Lamprey::Config.configure!(**options)

RDF::Lamprey.run!
Loading

0 comments on commit 9baacfd

Please sign in to comment.