Class: Camping::Server
- Defined in:
- lib/camping/server.rb
Defined Under Namespace
Instance Method Summary collapse
-
#app ⇒ Object
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.
-
#call(env) ⇒ Object
call(env) res == How routing works.
- #default_options ⇒ Object
-
#initialize ⇒ Server
constructor
A new instance of Server.
- #middleware ⇒ Object
- #opt_parser ⇒ Object
-
#path_matches?(path, *reg) ⇒ Boolean
path_matches? accepts a regular expression string in our case our apps and controllers.
-
#public_dir ⇒ Object
defines the public directory to be /public.
- #start ⇒ Object
Constructor Details
#initialize ⇒ Server
Returns a new instance of Server.
87 88 89 90 91 92 93 94 |
# File 'lib/camping/server.rb', line 87 def initialize(*) super @reloader = Camping::Reloader.new([:script]) do |app| if !app..has_key?(:dynamic_templates) app.[:dynamic_templates] = true end end end |
Instance Method Details
#app ⇒ Object
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.
165 166 167 |
# File 'lib/camping/server.rb', line 165 def app Rack::Cascade.new([Rack::Files.new(public_dir), self], [405, 404, 403]) end |
#call(env) ⇒ Object
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
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/camping/server.rb', line 215 def call(env) if ENV['environment'] == 'development' @reloader.reload Camping.make_camp end # our switch statement iterates through possible app outcomes, no apps # loaded, one app loaded, or multiple apps loaded. case @reloader.apps.length when 0 [200, {'content-type' => 'text/html'}, ["I'm sorry but no apps were found."]] when 1 @reloader.apps.values.first.call(env) # When we have one else # 2 and up get special treatment @reloader.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. @reloader.apps.values.first.call(env) end end |
#default_options ⇒ Object
100 101 102 103 104 |
# File 'lib/camping/server.rb', line 100 def super.merge({ :Port => 3301 }) end |
#middleware ⇒ Object
106 107 108 109 110 |
# File 'lib/camping/server.rb', line 106 def middleware h = super h["development"] << [XSendfile] h end |
#opt_parser ⇒ Object
96 97 98 |
# File 'lib/camping/server.rb', line 96 def opt_parser Options.new end |
#path_matches?(path, *reg) ⇒ Boolean
path_matches? accepts a regular expression string in our case our apps and controllers
172 173 174 175 176 177 178 |
# File 'lib/camping/server.rb', line 172 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 ⇒ Object
defines the public directory to be /public
159 160 161 |
# File 'lib/camping/server.rb', line 159 def public_dir File.('../public', @reloader.file) end |
#start ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/camping/server.rb', line 112 def start commands = [] ARGV.each do |cmd| commands << cmd end # Parse commands case commands[0] when "new" Camping::Commands.new_cmd(commands[1]) exit end if [:version] == true puts "Camping v#{Camping::VERSION}" exit end if [:routes] == true @reloader.reload! r = @reloader eval("self", TOPLEVEL_BINDING).(:reload!) { r.reload!; nil } ARGV.clear Camping::Commands.routes exit end if [:server] == "console" puts "** Starting console" @reloader.reload! r = @reloader eval("self", TOPLEVEL_BINDING).(:reload!) { r.reload!; nil } ARGV.clear IRB.start exit else @reloader.reload! r = @reloader Camping.make_camp name = server.name[/\w+$/] puts "** Starting #{name} on #{options[:Host]}:#{options[:Port]}" super end end |