class Camping::Server

Constants

T

Ensure trailing slash lambda

Public Class Methods

new(*) click to toggle source
Calls superclass method
# File lib/camping/server.rb, line 130
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

Public Instance Methods

app() click to toggle source

add the public directory as a Rack app serving files first, then the current value of self, which is our camping apps, as an app.

# File lib/camping/server.rb, line 197
def app
  Rack::Cascade.new([Rack::Files.new(public_dir), self], [405, 404, 403])
end
call(env) click to toggle source

call(env) res

How routing works

The first app added using Camping.goes is set at the root, we walk through the defined routes of the first app to see if there is a match. With no match we then walk through every other defined app. When we reach a matching route we call that app and Camping’s router handles the rest.

Mounting apps at different directories is now explicit by setting the url_prefix option:

camping.goes :Nuts          # Mounts Nuts at /
module Auth
   set :url_prefix, "auth/"
end
camping.goes :Auth          # Mounts Auth at /auth/
camping.goes :Blog          # Mounts Blog at /

Note that routes that you set explicitly with R are not prefixed. This us explicit control over routes:

module Auth::Controllers
   class Whatever < R '/thing/' # Mounts at /thing/
      def get
         render :some_view
      end
   end
end
# File lib/camping/server.rb, line 250
def call(env)
  @reloader.reload
  apps = @reloader.apps

  # our switch statement iterates through possible app outcomes, no apps
  # loaded, one app loaded, or multiple apps loaded.
  case apps.length
  when 0
    [200, {'Content-Type' => 'text/html'}, ["I'm sorry but no apps were found."]]
  when 1
    apps.values.first.call(env) # When we have one
  else
    # 2 and up get special treatment
    apps.each do |name, app|
      app.routes.each do |r|
        if (path_matches?(env['PATH_INFO'], r))
          return app.call(env)
          next
        end
      end
    end

    # Just return the first app if we didn't find a match.
    return apps.values.first.call(env)
  end
end
default_options() click to toggle source
Calls superclass method
# File lib/camping/server.rb, line 150
def default_options
  super.merge({
    :Port => 3301,
    :database => Options::DB
  })
end
middleware() click to toggle source
Calls superclass method
# File lib/camping/server.rb, line 157
def middleware
  h = super
  h["development"] << [XSendfile]
  h
end
opt_parser() click to toggle source
# File lib/camping/server.rb, line 146
def opt_parser
  Options.new
end
path_matches?(path, *reg) click to toggle source

path_matches? accepts a regular expression string in our case our apps and controllers

# File lib/camping/server.rb, line 204
def path_matches?(path, *reg)
  p = T.(path)
  reg.each do |r|
    return true if Regexp.new(T.(r)).match?(p) && p == T.(r)
  end
  false
end
public_dir() click to toggle source

defines the public directory to be /public

# File lib/camping/server.rb, line 191
def public_dir
  File.expand_path('../public', @reloader.file)
end
start() click to toggle source
Calls superclass method
# File lib/camping/server.rb, line 163
def start

  # If routes option was chosen to short circut here
  if options[:routes] == true
    @reloader.reload!
    r = @reloader
    eval("self", TOPLEVEL_BINDING).meta_def(:reload!) { r.reload!; nil }
    ARGV.clear
    Camping::Commands.routes
    exit
  end

  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