vvlad-rack_ext_direct 0.0.3 → 0.0.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.
- data/example/config.ru +12 -0
- data/lib/rack/ext/direct/client.rb +7 -4
- data/lib/rack/ext/direct/remoting.rb +31 -11
- data/lib/rack/ext/direct/request.rb +1 -0
- metadata +2 -1
data/example/config.ru
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$:<<File.expand_path(File.dirname( __FILE__) + "/../lib/")
|
2
|
+
require 'rack'
|
3
|
+
require 'rack/ext/direct'
|
4
|
+
|
5
|
+
# use Rack::Ext::Direct::Remoting,
|
6
|
+
# :service_root => "app/services",
|
7
|
+
# :remoting_path => /^\/services$/,
|
8
|
+
# :report_errors => true
|
9
|
+
|
10
|
+
use Rack::Ext::Direct::Dispatching ,"app/services", :report_errors => true
|
11
|
+
|
12
|
+
run Rack::File.new "public"
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'active_support'
|
2
2
|
require 'open-uri'
|
3
3
|
require 'net/http'
|
4
4
|
require 'json'
|
@@ -8,9 +8,12 @@ module Rack
|
|
8
8
|
module Direct
|
9
9
|
class Client
|
10
10
|
class_inheritable_accessor :service_url
|
11
|
+
class_inheritable_accessor :service_options
|
11
12
|
class <<self
|
12
|
-
def end_point_url(url)
|
13
|
+
def end_point_url(url,options={})
|
14
|
+
options.assert_valid_keys :service_name
|
13
15
|
self.service_url= url
|
16
|
+
self.service_options = options.to_options
|
14
17
|
end
|
15
18
|
|
16
19
|
def next_tid
|
@@ -20,7 +23,7 @@ module Rack
|
|
20
23
|
|
21
24
|
def perform_request(id,*params,&block)
|
22
25
|
request = {
|
23
|
-
:action =>
|
26
|
+
:action => service_options[:service_name]||name,
|
24
27
|
:method => id,
|
25
28
|
:data => [*params],
|
26
29
|
:type => "rpc",
|
@@ -28,7 +31,7 @@ module Rack
|
|
28
31
|
}
|
29
32
|
|
30
33
|
|
31
|
-
url = URI.parse(
|
34
|
+
url = URI.parse(service_url)
|
32
35
|
req = Net::HTTP::Post.new(url.path.empty? ? "/" : url.path)
|
33
36
|
req.body = request.to_json
|
34
37
|
req.content_type= "application/json"
|
@@ -1,16 +1,22 @@
|
|
1
1
|
require 'json'
|
2
|
-
require '
|
2
|
+
require 'active_support'
|
3
|
+
|
3
4
|
module Rack
|
4
5
|
module Ext
|
5
6
|
module Direct
|
6
|
-
class Remoting
|
7
|
-
|
7
|
+
class Remoting
|
8
|
+
|
9
|
+
include ActiveSupport::Dependencies
|
10
|
+
|
8
11
|
def initialize(app,options={})
|
9
12
|
@app = app
|
10
13
|
@services_root = ::File.expand_path(options[:service_root])
|
11
14
|
@services = {}
|
12
15
|
@report_errors = options[:report_errors] || false
|
13
16
|
@remoting_path = options[:remoting_path]
|
17
|
+
@reloading = ENV["RACK_ENV"] == "development"
|
18
|
+
load_paths << @services_root
|
19
|
+
|
14
20
|
raise "Services Directory `#{@services_root}' missing." unless ::File.directory? @services_root
|
15
21
|
end
|
16
22
|
|
@@ -29,11 +35,16 @@ module Rack
|
|
29
35
|
return @app.call(env) if service_class.nil?
|
30
36
|
|
31
37
|
response = begin
|
32
|
-
|
38
|
+
instance = service_class.new
|
39
|
+
response = Response.new request,instance.send(request.method.to_sym,request.data)
|
40
|
+
instance = nil
|
41
|
+
response
|
33
42
|
rescue => e
|
34
43
|
ResponseException.new request,e
|
35
44
|
end
|
36
45
|
|
46
|
+
remove_unloadable_constants!
|
47
|
+
|
37
48
|
[ 200 , {"Content-Type" => "application/json"}, [response.to_json]]
|
38
49
|
|
39
50
|
end
|
@@ -56,18 +67,27 @@ module Rack
|
|
56
67
|
end
|
57
68
|
|
58
69
|
def service_by_name(service_name)
|
59
|
-
|
70
|
+
try_load_service(service_name)
|
60
71
|
end
|
61
72
|
|
62
73
|
def try_load_service(service_name)
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
74
|
+
begin
|
75
|
+
if @reloading
|
76
|
+
puts "Reloading #{service_name}"
|
77
|
+
ActiveSupport::Dependencies.load_file("#{@services_root}/#{service_name.underscore}.rb")
|
78
|
+
else
|
79
|
+
require "#{@services_root}/#{service_name.underscore}.rb"
|
80
|
+
end
|
81
|
+
return service_name.constantize
|
82
|
+
rescue => e
|
83
|
+
puts e
|
84
|
+
puts e.backtrace
|
85
|
+
end
|
86
|
+
nil
|
68
87
|
end
|
69
88
|
|
70
89
|
end
|
71
90
|
end
|
72
91
|
end
|
73
|
-
end
|
92
|
+
end
|
93
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vvlad-rack_ext_direct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Verestiuc Vlad Ovidiu
|
@@ -53,6 +53,7 @@ extra_rdoc_files:
|
|
53
53
|
files:
|
54
54
|
- example/app/services/test_service.rb
|
55
55
|
- example/client.rb
|
56
|
+
- example/config.ru
|
56
57
|
- lib/rack/ext/direct/client.rb
|
57
58
|
- lib/rack/ext/direct/dispatching.rb
|
58
59
|
- lib/rack/ext/direct/remote_error.rb
|