camping 3.2.5 → 3.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 915bc9e71d2023113e730ce571ac0b6327946cc9d230eb4f3f4c5803b71551ec
4
- data.tar.gz: 0b7246146b74d7937ae77115c093605cd59a2e505465968effeacfe95ab3a666
3
+ metadata.gz: c98ea95a52b19b1bafad3d1c1d4a74dfa0eefe03af35aab54e646b4f772cc659
4
+ data.tar.gz: e56f1ba3b83d5881335546e8f8d3285337c327b992c5e451b9e4484854f9abdf
5
5
  SHA512:
6
- metadata.gz: af96a7f36ce4893de7af94c448cd96013fe7a85812abc2e55a98932129a4575cd3513dbc7894f49f3a312ba2c248a41180684897034a976577db18c840181ab6
7
- data.tar.gz: cb1d95c481f217eb848934fe022a5409a21b9bb31e4f7d6eb3874ba9774797a32470fd9c4179e006879948f76c6a682063ed962294feb170753b3f77beb6d0b3
6
+ metadata.gz: 1f99eb2260eaa7df8c87dc4204f7e5818e9f3fe21a0fc871b0b7fe3b8780dae1492c654ee0c80c102abbf5d2087d2b22891d94c1c7b0c698c52cbdba91fc2791
7
+ data.tar.gz: 38c338c173435474a95cb804194c4f0a09c75acaaf971abdefb1f3fd78fc9bf8d4cdf8216275a47c9f4c204f0411be48dbb514097d227b7d294d097d5a6546a6
data/Rakefile CHANGED
@@ -17,13 +17,14 @@ else
17
17
  end
18
18
  end
19
19
 
20
+ require "bundler/gem_tasks"
20
21
  require 'rake'
21
22
  require 'rake/clean'
22
23
  require 'rake/testtask'
23
24
  require 'tempfile'
24
25
  require 'open3'
25
26
 
26
- require File.expand_path('../constants', __FILE__)
27
+ # require File.expand_path('../constants', __FILE__)
27
28
 
28
29
  CLEAN.include ['**/.*.sw?', '*.gem', '.config', 'test/test.log', '.*.pt']
29
30
 
@@ -75,21 +76,38 @@ desc "Packages Camping."
75
76
  task :package => :clean
76
77
 
77
78
  ## Tests
78
- Rake::TestTask.new(:test) do |t|
79
- t.libs << "test"
80
- t.test_files = FileList['test/app_*.rb', 'test/gear/gear_*.rb']
81
- end
79
+ namespace :test do
82
80
 
83
- ## Reloader Tests
84
- Rake::TestTask.new(:reloader) do |t|
85
- t.libs << "test"
86
- t.test_files = FileList['test/reload_*.rb']
87
- end
81
+ Rake::TestTask.new(:camping) do |t|
82
+ t.libs << "test"
83
+ t.test_files = FileList['test/app_*.rb']
84
+ end
85
+
86
+ Rake::TestTask.new(:gear) do |t|
87
+ t.libs << "test"
88
+ t.test_files = FileList['test/gear/gear_*.rb']
89
+ end
90
+
91
+ ## Reloader Tests
92
+ Rake::TestTask.new(:reloader) do |t|
93
+ t.libs << "test"
94
+ t.test_files = FileList['test/reload_*.rb']
95
+ end
96
+
97
+ ## Config Reloader Tests
98
+ Rake::TestTask.new(:configreloader) do |t|
99
+ t.libs << "test"
100
+ t.test_files = FileList['test/config_*.rb']
101
+ end
102
+
103
+ desc "Run Camping::Server tests"
104
+ Rake::TestTask.new("server") do |t|
105
+ t.libs << 'test/server'
106
+ t.test_files = FileList["test/server/**/spec_*.rb"]
107
+ t.warning = false
108
+ t.verbose = false
109
+ end
88
110
 
89
- ## Config Reloader Tests
90
- Rake::TestTask.new(:configreloader) do |t|
91
- t.libs << "test"
92
- t.test_files = FileList['test/config_*.rb']
93
111
  end
94
112
 
95
113
  ## Diff
@@ -119,7 +137,7 @@ end
119
137
  error = false
120
138
 
121
139
  ## Check
122
- task :check => ["test", "reloader", "configreloader", "check:valid", "check:equal", "check:size", "check:lines", "check:exit"]
140
+ task :check => ["test:camping", "test:gear", "test:reloader", "test:configreloader", "test:server", "check:valid", "check:equal", "check:size", "check:lines", "check:exit"]
123
141
  namespace :check do
124
142
 
125
143
  desc "Check source code validity"
@@ -258,7 +258,7 @@ KDL
258
258
  source 'https://rubygems.org'
259
259
 
260
260
  gem 'camping'
261
- gem 'puma'
261
+ gem 'falcon'
262
262
  gem 'rake'
263
263
 
264
264
  group :production do
@@ -189,6 +189,7 @@ module Camping
189
189
  def setup_zeit(loader)
190
190
  loader.push_dir("#{@root}/apps") if can_add_directory "#{@root}/apps"
191
191
  loader.push_dir("#{@root}/lib") if can_add_directory "#{@root}/lib"
192
+ loader.ignore("#{@root}/lib/camping-unabridged.rb")
192
193
  loader.enable_reloading if ENV['environment'] == 'development'
193
194
  loader.setup
194
195
  end
@@ -196,7 +197,7 @@ module Camping
196
197
  # verifies that we can add a directory to the loader.
197
198
  # used for testing to prevent multiple loaders from watching the same directory.
198
199
  def can_add_directory(directory)
199
- if Dir.exist?("#{@root}/apps")
200
+ if Dir.exist?(directory)
200
201
  Loaders.each do |loader|
201
202
  return false if loader.dirs.include? directory
202
203
  end
@@ -219,6 +220,7 @@ module Camping
219
220
  def reload_directory(directory)
220
221
  files, folders = folders_and_files_in(directory)
221
222
  files.each {|file|
223
+ next if file.include? "unabridged"
222
224
  @requires << [file, File.expand_path(file)]
223
225
  load file
224
226
  }
data/lib/camping/loads.rb CHANGED
@@ -9,8 +9,8 @@ require 'bundler/setup'
9
9
  # internal dependencies
10
10
  require 'camping/tools'
11
11
  require 'camping/campguide'
12
- require 'camping/gear/filters'
13
- require 'camping/gear/nancy'
14
- require 'camping/gear/inspection'
15
- require 'camping/gear/kuddly'
16
- require 'camping/gear/firewatch'
12
+ require 'gear/filters'
13
+ require 'gear/nancy'
14
+ require 'gear/inspection'
15
+ require 'gear/kuddly'
16
+ require 'gear/firewatch'
@@ -0,0 +1,31 @@
1
+ class MissingLibrary < Exception #:nodoc: all
2
+ end
3
+ begin
4
+ require 'sequel'
5
+ rescue LoadError => e
6
+ raise MissingLibrary, "Sequel gem could not be loaded (is it installed?): #{e.message}"
7
+ end
8
+
9
+ $SEQUEL_EXTRAS = %{
10
+ Sequel::Model.plugin :timestamps
11
+ Base = Sequel::Model
12
+
13
+ # class SchemaInfo < Base
14
+ # end
15
+ }
16
+
17
+ module Camping
18
+ module Models
19
+ module_eval $SEQUEL_EXTRAS
20
+ end
21
+ end
22
+
23
+ Camping::S.sub!(/autoload\s*:Base\s*,\s*['"]camping\/sequel['"]/, $SEQUEL_EXTRAS)
24
+ Camping::Apps.each do |c|
25
+ c::Models.module_eval $SEQUEL_EXTRAS.gsub('Camping', c.to_s)
26
+ end
27
+
28
+ ENV['DATABASE_URL'] = 'db/camp.db' if ENV['DATABASE_URL'] == ''
29
+
30
+ # connect to a database by default
31
+ DB = Sequel.connect(ENV['DATABASE_URL'])
@@ -3,7 +3,7 @@ require 'erb'
3
3
  require 'rack'
4
4
  require 'rackup'
5
5
  require 'camping/version'
6
- require 'camping/gear/firewatch'
6
+ require 'gear/firewatch'
7
7
  require 'camping/loader'
8
8
  require 'camping/commands'
9
9
 
@@ -142,6 +142,10 @@ module Camping
142
142
  exit
143
143
  end
144
144
 
145
+ Dir['kindling/*.rb'].each do |kindling|
146
+ require_relative File.expand_path(kindling)
147
+ end
148
+
145
149
  @reloader.reload!
146
150
  r = @reloader
147
151
 
@@ -1,5 +1,5 @@
1
1
  module Camping
2
- VERSION = "3.2.5"
2
+ VERSION = "3.2.6"
3
3
  def self.version
4
4
  VERSION
5
5
  end
@@ -1014,6 +1014,7 @@ module Camping
1014
1014
  #
1015
1015
  # Models cannot be referred from Views at this time.
1016
1016
  module Models
1017
+ autoload :Base, 'camping/sequel'
1017
1018
  Helpers.include(X, self)
1018
1019
  end
1019
1020
 
data/lib/camping.rb CHANGED
@@ -81,6 +81,7 @@ IO.read(a.set:__FILE__,$`)=~/^__END__/&&(b=$'.split(/^@@\s*(.+?)\s*\r?\n/m)
81
81
  a.set :_meta, H[file: fl, line_number: ln, parent: self,
82
82
  root: (name != "Cam\ping" ? '/' + CampTools.to_snake(name) : '/')];C.configure(a)end end
83
83
  module Views;include X,Helpers end;module Models
84
+ autoload :Base, 'camping/sequel'
84
85
  Helpers.include(X,self) end;autoload:Mab,'camping/mab'
85
86
  autoload:Template,'camping/template';pack Gear::Inspection;pack Gear::Filters
86
87
  pack Gear::Nancy;pack Gear::Kuddly;pack Gear::Firewatch;C end
data/test/app_loader.rb CHANGED
@@ -23,7 +23,7 @@ module TestCaseLoaderToo
23
23
  end
24
24
 
25
25
  def after_all
26
- leave_loader
26
+ leave_dir
27
27
  super
28
28
  end
29
29
  end
@@ -3,7 +3,7 @@ source 'https://rubygems.org'
3
3
 
4
4
  gem 'bundler'
5
5
  gem 'rake'
6
- gem 'puma'
6
+ gem 'falcon'
7
7
  gem "camping", :path => "../../"
8
8
 
9
9
  group :production do
@@ -0,0 +1,7 @@
1
+ module Starty
2
+
3
+ def message = "Hello Friends"
4
+
5
+ def name = "Starty"
6
+
7
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ $:.unshift(File.expand_path('../lib', __dir__))
4
+ # $:.unshift(File.dirname(__FILE__) + '../../lib')
5
+
6
+ require_relative '../../lib/camping'
7
+ require_relative '../../lib/camping/server'
8
+
9
+ require 'minitest/global_expectations/autorun'
10
+ require 'stringio'
11
+
12
+ require 'minitest/reporters'
13
+ Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(:color => true)]
@@ -0,0 +1,103 @@
1
+ require_relative 'helper'
2
+
3
+ require 'tempfile'
4
+ require 'socket'
5
+ require 'webrick'
6
+ require 'open-uri'
7
+ require 'net/http'
8
+ require 'net/https'
9
+
10
+ begin
11
+ require 'stackprof'
12
+ require 'tmpdir'
13
+ rescue LoadError
14
+ else
15
+ test_profile = true
16
+ end
17
+
18
+ describe Camping::Server do
19
+ argv = Camping::Server::ARGV = []
20
+ define_method(:argv) { argv }
21
+
22
+ before {
23
+ argv.clear
24
+ @original_dir = Dir.pwd
25
+ Dir.chdir "test/integration"
26
+ }
27
+
28
+ after {
29
+ Dir.chdir @original_dir
30
+ }
31
+
32
+ # Probably need to revise this for the camping port
33
+ def app
34
+ lambda { |env| [200, { 'content-type' => 'text/plain' }, ['success']] }
35
+ end
36
+
37
+ # learn what this is doing.
38
+ def with_stderr
39
+ old, $stderr = $stderr, StringIO.new
40
+ yield $stderr
41
+ ensure
42
+ $stderr = old
43
+ end
44
+
45
+ # first copied test to make sure this works right.
46
+ it "overrides :config if :app is passed in" do
47
+ server = Rackup::Server.new(app: "FOO")
48
+ server.app.must_equal "FOO"
49
+ end
50
+
51
+ # Fails a lot
52
+ # it "runs a server" do
53
+ # pidfile = Tempfile.open('pidfile') { |f| break f }
54
+ # FileUtils.rm pidfile.path
55
+ # server = Camping::Server.new(
56
+ # app: "FOO",
57
+ # environment: 'none',
58
+ # pid: pidfile.path,
59
+ # Port: TCPServer.open('localhost', '0'){|s| s.addr[1] },
60
+ # Host: 'localhost',
61
+ # Logger: WEBrick::Log.new(nil, WEBrick::BasicLog::WARN),
62
+ # AccessLog: [],
63
+ # daemonize: false,
64
+ # server: 'webrick'
65
+ # )
66
+ # t = Thread.new { server.start { |s| Thread.current[:server] = s } }
67
+ # t.join(0.01) until t[:server] && t[:server].status != :Stop
68
+ # body = if URI.respond_to?(:open)
69
+ # URI.open("http://localhost:#{server.options[:Port]}/") { |f| f.read }
70
+ # else
71
+ # open("http://localhost:#{server.options[:Port]}/") { |f| f.read }
72
+ # end
73
+ # body.must_include 'Let&#39;s go Camping'
74
+ # Process.kill(:INT, $$)
75
+ # t.join
76
+ # open(pidfile.path) { |f| f.read.must_equal $$.to_s }
77
+ # end
78
+
79
+ it "Loads the kindling initializers" do
80
+ pidfile = Tempfile.open('pidfile') { |f| break f }
81
+ FileUtils.rm pidfile.path
82
+ server = Camping::Server.new
83
+
84
+ starty_file = false
85
+ starty_name = "Starty"
86
+
87
+ t = Thread.new {
88
+ server.start { |s|
89
+ Thread.current[:server] = s
90
+ starty_file = Object.constants.include? :Starty
91
+ starty_name = Starty.name
92
+ }
93
+ }
94
+ t.join(0.01) until t[:server] && t[:server].status != :Stop
95
+
96
+ starty_file.must_equal true
97
+ starty_name.must_equal "Starty"
98
+
99
+ Process.kill(:INT, $$)
100
+ t.join
101
+ end
102
+
103
+ end
data/test/test_helper.rb CHANGED
@@ -46,7 +46,7 @@ module CommandLineCommands
46
46
 
47
47
  # deletes the temporary directories found in the /apps directory for reloader testing.
48
48
  def leave_reloader
49
- Dir.chdir @original_dir
49
+ leave_dir
50
50
  `rm -rf test/apps/reloader/apps` if File.exist?('test/apps/reloader/apps')
51
51
  `rm -rf test/apps/reloader/lib` if File.exist?('test/apps/reloader/lib')
52
52
  end
@@ -61,11 +61,15 @@ module CommandLineCommands
61
61
  Dir.mkdir("lib") unless Dir.exist?("lib")
62
62
  end
63
63
 
64
- # deletes the temporary directories found in the /apps directory for reloader testing.
65
- def leave_loader
66
- Dir.chdir @original_dir
64
+ # generic move_to(dir) method
65
+ def move_to(dir)
66
+ @original_dir = Dir.pwd
67
+ Dir.chdir dir
67
68
  end
68
69
 
70
+ # generic leave_dir method
71
+ def leave_dir = Dir.chdir @original_dir
72
+
69
73
  def write(file, content)
70
74
  raise "cannot write nil" unless file
71
75
  file = tmp_file(file)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: camping
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.5
4
+ version: 3.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - why the lucky stiff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-14 00:00:00.000000000 Z
11
+ date: 2024-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -186,6 +186,62 @@ dependencies:
186
186
  - - "~>"
187
187
  - !ruby/object:Gem::Version
188
188
  version: 1.0.4
189
+ - !ruby/object:Gem::Dependency
190
+ name: bundler
191
+ requirement: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - ">="
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
196
+ type: :development
197
+ prerelease: false
198
+ version_requirements: !ruby/object:Gem::Requirement
199
+ requirements:
200
+ - - ">="
201
+ - !ruby/object:Gem::Version
202
+ version: '0'
203
+ - !ruby/object:Gem::Dependency
204
+ name: minitest
205
+ requirement: !ruby/object:Gem::Requirement
206
+ requirements:
207
+ - - "~>"
208
+ - !ruby/object:Gem::Version
209
+ version: '5.0'
210
+ type: :development
211
+ prerelease: false
212
+ version_requirements: !ruby/object:Gem::Requirement
213
+ requirements:
214
+ - - "~>"
215
+ - !ruby/object:Gem::Version
216
+ version: '5.0'
217
+ - !ruby/object:Gem::Dependency
218
+ name: minitest-global_expectations
219
+ requirement: !ruby/object:Gem::Requirement
220
+ requirements:
221
+ - - ">="
222
+ - !ruby/object:Gem::Version
223
+ version: '0'
224
+ type: :development
225
+ prerelease: false
226
+ version_requirements: !ruby/object:Gem::Requirement
227
+ requirements:
228
+ - - ">="
229
+ - !ruby/object:Gem::Version
230
+ version: '0'
231
+ - !ruby/object:Gem::Dependency
232
+ name: minitest-sprint
233
+ requirement: !ruby/object:Gem::Requirement
234
+ requirements:
235
+ - - ">="
236
+ - !ruby/object:Gem::Version
237
+ version: '0'
238
+ type: :development
239
+ prerelease: false
240
+ version_requirements: !ruby/object:Gem::Requirement
241
+ requirements:
242
+ - - ">="
243
+ - !ruby/object:Gem::Version
244
+ version: '0'
189
245
  description:
190
246
  email: why@ruby-lang.org
191
247
  executables:
@@ -237,20 +293,20 @@ files:
237
293
  - lib/camping/ar.rb
238
294
  - lib/camping/campguide.rb
239
295
  - lib/camping/commands.rb
240
- - lib/camping/gear/filters.rb
241
- - lib/camping/gear/firewatch.rb
242
- - lib/camping/gear/inspection.rb
243
- - lib/camping/gear/kuddly.rb
244
- - lib/camping/gear/nancy.rb
245
296
  - lib/camping/loader.rb
246
297
  - lib/camping/loads.rb
247
298
  - lib/camping/mab.rb
299
+ - lib/camping/sequel.rb
248
300
  - lib/camping/server.rb
249
301
  - lib/camping/session.rb
250
302
  - lib/camping/template.rb
251
303
  - lib/camping/tools.rb
252
304
  - lib/camping/version.rb
253
- - lib/campingtrip.md
305
+ - lib/gear/filters.rb
306
+ - lib/gear/firewatch.rb
307
+ - lib/gear/inspection.rb
308
+ - lib/gear/kuddly.rb
309
+ - lib/gear/nancy.rb
254
310
  - test/app_camping_gear.rb
255
311
  - test/app_camping_tools.rb
256
312
  - test/app_config.rb
@@ -291,8 +347,11 @@ files:
291
347
  - test/integration/Rakefile
292
348
  - test/integration/camp.rb
293
349
  - test/integration/config.kdl
350
+ - test/integration/kindling/starty.rb
294
351
  - test/integration/test/test_helper.rb
295
352
  - test/reload_reloader.rb
353
+ - test/server/helper.rb
354
+ - test/server/spec_server.rb
296
355
  - test/test_helper.rb
297
356
  homepage: http://rubycamping.org/
298
357
  licenses:
@@ -321,7 +380,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
321
380
  - !ruby/object:Gem::Version
322
381
  version: '0'
323
382
  requirements: []
324
- rubygems_version: 3.3.9
383
+ rubygems_version: 3.5.11
325
384
  signing_key:
326
385
  specification_version: 4
327
386
  summary: miniature rails for anyone
data/lib/campingtrip.md DELETED
@@ -1,341 +0,0 @@
1
- # How does Camping work?
2
- This is an academic document written to help people, but mostly me, understand what Camping is doing and in what sequence. Why? Because I want to make camping better, but how do you make it better unless you understand what you've got?
3
-
4
- # Start
5
- Camping starts off with some simple code you type: `camping nuts.rb` into your terminal and the camping gem is loaded and executed. This assumes that you've installed camping via ruby gems: `gem install camping`. Gems are then given a binary command you can use on the command line, in our case **camping**. The camping command accepts a file as an argument and maybe some options. This command calls a *binary* that's found the camping gem, this is what it looks like:
6
- ```ruby
7
- #!/usr/bin/env ruby
8
-
9
- $:.unshift File.dirname(__FILE__) + "/../lib"
10
-
11
- require 'camping'
12
- require 'camping/server'
13
-
14
- begin
15
- Camping::Server.start
16
- rescue OptionParser::ParseError => ex
17
- STDERR.puts "!! #{ex.message}"
18
- puts "** use `#{File.basename($0)} --help` for more details..."
19
- exit 1
20
- end
21
- ```
22
-
23
- First wee see `$:.unshift File.dirname(__FILE__) + "/../lib"`, `$:`, is a global variable, that contains the loadpath for scripts. Every ruby file is a script, you may be more familiar with `$LOAD_PATH` which is an alias for the `$:` global. Next `unshift` is an array method that prepends an item to the beginning of an array. `File` is a builtin ruby class that lets you work with files. `File.dirname(file)` returns a string of the complete file path of the file given, except for the file's name. In our case we're using `__FILE__` to return the current file name, which is the binary file in the camping gem. the last portion: `+ "/../lib"` appends the string to the directory path that we just got. All of this is done to ensure that the `lib` folder where all of camping's code resides is added to the script load path.
24
-
25
- Next we require camping:
26
- ```ruby
27
- require 'camping'
28
- require 'camping/server'
29
- ```
30
-
31
- Which loads camping and it's server code in to the current script context.
32
-
33
- ```ruby
34
- Camping::Server.start
35
- ```
36
-
37
- The above code finally Starts the camping server. So let's take a look at the server:
38
-
39
- ```ruby
40
- require 'irb'
41
- require 'erb'
42
- require 'rack'
43
- require 'camping/reloader'
44
- require 'camping/commands'
45
-
46
- # == The Camping Server (for development)
47
- #
48
- # Camping includes a pretty nifty server which is built for development.
49
- # It follows these rules:
50
- #
51
- # * Load all Camping apps in a file.
52
- # * Mount those apps according to their name. (e.g. Blog is mounted at /blog.)
53
- # * Run each app's <tt>create</tt> method upon startup.
54
- # * Reload the app if its modification time changes.
55
- # * Reload the app if it requires any files under the same directory and one
56
- # of their modification times changes.
57
- # * Support the X-Sendfile header.
58
- #
59
- # Run it like this:
60
- #
61
- # camping blog.rb # Mounts Blog at /
62
- #
63
- # And visit http://localhost:3301/ in your browser.
64
- module Camping
65
- class Server < Rack::Server
66
- class Options
67
- if home = ENV['HOME'] # POSIX
68
- DB = File.join(home, '.camping.db')
69
- RC = File.join(home, '.campingrc')
70
- elsif home = ENV['APPDATA'] # MSWIN
71
- DB = File.join(home, 'Camping.db')
72
- RC = File.join(home, 'Campingrc')
73
- else
74
- DB = nil
75
- RC = nil
76
- end
77
-
78
- HOME = File.expand_path(home) + '/'
79
-
80
- def parse!(args)
81
- args = args.dup
82
-
83
- options = {}
84
-
85
- opt_parser = OptionParser.new("", 24, ' ') do |opts|
86
- opts.banner = "Usage: camping my-camping-app.rb"
87
- opts.define_head "#{File.basename($0)}, the microframework ON-button for ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
88
- opts.separator ""
89
- opts.separator "Specific options:"
90
-
91
- opts.on("-h", "--host HOSTNAME",
92
- "Host for web server to bind to (default is all IPs)") { |v| options[:Host] = v }
93
-
94
- opts.on("-p", "--port NUM",
95
- "Port for web server (defaults to 3301)") { |v| options[:Port] = v }
96
-
97
- db = DB.sub(HOME, '~/') if DB
98
- opts.on("-d", "--database FILE",
99
- "SQLite3 database path (defaults to #{db ? db : '<none>'})") { |db_path| options[:database] = db_path }
100
-
101
- opts.on("-C", "--console",
102
- "Run in console mode with IRB") { options[:server] = "console" }
103
-
104
- server_list = ["thin", "webrick", "console"]
105
- opts.on("-s", "--server NAME",
106
- "Server to force (#{server_list.join(', ')})") { |v| options[:server] = v }
107
-
108
- opts.separator ""
109
- opts.separator "Common options:"
110
-
111
- # No argument, shows at tail. This will print an options summary.
112
- # Try it and see!
113
- opts.on("-?", "--help", "Show this message") do
114
- puts opts
115
- exit
116
- end
117
-
118
- # Another typical switch to print the version.
119
- opts.on("-m", "--mounting", "Shows Mounting Guide") do
120
- puts "Mounting Guide"
121
- puts ""
122
- puts "To mount your horse, hop up on the side and put it."
123
- exit
124
- end
125
-
126
- # Another typical switch to print the version.
127
- opts.on("-v", "--version", "Show version") do
128
- puts Gem.loaded_specs['camping'].version
129
- exit
130
- end
131
-
132
- end
133
-
134
- opt_parser.parse!(args)
135
-
136
- # If no Arguments were called.
137
- if args.empty?
138
- args << "cabin.rb" # adds cabin.rb as a default camping entrance file
139
- end
140
-
141
- # Parses the first argument as the script to load into the server.
142
- options[:script] = args.shift
143
- options
144
- end
145
- end
146
-
147
- def initialize(*)
148
- super
149
- @reloader = Camping::Reloader.new(options[:script]) do |app|
150
- if !app.options.has_key?(:dynamic_templates)
151
- app.options[:dynamic_templates] = true
152
- end
153
-
154
- if !Camping::Models.autoload?(:Base) && options[:database]
155
- Camping::Models::Base.establish_connection(
156
- :adapter => 'sqlite3',
157
- :database => options[:database]
158
- )
159
- end
160
- end
161
- end
162
-
163
- def opt_parser
164
- Options.new
165
- end
166
-
167
- def default_options
168
- super.merge({
169
- :Port => 3301,
170
- :database => Options::DB
171
- })
172
- end
173
-
174
- def middleware
175
- h = super
176
- h["development"] << [XSendfile]
177
- h
178
- end
179
-
180
- def start
181
- if options[:server] == "console"
182
- puts "** Starting console"
183
- @reloader.reload!
184
- r = @reloader
185
- eval("self", TOPLEVEL_BINDING).meta_def(:reload!) { r.reload!; nil }
186
- ARGV.clear
187
- IRB.start
188
- exit
189
- else
190
- name = server.name[/\w+$/]
191
- puts "** Starting #{name} on #{options[:Host]}:#{options[:Port]}"
192
- super
193
- end
194
- end
195
-
196
- # defines the public directory to be /public
197
- def public_dir
198
- File.expand_path('../public', @reloader.file)
199
- end
200
-
201
- # add the public directory as a Rack app serving files first, then the
202
- # current value of self, which is our camping apps, as an app.
203
- def app
204
- Rack::Cascade.new([Rack::Files.new(public_dir), self], [405, 404, 403])
205
- end
206
-
207
- # path_matches?
208
- # accepts a regular expression string
209
- # in our case our apps and controllers
210
- def path_matches?(path, *reg)
211
- reg.each do |r|
212
- return true if Regexp.new(r).match? path
213
- end
214
- false
215
- end
216
-
217
- # call(env) res
218
- # == How routing works
219
- #
220
- # The first app added using Camping.goes is set at the root, we walk through
221
- # the defined routes of the first app to see if there is a match.
222
- # With no match we then walk through every other defined app.
223
- # Each subsequent app defined is loaded at a directory named after them:
224
- #
225
- # camping.goes :Nuts # Mounts Nuts at /
226
- # camping.goes :Auth # Mounts Auth at /auth/
227
- # camping.goes :Blog # Mounts Blog at /blog/
228
- #
229
- def call(env)
230
- @reloader.reload
231
- apps = @reloader.apps
232
-
233
- # our switch statement iterates through possible app outcomes, no apps
234
- # loaded, one app loaded, or multiple apps loaded.
235
- case apps.length
236
- when 0
237
- [200, {'Content-Type' => 'text/html'}, ["I'm sorry but no apps were found."]]
238
- when 1
239
- apps.values.first.call(env) # When we have one
240
- else
241
- # 2 and up get special treatment
242
- count = 0
243
- apps.each do |name, app|
244
- if count == 0
245
- app.routes.each do |r|
246
- if (path_matches?(env['PATH_INFO'], r))
247
- next
248
- end
249
- return app.call(env) unless !(path_matches?(env['PATH_INFO'], r))
250
- end
251
- else
252
- mount = name.to_s.downcase
253
- case env["PATH_INFO"]
254
- when %r{^/#{mount}}
255
- env["SCRIPT_NAME"] = env["SCRIPT_NAME"] + $&
256
- env["PATH_INFO"] = $'
257
- return app.call(env)
258
- when %r{^/code/#{mount}}
259
- return [200, {'Content-Type' => 'text/plain', 'X-Sendfile' => @reloader.file}, []]
260
- end
261
- end
262
- count += 1
263
- end
264
-
265
- # Just return the first app if we didn't find a match.
266
- return apps.values.first.call(env)
267
- end
268
- end
269
-
270
- class XSendfile
271
- def initialize(app)
272
- @app = app
273
- end
274
-
275
- def call(env)
276
- status, headers, body = @app.call(env)
277
-
278
- if key = headers.keys.grep(/c-sendfile/i).first
279
- filename = headers[key]
280
- content = open(filename,'rb') { | io | io.read}
281
- headers['content-length'] = size(content).to_s
282
- body = [content]
283
- end
284
-
285
- return status, headers, body
286
- end
287
-
288
- if "".respond_to?(:bytesize)
289
- def size(str)
290
- str.bytesize
291
- end
292
- else
293
- def size(str)
294
- str.size
295
- end
296
- end
297
- end
298
- end
299
- end
300
- ```
301
-
302
- The beginning of Server loads the required code to get Camping started, and then opens the `Camping` module:
303
-
304
- ```ruby
305
- module Camping
306
- class Server < Rack::Server
307
- end
308
- end
309
- ```
310
-
311
- `Server` inherits from `Rack::Server`. Camping is Rack based to give ourselves a predictable interface for our web server code. Consequentally a lot of utilities useful for webservers are just baked into Rack, It gives Camping the chance to do what it does best, magic!
312
-
313
- The first class declared in `Server` is called `Options`. It's in charge of parsing the command line options supplied to camping, and then supplying those options as a hash for the program further down the line. This class is declared inside of the `Server` class so that we can encapsulate the behaviour of options within the server. Which is pretty nice. Next is initialize:
314
-
315
- ```ruby
316
- def initialize(*)
317
- super
318
- @reloader = Camping::Reloader.new(options[:script]) do |app|
319
- if !app.options.has_key?(:dynamic_templates)
320
- app.options[:dynamic_templates] = true
321
- end
322
-
323
- if !Camping::Models.autoload?(:Base) && options[:database]
324
- Camping::Models::Base.establish_connection(
325
- :adapter => 'sqlite3',
326
- :database => options[:database]
327
- )
328
- end
329
- end
330
- end
331
- ```
332
-
333
- `initialize` is the method called whenever you instantiate a new object of a class. You'll notice that the line of code in the method is a call to `super`. Because `Camping::Server` is a subclass of `Rack::Server`, and to get things rolling we first call `Rack::Server`'s initialize. Afterwards we setup the reloader, and optionally include the database.
334
-
335
- When you call super naked like that it passes along whatever arguments were sent to the method that called super. In our case It's a splat: `initialize(*)`, so everything is sent along.
336
-
337
-
338
-
339
-
340
-
341
- Remember earlier from the Camping binary where we start the server? : `Camping::Server.start`, You may notice that this is a call to a class method `start`, but we don't declare any class methods in `Camping::Server` only instance methods.
File without changes
File without changes
File without changes
File without changes
File without changes