Class: Camping::Server

Inherits:
Rack::Server
  • Object
show all
Defined in:
lib/camping/server.rb

Defined Under Namespace

Classes: Options, XSendfile

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/camping/server.rb', line 98

def initialize(*)
  super
  @reloader = Camping::Reloader.new(options[:script]) do |app|
    if !app.options.has_key?(:dynamic_templates)
    app.options[:dynamic_templates] = true
   end
   
    if !Camping::Models.autoload?(:Base) && options[:database]
      Camping::Models::Base.establish_connection(
        :adapter => 'sqlite3',
        :database => options[:database]
      )
    end
  end
end

Instance Method Details

#appObject



151
152
153
# File 'lib/camping/server.rb', line 151

def app
  Rack::Cascade.new([Rack::File.new(public_dir), self], [405, 404, 403])
end

#call(env) ⇒ Object



164
165
166
167
# File 'lib/camping/server.rb', line 164

def call(env)
  app = current_app || raise("Could not find an app called `#{@reloader.name}`")
  app.call(env)
end

#current_appObject



155
156
157
158
159
160
161
162
# File 'lib/camping/server.rb', line 155

def current_app
  @reloader.reload
  apps = @reloader.apps
  return apps.values.first if apps.size == 1
  if key = apps.keys.grep(/^#{@reloader.name}$/i)[0]
    apps[key]
  end
end

#default_optionsObject



118
119
120
121
122
123
# File 'lib/camping/server.rb', line 118

def default_options
  super.merge({
    :Port => 3301,
    :database => Options::DB
  })
end

#middlewareObject



125
126
127
128
129
# File 'lib/camping/server.rb', line 125

def middleware
  h = super
  h["development"] << [XSendfile]
  h
end

#opt_parserObject



114
115
116
# File 'lib/camping/server.rb', line 114

def opt_parser
  Options.new
end

#public_dirObject



147
148
149
# File 'lib/camping/server.rb', line 147

def public_dir
  File.expand_path('../public', @reloader.file)
end

#startObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/camping/server.rb', line 131

def start
  if options[:server] == "console"
    puts "** Starting console"
    @reloader.reload!
    r = @reloader
    eval("self", TOPLEVEL_BINDING).meta_def(:reload!) { r.reload!; nil }
    ARGV.clear
    IRB.start
    exit
  else
    name = server.name[/\w+$/]
    puts "** Starting #{name} on #{options[:Host]}:#{options[:Port]}"
    super
  end
end