Class: Staticd::App

Inherits:
Object
  • Object
show all
Defined in:
lib/staticd/app.rb

Overview

Staticd App.

This class manage the app initialization and runtime.

Example:

app = Staticd::App.new(config)
app.run

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ App

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



42
43
44
45
46
47
48
49
50
51
52
53
# 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

Instance Method Details

#runObject

Start the application.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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