sample_core_api 0.0.8 → 0.0.9.pre.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 68ecb98d9ffc306169575a50fd4f2ebf1d260926b673867252ea73d29b115685
4
- data.tar.gz: 91bc5d365ccc71c597d9669a7a1b614f8902711a397f1a0fbd6d14dc2eec48a0
3
+ metadata.gz: ffbc9ce42646170555907763750ce5bc3f538eb679f44b77340eb5567b796b68
4
+ data.tar.gz: af2c9277493b7ce60deeb45bff4c58cd82965b06c2c96f547b31dd912d043051
5
5
  SHA512:
6
- metadata.gz: f0465b78f64b99eab510fb078ec7a7c665afab80d8ad62b5d56fa1cc49d3cce6159f40de608b46aa404b605663507170be19562724faa27427d8fc3b6835592d
7
- data.tar.gz: 0aad292b94b8d642c0219256fb6ed958f64de7fb22753be5171347c9fb15848d21c454509e5c36794dc293d07ec8da9667dfa5d13ca6e3cbf5cc38dd99f3d619
6
+ metadata.gz: 072f69955dbc3e9765a38a61958b77e9d5ea09d864329bcbc839fe28fef266419088b2400086bb72249738dc1b27f95542f7b10bd6a7e3c1c11b7ee90819221d
7
+ data.tar.gz: fded44fd29b6edfd37c0f5cfb8fc0f7eef22b86268ab8976c5bdf92d560cb0ab7a629b2e4ddffef87da1e7cae8f39f6124b0fca68a6c02eb79d8482c4725baf3
@@ -0,0 +1,20 @@
1
+ require_relative 'consul/consul_service'
2
+ require_relative 'cassandra/cassandra_service'
3
+
4
+ class AppService < Injector
5
+ include Inject['consul_service', 'cassandra_service']
6
+
7
+ injector
8
+
9
+ def start
10
+ puts 'start app_service'
11
+ consul_service.start
12
+ cassandra_service.start
13
+ end
14
+
15
+ def stop
16
+ puts 'stop app_service'
17
+ consul_service.stop
18
+ cassandra_service.stop
19
+ end
20
+ end
@@ -19,30 +19,28 @@ module ApplicationConfiguration
19
19
  check: {
20
20
  id: "bluesky-api",
21
21
  name: "HTTP Health Check API on port 9292",
22
- http: "http://localhost:9292/health-check",
22
+ http: "http://localhost:9292/health-check-new",
23
23
  tls_skip_verify: false,
24
24
  method: "GET",
25
25
  interval: "10s",
26
26
  timeout: "1s"
27
27
  }
28
- },
29
- consul_check_http: "health-check-new"
28
+ }
30
29
  }
31
- config_consul[:consul_check_http] = config_rest[:consul_check_http]
32
30
  config_consul[:consul_service_data][:port] = config_rest[:port]
33
- config_consul[:consul_service_data][:check][:http] = "http://#{config_rest[:host]}:#{config_rest[:port]}/#{config_rest[:consul_check_http]}"
31
+ config_consul[:consul_service_data][:check][:http] = "http://#{config_rest[:host]}:#{config_rest[:port].to_s}/#{random_name}"
34
32
  config_consul
35
33
  end
36
34
 
37
35
  def config_rest
38
36
  config_rest = {
39
- host: 'localhost:9292',
40
- path: '/api/v1',
41
- consul_check_http: random_name
37
+ host: 'localhost',
38
+ port: 9292,
39
+ path: '/api/v1'
42
40
  }
43
41
  end
44
42
 
45
43
  def random_name
46
- "/health-check-#{SecureRandom.urlsafe_base64(8)}"
44
+ "health-check-#{SecureRandom.urlsafe_base64(8)}"
47
45
  end
48
46
  end
@@ -1,10 +1,12 @@
1
1
  require 'cassandra'
2
- require_relative '../ioc/container'
2
+ require_relative '../ioc/injector'
3
3
  require_relative 'abstracted_cassandra_repository'
4
4
 
5
- class CassandraService
5
+ class CassandraService < Injector
6
6
  include ::AbstractedCassandraRepository
7
7
 
8
+ injector
9
+
8
10
  attr_accessor :cassandra_cluster
9
11
 
10
12
  def initialize
@@ -28,4 +30,3 @@ class CassandraService
28
30
  end
29
31
  end
30
32
  end
31
- Service CassandraService
@@ -19,8 +19,7 @@ module ConsulConfiguration
19
19
  interval: "10s",
20
20
  timeout: "1s"
21
21
  }
22
- },
23
- consul_check_http: "health-check"
22
+ }
24
23
  }.merge( ConsulConfiguration.config_consul )
25
24
 
26
25
  def configure
@@ -1,11 +1,13 @@
1
1
  require 'diplomat'
2
- require_relative '../ioc/container'
2
+ require_relative '../ioc/injector'
3
3
  require_relative 'consul_configuration'
4
4
  require_relative '../rest/rest_service'
5
5
 
6
- class ConsulService
6
+ class ConsulService < Injector
7
7
  extend ConsulConfiguration
8
8
 
9
+ injector
10
+
9
11
  attr_accessor :consul_service_data
10
12
 
11
13
  def initialize(option=nil)
@@ -15,16 +17,17 @@ class ConsulService
15
17
 
16
18
  def start
17
19
  puts 'start consul'
18
- puts ConsulConfiguration::CONFIG[:consul_check_http]
19
20
  consul_service_data = ConsulConfiguration::CONFIG[:consul_service_data] if consul_service_data.nil?
20
21
  Diplomat::Service.register(consul_service_data)
22
+
21
23
  # start url for consul to check health
22
- RestService.start_consul_check_http(ConsulConfiguration::CONFIG[:consul_check_http])
24
+ consul_check_route = "/" + consul_service_data[:check][:http].split('/').last
25
+ RestService.start_consul_check_http(consul_check_route)
23
26
  end
24
27
 
25
28
  def stop
26
29
  puts 'stop consul'
27
- Diplomat::Service.deregister(consul_service_data[:id])
30
+ consul_service_id = self.consul_service_data.try(:id) || ConsulConfiguration::CONFIG[:consul_service_data][:id]
31
+ Diplomat::Service.deregister(consul_service_id)
28
32
  end
29
33
  end
30
- Service ConsulService
@@ -1,28 +1,44 @@
1
- require "method_decorators"
1
+ require_relative '../ioc/injector'
2
2
  require_relative '../security/security_context_holder'
3
3
 
4
+ class Authenticator < Injector
4
5
 
5
- class Authenticate < MethodDecorators::Decorator
6
- def initialize(authorize_function, arguments)
7
- @authorize_function = authorize_function
8
- @arguments = arguments
9
- end
6
+ injector
10
7
 
11
- def call(wrapped, this, *args, &blk)
12
- puts "before authentication"
13
- puts "user #" + SecurityContextHolder.get_user()
14
- if self.send(@authorize_function, @arguments, *args, &blk)
15
- result = wrapped.call(*args, &blk)
16
- else
17
- raise CommonError::InvalidToken
18
- end
19
- puts "after authentication"
20
-
21
- result
8
+ def has_roles roles
9
+ lambda {|roles|
10
+ true
11
+ }
22
12
  end
23
13
 
24
- def has_roles(roles, *args, &blk)
25
- # check roles here
26
- return true
14
+ def authenticate isAuthorized
15
+ lambda {|wrapped, this, *args, &blk|
16
+ puts "isAuthorized"
17
+ }
27
18
  end
28
- end
19
+ end
20
+
21
+ # class Authenticate < MethodDecorators::Decorator
22
+ # def initialize(authorize_function, arguments)
23
+ # @authorize_function = authorize_function
24
+ # @arguments = arguments
25
+ # end
26
+
27
+ # def call(wrapped, this, *args, &blk)
28
+ # puts "before authentication"
29
+ # puts "user #" + SecurityContextHolder.get_user()
30
+ # if self.send(@authorize_function, @arguments, *args, &blk)
31
+ # result = wrapped.call(*args, &blk)
32
+ # else
33
+ # raise CommonError::InvalidToken
34
+ # end
35
+ # puts "after authentication"
36
+
37
+ # result
38
+ # end
39
+
40
+ # def has_roles(roles, *args, &blk)
41
+ # # check roles here
42
+ # return true
43
+ # end
44
+ # end
@@ -1,25 +1,35 @@
1
- require "method_decorators"
1
+ require_relative '../ioc/injector'
2
2
 
3
- class Valid < MethodDecorators::Decorator
4
- def initialize(validate_function, arguments)
5
- @validate_function = validate_function
6
- @arguments = arguments
7
- end
3
+ class Validator < Injector
8
4
 
9
- def call(wrapped, this, *args, &blk)
10
- puts "before validation"
11
- if self.send(@validate_function, @arguments, *args, &blk)
12
- result = wrapped.call(*args, &blk)
13
- else
14
- raise CommonError::InvalidData
15
- end
16
- puts "after validation"
17
- result
18
- end
5
+ injector
19
6
 
20
- def validate(arguments, *args, &blk)
21
- # check validation here
22
- return true
7
+ def valid
8
+ lambda {|wrapped, this, *args, &blk|
9
+ puts "Valid"
10
+ }
23
11
  end
12
+ end
13
+
14
+ # class Valid < MethodDecorators::Decorator
15
+ # def initialize(validate_function, arguments)
16
+ # @validate_function = validate_function
17
+ # @arguments = arguments
18
+ # end
19
+
20
+ # def call(wrapped, this, *args, &blk)
21
+ # puts "before validation"
22
+ # if self.send(@validate_function, @arguments, *args, &blk)
23
+ # result = wrapped.call(*args, &blk)
24
+ # else
25
+ # raise CommonError::InvalidData
26
+ # end
27
+ # puts "after validation"
28
+ # result
29
+ # end
24
30
 
25
- end
31
+ # def validate(arguments, *args, &blk)
32
+ # # check validation here
33
+ # return true
34
+ # end
35
+ # end
data/lib/ioc/container.rb CHANGED
@@ -1,13 +1,5 @@
1
1
  require "dry-container"
2
2
  require "dry-auto_inject"
3
- require "dry/inflector"
4
3
 
5
- Inflector = Dry::Inflector.new
6
4
  Container = Dry::Container.new
7
5
  Inject = Dry::AutoInject(Container)
8
-
9
-
10
- def Service clazz, name = nil
11
- name = name.nil? ? clazz.name[0, 1].downcase + clazz.name[1..-1] : name
12
- Container.register(Inflector.underscore(name), clazz.new)
13
- end
@@ -0,0 +1,53 @@
1
+ require_relative "container"
2
+ require "dry/inflector"
3
+
4
+ Inflector = Dry::Inflector.new
5
+ def injector name=nil, instance_method=nil
6
+ name = name.nil? ? self.name[0, 1].downcase + self.name[1..-1] : name
7
+ instance_method = instance_method.nil? ? self.new : instance_method
8
+ Container.register(Inflector.underscore(name), instance_method)
9
+ end
10
+
11
+ class Injector
12
+ class << self
13
+ @@decorators = Hash.new
14
+
15
+ def decorators
16
+ @@decorators
17
+ end
18
+
19
+ def decorate name, decorators
20
+ if @@decorators[name].nil?
21
+ @@decorators[name] = []
22
+ end
23
+ @@decorators[name] = {:is_processed => false, :decorators => decorators}
24
+ end
25
+
26
+ def method_added(name)
27
+ super
28
+ orig_method = instance_method(name)
29
+ decorators = self.decorators[name]
30
+ return if decorators.nil? || decorators[:is_processed] == true
31
+
32
+ if private_method_defined?(name);
33
+ visibility = :private
34
+ elsif protected_method_defined?(name);
35
+ visibility = :protected
36
+ else
37
+ visibility = :public
38
+ end
39
+
40
+ decorators[:is_processed] = true
41
+ define_method(name) do |*args, &blk|
42
+ orig_method.bind(self).call(*args, &blk)
43
+ end
44
+
45
+ case visibility
46
+ when :protected;
47
+ protected name
48
+ when :private;
49
+ private name
50
+ end
51
+ end
52
+ end
53
+ end
@@ -2,9 +2,9 @@ module RestConfiguration
2
2
  extend ApplicationConfiguration
3
3
 
4
4
  CONFIG = {
5
- host: 'localhost:9292',
6
- path: '/api/v1',
7
- consul_check_http: "/health-check"
5
+ host: 'localhost',
6
+ port: 9292,
7
+ path: '/api/v1'
8
8
  }.merge( RestConfiguration.config_rest )
9
9
 
10
10
  end
@@ -1,15 +1,15 @@
1
1
  require 'bundler/setup'
2
2
  require 'sinatra/base'
3
- require_relative 'ioc/container'
3
+ require 'request_store'
4
+
4
5
  require_relative 'application_configuration'
5
- require_relative 'cassandra/cassandra_service'
6
- require_relative 'consul/consul_service'
6
+ require_relative 'app_service'
7
7
  require_relative 'rest/rest_service'
8
8
  # Dir["#{File.dirname(__FILE__)}/**/*.rb"].each {|file| require file }
9
9
 
10
10
 
11
11
  class Application
12
- extend ApplicationConfiguration
12
+ extend ApplicationConfiguration
13
13
 
14
14
  def initialize(rests=[], config={})
15
15
  @config = config
@@ -19,10 +19,6 @@ class Application
19
19
  def init_rest_service
20
20
  rests = @rests
21
21
  return Sinatra.new {
22
- # before '/*' do
23
- # puts "set local thread"
24
- # # Thread.current[:user] = "ltran"
25
- # end
26
22
  rests.each {|a| use a}
27
23
  not_found do
28
24
  JSON RestService.error_response('EntityNotFound')
@@ -32,13 +28,11 @@ class Application
32
28
 
33
29
  def start
34
30
  puts 'start'
35
- Container["consul_service"].start
36
- Container["cassandra_service"].start
31
+ Container['app_service'].start
37
32
  init_rest_service
38
33
  end
39
34
 
40
35
  def stop
41
- Container["consul_service"].stop
42
- Container["cassandra_service"].stop
36
+ Container['app_service'].stop
43
37
  end
44
38
  end
@@ -1,5 +1,3 @@
1
- require 'request_store'
2
-
3
1
  class SecurityContextHolder
4
2
  def self.get_user
5
3
  RequestStore.store[:user]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sample_core_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9.pre.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fusin Thang
@@ -110,26 +110,6 @@ dependencies:
110
110
  - - ">="
111
111
  - !ruby/object:Gem::Version
112
112
  version: 3.2.2
113
- - !ruby/object:Gem::Dependency
114
- name: method_decorators
115
- requirement: !ruby/object:Gem::Requirement
116
- requirements:
117
- - - "~>"
118
- - !ruby/object:Gem::Version
119
- version: 0.9.6
120
- - - ">="
121
- - !ruby/object:Gem::Version
122
- version: 0.9.6
123
- type: :runtime
124
- prerelease: false
125
- version_requirements: !ruby/object:Gem::Requirement
126
- requirements:
127
- - - "~>"
128
- - !ruby/object:Gem::Version
129
- version: 0.9.6
130
- - - ">="
131
- - !ruby/object:Gem::Version
132
- version: 0.9.6
133
113
  - !ruby/object:Gem::Dependency
134
114
  name: request_store
135
115
  requirement: !ruby/object:Gem::Requirement
@@ -236,16 +216,16 @@ executables: []
236
216
  extensions: []
237
217
  extra_rdoc_files: []
238
218
  files:
219
+ - lib/app_service.rb
239
220
  - lib/application_configuration.rb
240
221
  - lib/cassandra/abstracted_cassandra_repository.rb
241
222
  - lib/cassandra/cassandra_service.rb
242
223
  - lib/consul/consul_configuration.rb
243
224
  - lib/consul/consul_service.rb
244
- - lib/container.rb
245
225
  - lib/decoractors/authenticate.rb
246
226
  - lib/decoractors/valid.rb
247
- - lib/ioc/base_service.rb
248
227
  - lib/ioc/container.rb
228
+ - lib/ioc/injector.rb
249
229
  - lib/rest/rest_common_error.rb
250
230
  - lib/rest/rest_configuration.rb
251
231
  - lib/rest/rest_error_handler.rb
@@ -268,9 +248,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
268
248
  version: 2.3.0
269
249
  required_rubygems_version: !ruby/object:Gem::Requirement
270
250
  requirements:
271
- - - ">="
251
+ - - ">"
272
252
  - !ruby/object:Gem::Version
273
- version: '0'
253
+ version: 1.3.1
274
254
  requirements: []
275
255
  rubyforge_project:
276
256
  rubygems_version: 2.7.7
data/lib/container.rb DELETED
@@ -1,12 +0,0 @@
1
- require "dry-container"
2
- require "dry-auto_inject"
3
- require "dry/inflector"
4
-
5
- Inflector = Dry::Inflector.new
6
- Container = Dry::Container.new
7
- Inject = Dry::AutoInject(Container)
8
-
9
- def Service clazz, name = nil
10
- name = name.nil? ? (clazz.name[0, 1].downcase + clazz.name[1..-1]) : name
11
- Container.register(Inflector.underscore(name), clazz.new)
12
- end
@@ -1,5 +0,0 @@
1
- require "method_decorators"
2
-
3
- class BaseService
4
- extend MethodDecorators
5
- end