sample_core_api 0.0.7 → 0.0.8

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
- SHA1:
3
- metadata.gz: 5abf9f1aa3ad1476094adca42ab1d1b1ec26d4ff
4
- data.tar.gz: 85f6c2bc749250447471ef412eaaef49abeca044
2
+ SHA256:
3
+ metadata.gz: 68ecb98d9ffc306169575a50fd4f2ebf1d260926b673867252ea73d29b115685
4
+ data.tar.gz: 91bc5d365ccc71c597d9669a7a1b614f8902711a397f1a0fbd6d14dc2eec48a0
5
5
  SHA512:
6
- metadata.gz: a3a8b0ecb8091abef6053ad05f6da90a393c1f5bf0db14c2b5af506a59f21703eac03b35da3a13a3806360abbb17f3317dbdb0bfeb387d507d9c5711f8b20884
7
- data.tar.gz: 85f77a3a5f91c73913a2020425671d5eeb8a78166c122c339d21497336eb560fe7ae7369f89a715a4a9e28ff8c10270cedfe4244b3bdedfe108125256bb8ac17
6
+ metadata.gz: f0465b78f64b99eab510fb078ec7a7c665afab80d8ad62b5d56fa1cc49d3cce6159f40de608b46aa404b605663507170be19562724faa27427d8fc3b6835592d
7
+ data.tar.gz: 0aad292b94b8d642c0219256fb6ed958f64de7fb22753be5171347c9fb15848d21c454509e5c36794dc293d07ec8da9667dfa5d13ca6e3cbf5cc38dd99f3d619
@@ -1,25 +1,48 @@
1
+ require 'securerandom'
2
+
1
3
  module ApplicationConfiguration
2
4
  def config
3
5
  {
4
- consul: {
5
- consul_service_data: {
6
- name: "Service-1",
7
- id: "Service1",
8
- port: 9292,
9
- check: {
10
- id: "bluesky-api",
11
- name: "HTTP Health Check API on port 9292",
12
- http: "http://localhost:9292/health-check-new",
13
- tls_skip_verify: false,
14
- method: "GET",
15
- interval: "10s",
16
- timeout: "1s"
17
- }
18
- }
19
- },
20
- rest: {
21
- consul_check_http: "/health-check-new"
6
+ rest: config_rest,
7
+ consul: config_consul
8
+ }
9
+ end
10
+
11
+ def config_consul
12
+ config_consul = {
13
+ port: 8500,
14
+ path: "http://localhost",
15
+ consul_service_data: {
16
+ name: "Service-1",
17
+ id: "Service1",
18
+ port: 9292,
19
+ check: {
20
+ id: "bluesky-api",
21
+ name: "HTTP Health Check API on port 9292",
22
+ http: "http://localhost:9292/health-check",
23
+ tls_skip_verify: false,
24
+ method: "GET",
25
+ interval: "10s",
26
+ timeout: "1s"
27
+ }
22
28
  },
29
+ consul_check_http: "health-check-new"
23
30
  }
31
+ config_consul[:consul_check_http] = config_rest[:consul_check_http]
32
+ 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]}"
34
+ config_consul
35
+ end
36
+
37
+ def config_rest
38
+ config_rest = {
39
+ host: 'localhost:9292',
40
+ path: '/api/v1',
41
+ consul_check_http: random_name
42
+ }
43
+ end
44
+
45
+ def random_name
46
+ "/health-check-#{SecureRandom.urlsafe_base64(8)}"
24
47
  end
25
48
  end
@@ -1,5 +1,5 @@
1
1
  require 'cassandra'
2
- require_relative '../container'
2
+ require_relative '../ioc/container'
3
3
  require_relative 'abstracted_cassandra_repository'
4
4
 
5
5
  class CassandraService
@@ -28,5 +28,4 @@ class CassandraService
28
28
  end
29
29
  end
30
30
  end
31
-
32
- registerService CassandraService.new
31
+ Service CassandraService
@@ -19,8 +19,9 @@ module ConsulConfiguration
19
19
  interval: "10s",
20
20
  timeout: "1s"
21
21
  }
22
- }
23
- }.merge( ConsulConfiguration.config[:consul] )
22
+ },
23
+ consul_check_http: "health-check"
24
+ }.merge( ConsulConfiguration.config_consul )
24
25
 
25
26
  def configure
26
27
  Diplomat.configure do |config|
@@ -1,23 +1,25 @@
1
1
  require 'diplomat'
2
- require_relative '../container'
2
+ require_relative '../ioc/container'
3
3
  require_relative 'consul_configuration'
4
+ require_relative '../rest/rest_service'
4
5
 
5
6
  class ConsulService
6
- extend ::ConsulConfiguration
7
+ extend ConsulConfiguration
7
8
 
8
9
  attr_accessor :consul_service_data
9
10
 
10
- def initialize
11
+ def initialize(option=nil)
11
12
  puts 'init consul'
12
- self.consul_service_data = ConsulService::configure
13
+ self.consul_service_data = ConsulService.configure
13
14
  end
14
15
 
15
16
  def start
16
17
  puts 'start consul'
17
- if !consul_service_data
18
- consul_service_data = ConsulService::configure
19
- end
18
+ puts ConsulConfiguration::CONFIG[:consul_check_http]
19
+ consul_service_data = ConsulConfiguration::CONFIG[:consul_service_data] if consul_service_data.nil?
20
20
  Diplomat::Service.register(consul_service_data)
21
+ # start url for consul to check health
22
+ RestService.start_consul_check_http(ConsulConfiguration::CONFIG[:consul_check_http])
21
23
  end
22
24
 
23
25
  def stop
@@ -25,5 +27,4 @@ class ConsulService
25
27
  Diplomat::Service.deregister(consul_service_data[:id])
26
28
  end
27
29
  end
28
-
29
- registerService ConsulService.new
30
+ Service ConsulService
data/lib/container.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  require "dry-container"
2
2
  require "dry-auto_inject"
3
+ require "dry/inflector"
3
4
 
5
+ Inflector = Dry::Inflector.new
4
6
  Container = Dry::Container.new
5
7
  Inject = Dry::AutoInject(Container)
6
8
 
7
- def registerService instance
8
- class_name = instance.class.name
9
- name = class_name.sub(class_name[0], class_name[0].downcase)
10
- Container.register(name, instance)
11
- end
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
@@ -0,0 +1,28 @@
1
+ require "method_decorators"
2
+ require_relative '../security/security_context_holder'
3
+
4
+
5
+ class Authenticate < MethodDecorators::Decorator
6
+ def initialize(authorize_function, arguments)
7
+ @authorize_function = authorize_function
8
+ @arguments = arguments
9
+ end
10
+
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
22
+ end
23
+
24
+ def has_roles(roles, *args, &blk)
25
+ # check roles here
26
+ return true
27
+ end
28
+ end
@@ -0,0 +1,25 @@
1
+ require "method_decorators"
2
+
3
+ class Valid < MethodDecorators::Decorator
4
+ def initialize(validate_function, arguments)
5
+ @validate_function = validate_function
6
+ @arguments = arguments
7
+ end
8
+
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
19
+
20
+ def validate(arguments, *args, &blk)
21
+ # check validation here
22
+ return true
23
+ end
24
+
25
+ end
@@ -0,0 +1,5 @@
1
+ require "method_decorators"
2
+
3
+ class BaseService
4
+ extend MethodDecorators
5
+ end
@@ -0,0 +1,13 @@
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
+
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
@@ -5,6 +5,6 @@ module RestConfiguration
5
5
  host: 'localhost:9292',
6
6
  path: '/api/v1',
7
7
  consul_check_http: "/health-check"
8
- }.merge( RestConfiguration.config[:rest] )
8
+ }.merge( RestConfiguration.config_rest )
9
9
 
10
10
  end
@@ -29,11 +29,12 @@ class RestService < RestApplication
29
29
  extend ::RestConfiguration
30
30
  extend ::RestResponseModel
31
31
 
32
- # Health Check for Consul
33
- get RestConfiguration::CONFIG[:consul_check_http] do
34
- 'The service is healthy!'
32
+ # Health Check for Consul, run when start Consul
33
+ def self.start_consul_check_http(url)
34
+ get url do
35
+ 'The service is healthy!'
36
+ end
35
37
  end
36
-
37
38
  # API route
38
39
  # namespace RestConfiguration::CONFIG[:path] do
39
40
  # get '/seed_1' do
@@ -55,8 +56,4 @@ class RestService < RestApplication
55
56
  # ]
56
57
  # raise CommonError::InvalidData
57
58
  # end
58
-
59
- not_found do
60
- JSON RestService.error_response('EntityNotFound')
61
- end
62
59
  end
@@ -1,32 +1,44 @@
1
1
  require 'bundler/setup'
2
2
  require 'sinatra/base'
3
+ require_relative 'ioc/container'
3
4
  require_relative 'application_configuration'
4
5
  require_relative 'cassandra/cassandra_service'
5
6
  require_relative 'consul/consul_service'
6
7
  require_relative 'rest/rest_service'
7
- require_relative 'container'
8
8
  # Dir["#{File.dirname(__FILE__)}/**/*.rb"].each {|file| require file }
9
9
 
10
- class SampleCoreApi
11
- extend ::ApplicationConfiguration
12
- include Inject["consulService","cassandraService"]
13
10
 
14
- def initialize(config)
11
+ class Application
12
+ extend ApplicationConfiguration
13
+
14
+ def initialize(rests=[], config={})
15
15
  @config = config
16
+ @rests = rests
16
17
  end
17
18
 
18
- def run_rest_services
19
- RestService.run!
19
+ def init_rest_service
20
+ rests = @rests
21
+ return Sinatra.new {
22
+ # before '/*' do
23
+ # puts "set local thread"
24
+ # # Thread.current[:user] = "ltran"
25
+ # end
26
+ rests.each {|a| use a}
27
+ not_found do
28
+ JSON RestService.error_response('EntityNotFound')
29
+ end
30
+ }
20
31
  end
21
32
 
22
33
  def start
23
- Container["consulService"].start
24
- Container["cassandraService"].start
25
- run_rest_services
34
+ puts 'start'
35
+ Container["consul_service"].start
36
+ Container["cassandra_service"].start
37
+ init_rest_service
26
38
  end
27
39
 
28
40
  def stop
29
- consulService.stop
30
- cassandraService.stop
41
+ Container["consul_service"].stop
42
+ Container["cassandra_service"].stop
31
43
  end
32
44
  end
@@ -0,0 +1,7 @@
1
+ require 'request_store'
2
+
3
+ class SecurityContextHolder
4
+ def self.get_user
5
+ RequestStore.store[:user]
6
+ end
7
+ end
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.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fusin Thang
@@ -11,45 +11,25 @@ cert_chain: []
11
11
  date: 2018-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: dry-container
14
+ name: dry-system
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.6.0
19
+ version: 0.10.0
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 0.6.0
22
+ version: 0.10.0
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - "~>"
28
28
  - !ruby/object:Gem::Version
29
- version: 0.6.0
29
+ version: 0.10.0
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 0.6.0
33
- - !ruby/object:Gem::Dependency
34
- name: dry-auto_inject
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - "~>"
38
- - !ruby/object:Gem::Version
39
- version: 0.4.6
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- version: 0.4.6
43
- type: :runtime
44
- prerelease: false
45
- version_requirements: !ruby/object:Gem::Requirement
46
- requirements:
47
- - - "~>"
48
- - !ruby/object:Gem::Version
49
- version: 0.4.6
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- version: 0.4.6
32
+ version: 0.10.0
53
33
  - !ruby/object:Gem::Dependency
54
34
  name: sinatra
55
35
  requirement: !ruby/object:Gem::Requirement
@@ -130,6 +110,46 @@ dependencies:
130
110
  - - ">="
131
111
  - !ruby/object:Gem::Version
132
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
+ - !ruby/object:Gem::Dependency
134
+ name: request_store
135
+ requirement: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: 1.4.1
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: 1.4.1
143
+ type: :runtime
144
+ prerelease: false
145
+ version_requirements: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - "~>"
148
+ - !ruby/object:Gem::Version
149
+ version: 1.4.1
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: 1.4.1
133
153
  - !ruby/object:Gem::Dependency
134
154
  name: bundler
135
155
  requirement: !ruby/object:Gem::Requirement
@@ -222,12 +242,17 @@ files:
222
242
  - lib/consul/consul_configuration.rb
223
243
  - lib/consul/consul_service.rb
224
244
  - lib/container.rb
245
+ - lib/decoractors/authenticate.rb
246
+ - lib/decoractors/valid.rb
247
+ - lib/ioc/base_service.rb
248
+ - lib/ioc/container.rb
225
249
  - lib/rest/rest_common_error.rb
226
250
  - lib/rest/rest_configuration.rb
227
251
  - lib/rest/rest_error_handler.rb
228
252
  - lib/rest/rest_response_model.rb
229
253
  - lib/rest/rest_service.rb
230
254
  - lib/sample_core_api.rb
255
+ - lib/security/security_context_holder.rb
231
256
  homepage: http://rubygems.org/gems/sample_core_api
232
257
  licenses:
233
258
  - MIT
@@ -248,7 +273,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
248
273
  version: '0'
249
274
  requirements: []
250
275
  rubyforge_project:
251
- rubygems_version: 2.6.13
276
+ rubygems_version: 2.7.7
252
277
  signing_key:
253
278
  specification_version: 4
254
279
  summary: Sample Core Api