strobe 0.1.3 → 0.1.4

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.
@@ -1,5 +1,7 @@
1
1
  module Strobe
2
2
  class CLI::Main < CLI
3
+ include CLI::Preview
4
+
3
5
  def help(*args)
4
6
  if args.first == "users"
5
7
  CLI::Users.start( [ "help" ] + args[1..-1] )
@@ -61,6 +63,10 @@ module Strobe
61
63
  end
62
64
  end
63
65
 
66
+ action "preview", "preview your application as it would show up on Strobe Platform" do
67
+ preview_strobe_application
68
+ end
69
+
64
70
  application_path_option
65
71
  method_option "staging", :type => :boolean, :banner => "deploy to a staging environment"
66
72
  method_option "sc-build", :type => :boolean, :banner => "run `sc-build -c` before deploying"
@@ -10,33 +10,21 @@ module Strobe
10
10
  alias wrapped_app app
11
11
  end
12
12
 
13
- class MagicWrapper
14
- def initialize(app)
15
- @app = app
16
- end
17
-
18
- def call(env)
19
- puts "ZOMGZOMG, calling: #{env['PATH_INFO']}"
20
- @app.call(env)
21
- end
22
- end
23
-
24
13
  private
25
14
 
26
- def sproutcore_app
27
- gem "sproutcore", ">= 1.4.0"
28
- require "sproutcore"
29
-
30
- project = SC::Tools.new.requires_project!
31
- SC::Rack::Service.new(*project)
15
+ def preview_strobe_application
16
+ is_sproutcore?? preview_sproutcore_application : preview_html_application
32
17
  end
33
18
 
34
- def wrap(app)
35
- MagicWrapper.new(app)
19
+ def preview_html_application
20
+ app = Rack::Static.new lambda { |e| [] }, :urls => [ '/' ], :root => '.'
21
+ app = Middleware::Rewrite.new(Middleware::Proxy.new(app))
22
+ Server.start :app => app, :host => '0.0.0.0', :Port => 9292
36
23
  end
37
24
 
38
25
  def preview_sproutcore_application
39
- Server.start :app => wrap(sproutcore_app), :Port => 9292
26
+ require 'strobe/sproutcore'
27
+ SC::Tools.start ['server'] + ARGV[1..-1] + [ '--port', '9292' ]
40
28
  end
41
29
  end
42
30
  end
@@ -0,0 +1,117 @@
1
+ require 'thread'
2
+ require 'net/http'
3
+
4
+ module Strobe
5
+ class Middleware::Proxy
6
+
7
+ # A wrapper around the Net/HTTP response body
8
+ # that allows rack to stream the result down
9
+ class Body
10
+ def initialize(queue)
11
+ @queue = queue
12
+ end
13
+
14
+ def each
15
+ while chunk = @queue.pop
16
+ if Exception === chunk
17
+ raise chunk
18
+ else
19
+ yield chunk
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ def initialize(app)
26
+ @app = app
27
+ end
28
+
29
+ def call(env)
30
+ if env['PATH_INFO'] =~ proxy_path
31
+ host, port = $1.split(':')
32
+
33
+ queue = run_request(env, host, ( port || 80 ).to_i, $2 || '/')
34
+
35
+ msg = queue.pop
36
+
37
+ if Exception === msg
38
+ raise msg
39
+ else
40
+ [ msg[0], msg[1], Body.new(queue) ]
41
+ end
42
+ else
43
+ @app.call(env)
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def proxy_path
50
+ %r[/_strobe/proxy/([^/]+)(/.*)?$]
51
+ end
52
+
53
+ KEEP = [ 'CONTENT_LENGTH', 'CONTENT_TYPE' ]
54
+
55
+ def run_request(env, host, port, path)
56
+ queue = Queue.new
57
+
58
+ if env['CONTENT_LENGTH'] || env['HTTP_TRANSFER_ENCODING']
59
+ body = env['rack.input']
60
+ end
61
+
62
+ unless env['QUERY_STRING'].blank?
63
+ path += "?#{env['QUERY_STRING']}"
64
+ end
65
+
66
+ http = Net::HTTP.new(host, port)
67
+ http.read_timeout = 60
68
+ http.open_timeout = 60
69
+
70
+ request = Net::HTTPGenericRequest.new(
71
+ env['REQUEST_METHOD'], !!body, true, path,
72
+ env_to_http_headers(env))
73
+
74
+ request.body_stream = body if body
75
+
76
+ Thread.new do
77
+ begin
78
+ http.request(request) do |response|
79
+ hdrs = {}
80
+ response.each_header do |name, val|
81
+ hdrs[name] = val
82
+ end
83
+
84
+ queue << [ response.code.to_i, hdrs ]
85
+
86
+ response.read_body do |chunk|
87
+ queue << chunk
88
+ end
89
+
90
+ queue << nil
91
+ end
92
+ rescue Exception => e
93
+ queue << e
94
+ end
95
+ end
96
+
97
+ queue
98
+ end
99
+
100
+ def env_to_http_headers(env)
101
+ {}.tap do |hdrs|
102
+ env.each do |name, val|
103
+ next unless name.is_a?(String)
104
+ next if name == 'HTTP_HOST'
105
+ next unless name =~ /^HTTP_/ || KEEP.include?(name)
106
+
107
+ hdrs[ headerize(name) ] = val
108
+ end
109
+ end
110
+ end
111
+
112
+ def headerize(str)
113
+ parts = str.gsub(/^HTTP_/, '').split('_')
114
+ parts.map! { |p| p.capitalize }.join('-')
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,20 @@
1
+ module Strobe
2
+ class Middleware::Rewrite
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ status, hdrs, body = @app.call(env)
9
+
10
+ if status == 404
11
+ path_info = env['PATH_INFO']
12
+ env['PATH_INFO'] = "#{path_info}/index.html".gsub('//', '/')
13
+
14
+ return @app.call(env)
15
+ end
16
+
17
+ [ status, hdrs, body ]
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ require 'sproutcore'
2
+
3
+ class SC::Rack::Service
4
+ def call(env)
5
+ wrapped_app.call(env)
6
+ end
7
+
8
+ def wrapped_app
9
+ @wrapped_app ||= Strobe::Middleware::Proxy.new(@app)
10
+ end
11
+ end
data/lib/strobe.rb CHANGED
@@ -17,6 +17,11 @@ module Strobe
17
17
  autoload :Resources, 'strobe/resources'
18
18
  autoload :Validations, 'strobe/validations'
19
19
 
20
+ module Middleware
21
+ autoload :Proxy, 'strobe/middleware/proxy'
22
+ autoload :Rewrite, 'strobe/middleware/rewrite'
23
+ end
24
+
20
25
  module Resource
21
26
  autoload :Base, 'strobe/resource/base'
22
27
  autoload :Collection, 'strobe/resource/collection'
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 3
9
- version: 0.1.3
8
+ - 4
9
+ version: 0.1.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Yehuda Katz
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-05 00:00:00 -08:00
18
+ date: 2011-01-11 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -117,6 +117,8 @@ files:
117
117
  - lib/strobe/connection.rb
118
118
  - lib/strobe/identity_map.rb
119
119
  - lib/strobe/key.rb
120
+ - lib/strobe/middleware/proxy.rb
121
+ - lib/strobe/middleware/rewrite.rb
120
122
  - lib/strobe/resource/base.rb
121
123
  - lib/strobe/resource/collection.rb
122
124
  - lib/strobe/resource/singleton.rb
@@ -129,6 +131,7 @@ files:
129
131
  - lib/strobe/resources/team.rb
130
132
  - lib/strobe/resources/user.rb
131
133
  - lib/strobe/resources.rb
134
+ - lib/strobe/sproutcore.rb
132
135
  - lib/strobe/validations.rb
133
136
  - lib/strobe.rb
134
137
  - bin/strobe