class Staticd::App
This class manage the app initialization and runtime.
Example:
app = Staticd::App.new(config) app.run
Public Class Methods
new(config)
click to toggle source
Initialize the Staticd
app.
General configuration:
-
environment: the app environment (test, development or production)
-
domain: base to generate per app sub-domain
-
public_port: port used to generate application and endpoint url, (default to 80)
-
database: database url to store resources metadata
-
datastore: datastore url to store resources
-
host: host to listen to
-
port: port to listen to
API
service configuration:
-
api: enable the
API
service -
access_id: HMAC authentication access ID for the
API
service -
secret_key: HMAC authentication secret key for the
API
service
HTTP service configuration:
-
http: enable the HTTP service
-
http_cache: folder where resources are cached
# File lib/staticd/app.rb, line 42 def initialize(config) @config = config @config[:public_port] ||= "80" require_settings(:environment, :domain, :database, :datastore) env = @config[:environment] puts "Starting Staticd in #{env} environment." unless env == "test" display_current_config if env == "development" init_database init_datastore end
Public Instance Methods
run()
click to toggle source
Start the application.
# File lib/staticd/app.rb, line 56 def run require_settings(:host, :port) require_settings(:http_cache) if @config[:http] require_settings(:access_id, :secret_key) if @config[:api] routes = {} routes["/"] = build_http_service if @config[:http] routes["/api/#{Staticd::API::VERSION}"] = build_api_service if @config[:api] router = Rack::URLMap.new(routes) Rack::Server.start( Host: @config[:host], Port: @config[:port], app: router, environment: @config[:environment] ) end
Private Instance Methods
build_api_service()
click to toggle source
# File lib/staticd/app.rb, line 111 def build_api_service api_service = Staticd::API.new(@config) # Bind the API service with the HMAC middleware. raise "No access ID provided" unless @config[:access_id] raise "No secret_key provided" unless @config[:secret_key] Rack::Auth::HMAC.new( api_service, except: Staticd::API::PUBLIC_URI ) do |access_id| @config[:secret_key] if access_id == @config[:access_id].to_s end end
build_http_service()
click to toggle source
# File lib/staticd/app.rb, line 124 def build_http_service http_service = Staticd::HTTPServer.new(@config[:http_cache]) cache_middleware = Staticd::HTTPCache.new(@config[:http_cache], http_service) Rack::RequestTime.new(cache_middleware) end
display_current_config()
click to toggle source
# File lib/staticd/app.rb, line 84 def display_current_config puts "Configuration:" puts "* Database: #{@config[:database]}" puts "* Datastore: #{@config[:datastore]}" if Staticd::Config[:api] puts "* Host: #{@config[:host]}" puts "* Port: #{@config[:port]}" puts "* Domain: #{@config[:domain]}" puts "* Public Port: #{@config[:public_port]}" puts "* Access ID: #{@config[:access_id]}" puts "* Secret Key: #{@config[:secret_key]}" end if Staticd::Config[:http] puts "* HTTP cache: #{@config[:http_cache]}" end end
init_database()
click to toggle source
# File lib/staticd/app.rb, line 103 def init_database Staticd::Database.setup(@config[:environment], @config[:database]) end
init_datastore()
click to toggle source
# File lib/staticd/app.rb, line 107 def init_datastore Staticd::Datastore.setup(@config[:datastore]) end
require_settings(*settings)
click to toggle source
# File lib/staticd/app.rb, line 76 def require_settings(*settings) settings.each do |setting| unless @config.key?(setting) && !@config[setting].nil? raise "Missing '#{setting}' setting" end end end