blsk_ruby_core_api 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/src/application.rb +47 -0
- data/src/application_configuration.rb +42 -0
- data/src/application_service.rb +10 -0
- data/src/application_status.rb +0 -0
- data/src/bluesky_service_core.rb +13 -0
- data/src/cassandra/cassandra_configuration.rb +5 -0
- data/{lib/cassandra/abstracted_cassandra_repository.rb → src/cassandra/cassandra_dao.rb} +1 -1
- data/src/cassandra/cassandra_service.rb +30 -0
- data/src/cassandra/index.rb +3 -0
- data/src/config.yml +27 -0
- data/src/consul/consul_configuration.rb +15 -0
- data/src/consul/consul_rest.rb +14 -0
- data/src/consul/consul_service.rb +41 -0
- data/src/consul/index.rb +3 -0
- data/{lib → src}/ioc/container.rb +0 -0
- data/src/ioc/index.rb +2 -0
- data/{lib → src}/ioc/injector.rb +26 -1
- data/src/models/index.rb +1 -0
- data/{lib/model → src/models}/model.rb +0 -0
- data/{lib/rest/rest_common_error.rb → src/rest/errors/common_error.rb} +5 -1
- data/src/rest/index.rb +5 -0
- data/src/rest/models/response_model.rb +35 -0
- data/src/rest/rest_base.rb +57 -0
- data/src/rest/rest_configuration.rb +4 -0
- data/src/rest/rest_service.rb +11 -0
- data/{lib/decoractors → src/security/decorators}/authenticate.rb +4 -5
- data/src/security/index.rb +2 -0
- data/{lib → src}/security/security_context_holder.rb +0 -0
- data/src/validation/decorators/valid.rb +34 -0
- data/src/validation/index.rb +1 -0
- metadata +36 -25
- data/lib/app_service.rb +0 -20
- data/lib/application_configuration.rb +0 -56
- data/lib/blsk_ruby_core_api.rb +0 -41
- data/lib/cassandra/cassandra_configuration.rb +0 -10
- data/lib/cassandra/cassandra_service.rb +0 -35
- data/lib/consul/consul_configuration.rb +0 -39
- data/lib/consul/consul_service.rb +0 -33
- data/lib/decoractors/valid.rb +0 -35
- data/lib/rest/rest_base.rb +0 -73
- data/lib/rest/rest_configuration.rb +0 -10
- data/lib/rest/rest_error_handler.rb +0 -33
- data/lib/rest/rest_response_model.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0fe7e0229df8e0580b3039705f41eb4af7d2b2d6a871d145b5625f49e170230c
|
4
|
+
data.tar.gz: e6d1b9449833e454492a84336b79cb521d0d356ee5daa0b4e02e175536535c2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 125a0ad82c6bfaa33495d1e43fa35606e1fec7a76139ce24ee5e04414ca2277f1adeee332f928e82e563028be232fb2576bf8db99b25920ec8e7dfb105674206
|
7
|
+
data.tar.gz: 2072564a283c4a484e7940c1866e3bdf2320e30b4aedde1ab7223da23c5aed503410fe5f849d6f74df4f95d44d8d068644b5408e95f95f8c8fdefc1710e9f895
|
data/src/application.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require_relative 'application_configuration'
|
3
|
+
require_relative 'application_service'
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
class Application
|
8
|
+
DEFAULT_SERVICES = [ConsulService, CassandraService]
|
9
|
+
|
10
|
+
def initialize(config_path='', rests=[], services=[])
|
11
|
+
@config = get_config(config_path)
|
12
|
+
@rests = rests
|
13
|
+
@services = services
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_config(config_path='')
|
17
|
+
file = File.expand_path( config_path )
|
18
|
+
unless File.file? file
|
19
|
+
file = File.expand_path( File.join File.dirname(__FILE__), "./config.yml" )
|
20
|
+
end
|
21
|
+
|
22
|
+
YAML.load_file(file)
|
23
|
+
end
|
24
|
+
|
25
|
+
def start
|
26
|
+
puts 'starting application'
|
27
|
+
Container.register('application_configuration', ApplicationConfiguration.new(@config))
|
28
|
+
application_service = DEFAULT_SERVICES + @services
|
29
|
+
application_service.each do |service|
|
30
|
+
service_name = service.name[0, 1].downcase + service.name[1..-1]
|
31
|
+
Container.register(Inflector.underscore(service_name), service.extend(Injector).new.start)
|
32
|
+
end
|
33
|
+
start_rest_service
|
34
|
+
end
|
35
|
+
|
36
|
+
def start_rest_service
|
37
|
+
rests = @rests
|
38
|
+
Container.register('rest', RestService.start(rests))
|
39
|
+
return Container['rest']
|
40
|
+
end
|
41
|
+
|
42
|
+
def stop
|
43
|
+
consul_service.stop
|
44
|
+
cassandra_service.stop
|
45
|
+
# Container['application_service'].stop
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require_relative 'cassandra/cassandra_configuration'
|
2
|
+
require_relative 'consul/consul_configuration'
|
3
|
+
require_relative 'rest/rest_configuration'
|
4
|
+
|
5
|
+
class LogConfiguration < Model
|
6
|
+
attribute :host, String.default('localhost')
|
7
|
+
attribute :port, Integer.default(24224)
|
8
|
+
end
|
9
|
+
|
10
|
+
class ApplicationConfiguration < Model
|
11
|
+
attribute :name, String.default('bluesky-seed-service')
|
12
|
+
attribute :host, String.default('127.0.0.1')
|
13
|
+
attribute :tags, Array.default(['seed'])
|
14
|
+
attribute :cassandra, CassandraConfiguration
|
15
|
+
attribute :consul, ConsulConfiguration
|
16
|
+
attribute :rest, RestConfiguration
|
17
|
+
attribute :log, LogConfiguration
|
18
|
+
|
19
|
+
def cassandra_config
|
20
|
+
{
|
21
|
+
hosts: cassandra.hosts,
|
22
|
+
port: cassandra.port
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
def consul_service_config
|
27
|
+
{
|
28
|
+
name: name,
|
29
|
+
id: name,
|
30
|
+
port: consul.port,
|
31
|
+
check: {
|
32
|
+
id: name,
|
33
|
+
name: name,
|
34
|
+
http: "http://#{host}:#{rest.port}#{consul.service.health_check_path}",
|
35
|
+
tls_skip_verify: false,
|
36
|
+
method: "GET",
|
37
|
+
interval: consul.service.health_check_interval,
|
38
|
+
timeout: "1s"
|
39
|
+
}
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
File without changes
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'sinatra/base'
|
3
|
+
require 'request_store'
|
4
|
+
|
5
|
+
require_relative 'ioc/index'
|
6
|
+
require_relative 'models/index'
|
7
|
+
require_relative 'application'
|
8
|
+
require_relative 'rest/index'
|
9
|
+
require_relative 'cassandra/index'
|
10
|
+
require_relative 'consul/index'
|
11
|
+
require_relative 'security/index'
|
12
|
+
require_relative 'validation/index'
|
13
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'cassandra'
|
2
|
+
require 'query_builder'
|
3
|
+
require_relative 'cassandra_dao'
|
4
|
+
|
5
|
+
class CassandraService
|
6
|
+
include CassandraDao
|
7
|
+
|
8
|
+
attr_accessor :cassandra_cluster
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
# puts 'init cassandra'
|
12
|
+
self.cassandra_cluster = Cassandra.cluster(Container['application_configuration'].cassandra_config)
|
13
|
+
end
|
14
|
+
|
15
|
+
def start
|
16
|
+
# puts 'start cassandra, create cluster'
|
17
|
+
if !cassandra_cluster
|
18
|
+
cassandra_cluster = Cassandra.cluster(Container['application_configuration'].cassandra_config)
|
19
|
+
end
|
20
|
+
return self
|
21
|
+
end
|
22
|
+
|
23
|
+
def stop
|
24
|
+
# puts 'stop cassandra'
|
25
|
+
if cassandra_cluster
|
26
|
+
# puts 'closing'
|
27
|
+
cassandra_cluster.close
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/src/config.yml
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
name: "bluesky-seed-service"
|
2
|
+
host: "127.0.0.1"
|
3
|
+
tags:
|
4
|
+
- "seed"
|
5
|
+
|
6
|
+
rest:
|
7
|
+
port: 8080
|
8
|
+
context: '/api'
|
9
|
+
|
10
|
+
consul:
|
11
|
+
host: "localhost" #'172.16.15.3'
|
12
|
+
port: 8500
|
13
|
+
service:
|
14
|
+
health_check_path: "/health_check"
|
15
|
+
health_check_interval: 10s
|
16
|
+
client:
|
17
|
+
service_watch_interval: 10s
|
18
|
+
|
19
|
+
cassandra:
|
20
|
+
hosts:
|
21
|
+
- "172.16.15.3"
|
22
|
+
port: 9042
|
23
|
+
keyspace: "ltran_test"
|
24
|
+
|
25
|
+
log:
|
26
|
+
host: "localhost"
|
27
|
+
port: 24224
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class ConsulClientConfig < Model
|
2
|
+
attribute :service_watch_interval, String.default("10s")
|
3
|
+
end
|
4
|
+
|
5
|
+
class ConsulServiceConfig < Model
|
6
|
+
attribute :health_check_path, String.default("/health-check")
|
7
|
+
attribute :health_check_interval, String.default("10s")
|
8
|
+
end
|
9
|
+
|
10
|
+
class ConsulConfiguration < Model
|
11
|
+
attribute :host, String.default("localhost")
|
12
|
+
attribute :port, Integer.default(8500)
|
13
|
+
attribute :service, ConsulServiceConfig
|
14
|
+
attribute :client, ConsulClientConfig
|
15
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require_relative '../rest/rest_base'
|
2
|
+
|
3
|
+
class ConsulRest < RestBase
|
4
|
+
# Health Check for Consul, run when start Consul
|
5
|
+
def self.start_consul_check_http(url)
|
6
|
+
get url do
|
7
|
+
'The service is healthy!'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
#Register the routes here
|
11
|
+
def self.init_routes
|
12
|
+
return self
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'diplomat'
|
2
|
+
require_relative 'consul_configuration'
|
3
|
+
require_relative 'consul_rest'
|
4
|
+
|
5
|
+
class ConsulService
|
6
|
+
attr_accessor :consul_service_data
|
7
|
+
|
8
|
+
def initialize(option=nil)
|
9
|
+
# puts 'init consul'
|
10
|
+
self.consul_service_data = Container['application_configuration'].consul_service_config
|
11
|
+
end
|
12
|
+
|
13
|
+
def start
|
14
|
+
# puts 'start consul'
|
15
|
+
consul_configuration = Container['application_configuration'].consul
|
16
|
+
consul_service_data = Container['application_configuration'].consul_service_config if consul_service_data.nil?
|
17
|
+
|
18
|
+
Diplomat.configure do |config|
|
19
|
+
# Set up a custom Consul URL
|
20
|
+
config.url = "http://#{consul_configuration.host}:#{consul_configuration.port.to_s}"
|
21
|
+
# Set up a custom Faraday Middleware
|
22
|
+
# config.middleware = MyCustomMiddleware
|
23
|
+
# Connect into consul with custom access token (ACL)
|
24
|
+
# config.acl_token = "qwertyui-asdf-zxcv-1234-123456789012"
|
25
|
+
# Set extra Faraday configuration options
|
26
|
+
# config.options = {ssl: { version: :TLSv1_2 }}
|
27
|
+
end
|
28
|
+
Diplomat::Service.register(consul_service_data)
|
29
|
+
|
30
|
+
# start url for consul to check health
|
31
|
+
consul_check_route = consul_configuration.service.health_check_path
|
32
|
+
ConsulRest.start_consul_check_http(consul_check_route)
|
33
|
+
return self
|
34
|
+
end
|
35
|
+
|
36
|
+
def stop
|
37
|
+
# puts 'stop consul'
|
38
|
+
consul_service_id = Container['application_configuration'].name
|
39
|
+
Diplomat::Service.deregister(consul_service_id)
|
40
|
+
end
|
41
|
+
end
|
data/src/consul/index.rb
ADDED
File without changes
|
data/src/ioc/index.rb
ADDED
data/{lib → src}/ioc/injector.rb
RENAMED
@@ -17,11 +17,36 @@ module Injector
|
|
17
17
|
|
18
18
|
def decorate name, decorators
|
19
19
|
if @@decorators[name].nil?
|
20
|
-
@@decorators[name] = []
|
20
|
+
@@decorators[name] = {:is_processed => false, :decorators => []}
|
21
21
|
end
|
22
22
|
@@decorators[name] = {:is_processed => false, :decorators => decorators}
|
23
23
|
end
|
24
24
|
|
25
|
+
def validate name, decorators=[]
|
26
|
+
if @@decorators[name].nil?
|
27
|
+
@@decorators[name] = {:is_processed => false, :decorators => []}
|
28
|
+
end
|
29
|
+
@@decorators[name][:is_processed] = false
|
30
|
+
@@decorators[name][:decorators] << Validator.validate_model
|
31
|
+
if decorators.any?
|
32
|
+
decorators.each do |decorator|
|
33
|
+
@@decorators[name][:decorators] << decorator
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def authorize name, decorators=[]
|
39
|
+
if @@decorators[name].nil?
|
40
|
+
@@decorators[name] = {:is_processed => false, :decorators => []}
|
41
|
+
end
|
42
|
+
@@decorators[name][:is_processed] = false
|
43
|
+
if decorators.any?
|
44
|
+
decorators.each do |decorator|
|
45
|
+
@@decorators[name][:decorators] << decorator
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
25
50
|
def method_added(name)
|
26
51
|
super
|
27
52
|
orig_method = instance_method(name)
|
data/src/models/index.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'model'
|
File without changes
|
@@ -1,25 +1,29 @@
|
|
1
1
|
module CommonError
|
2
2
|
class InvalidToken < Exception
|
3
|
+
# Status Code 401
|
3
4
|
def initialize(msg = 'InvalidToken')
|
4
5
|
super
|
5
6
|
end
|
6
7
|
end
|
7
8
|
|
8
9
|
class PermissionDenied < Exception
|
10
|
+
# Status Code 403
|
9
11
|
def initialize(msg = 'PermissionDenied')
|
10
12
|
super
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
14
16
|
class EntityNotFound < Exception
|
17
|
+
# Status Code 404
|
15
18
|
def initialize(msg = 'EntityNotFound')
|
16
19
|
super
|
17
20
|
end
|
18
21
|
end
|
19
22
|
|
20
23
|
class InvalidData < Exception
|
24
|
+
# Status Code 422
|
21
25
|
def initialize(msg = 'InvalidData')
|
22
26
|
super
|
23
27
|
end
|
24
28
|
end
|
25
|
-
end
|
29
|
+
end
|
data/src/rest/index.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
class ResponseModel < Model
|
2
|
+
attribute :status, String.optional
|
3
|
+
attribute :status_code, Integer.optional
|
4
|
+
|
5
|
+
def self.ok
|
6
|
+
response_model = ResponseModel.new({status: 'OK', status_code: 200})
|
7
|
+
response_model
|
8
|
+
end
|
9
|
+
|
10
|
+
def response(data=nil)
|
11
|
+
attributes[:data] = data unless data.nil?
|
12
|
+
attributes
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.failed(errors=nil)
|
16
|
+
response_model = ResponseModel.new({status: 'FAILED', status_code: 401})
|
17
|
+
response_model.attributes[:errors] = errors if !errors.nil? && errors.any?
|
18
|
+
response_model
|
19
|
+
end
|
20
|
+
|
21
|
+
def status(status_message='OK')
|
22
|
+
self.attributes[:status] = status_message
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def status_code(status_code=200)
|
27
|
+
self.attributes[:status_code] = status_code
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
def raise_error(exception_class)
|
32
|
+
RestBase.init_and_raise_error(exception_class, self.attributes[:status_code], attributes)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require "sinatra/namespace"
|
3
|
+
require_relative 'errors/common_error'
|
4
|
+
require_relative 'models/response_model'
|
5
|
+
|
6
|
+
|
7
|
+
class RestBase < Sinatra::Base
|
8
|
+
register Sinatra::Namespace
|
9
|
+
|
10
|
+
set :server, :puma
|
11
|
+
disable :raise_errors
|
12
|
+
disable :show_exceptions
|
13
|
+
set :dump_errors, true
|
14
|
+
set :show_exceptions, :after_handler
|
15
|
+
|
16
|
+
before '/*' do
|
17
|
+
# puts 'before routes - set local thread'
|
18
|
+
RequestStore.store[:user] = "Nhuan Lai" #get the user by access_token
|
19
|
+
if request.env['CONTENT_TYPE'] == 'application/json'
|
20
|
+
body_content = JSON.load(request.body)
|
21
|
+
request.params[:body] = JSON.parse( body_content ) unless body_content.nil?
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
after '/*' do
|
26
|
+
response.body = response.body.to_json
|
27
|
+
end
|
28
|
+
|
29
|
+
# for handling error
|
30
|
+
def self.init_and_raise_error(exception_class, status_code, response_model)
|
31
|
+
error exception_class do
|
32
|
+
status status_code
|
33
|
+
response_model
|
34
|
+
end
|
35
|
+
raise exception_class
|
36
|
+
end
|
37
|
+
|
38
|
+
# register the routes here
|
39
|
+
def self.init_routes
|
40
|
+
# # Example API route
|
41
|
+
# namespace Container["application_configuration"].rest.context do
|
42
|
+
# get '/seed_1' do
|
43
|
+
# 'Seed 1 API'
|
44
|
+
# end
|
45
|
+
# end
|
46
|
+
return self
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# # Example for handling response
|
51
|
+
# get '/health' do
|
52
|
+
# # success case
|
53
|
+
# ResponseModel.ok.response
|
54
|
+
# # error case
|
55
|
+
# errors = ['errors_block']
|
56
|
+
# ResponseModel.failed(errors).status_code(422).raise_error(CommonError::InvalidData)
|
57
|
+
# end
|
@@ -1,8 +1,7 @@
|
|
1
|
-
require_relative '../
|
2
|
-
require_relative '../security/security_context_holder'
|
1
|
+
require_relative '../security_context_holder'
|
3
2
|
|
4
|
-
|
5
|
-
def has_roles roles
|
3
|
+
class Authenticator
|
4
|
+
def self.has_roles roles
|
6
5
|
lambda {|*args, &blk|
|
7
6
|
# puts 'has_roles'
|
8
7
|
check_role = true # check role here and return true/false
|
@@ -14,7 +13,7 @@ module Authenticator
|
|
14
13
|
}
|
15
14
|
end
|
16
15
|
|
17
|
-
def authenticate isAuthorized
|
16
|
+
def self.authenticate isAuthorized
|
18
17
|
lambda {|self_model, *args, &blk|
|
19
18
|
# puts "isAuthorized"
|
20
19
|
# check authentication and authorization here, then return true/false
|
File without changes
|
@@ -0,0 +1,34 @@
|
|
1
|
+
class Validator
|
2
|
+
def self.validate_model params={}
|
3
|
+
lambda {|self_model, *args, &blk|
|
4
|
+
# puts "Validate"
|
5
|
+
seed_model = args[0]
|
6
|
+
valid_result = check_validate(seed_model) # check validation and return [true]/[false, errors]
|
7
|
+
if valid_result[0]
|
8
|
+
true
|
9
|
+
else
|
10
|
+
ResponseModel
|
11
|
+
.new({status: 'FAILED', status_code: 422})
|
12
|
+
.error(CommonError::InvalidData, valid_result[1])
|
13
|
+
end
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.check_validate(seed_model)
|
18
|
+
errors = seed_model.validate
|
19
|
+
if errors.empty?
|
20
|
+
[true]
|
21
|
+
else
|
22
|
+
errors_response = convert_errors_response(errors)
|
23
|
+
[false, errors_response]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.convert_errors_response(error_list)
|
28
|
+
errors = []
|
29
|
+
error_list.each do |key, value|
|
30
|
+
errors << {field: key, message: value}
|
31
|
+
end
|
32
|
+
errors
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'decorators/valid'
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: blsk_ruby_core_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- blsk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
@@ -250,31 +250,42 @@ dependencies:
|
|
250
250
|
- - ">="
|
251
251
|
- !ruby/object:Gem::Version
|
252
252
|
version: 3.7.0
|
253
|
-
description:
|
254
|
-
email:
|
253
|
+
description: Code Skeleton for blsk Service Core
|
254
|
+
email: support@blsk.com
|
255
255
|
executables: []
|
256
256
|
extensions: []
|
257
257
|
extra_rdoc_files: []
|
258
258
|
files:
|
259
|
-
-
|
260
|
-
-
|
261
|
-
-
|
262
|
-
-
|
263
|
-
-
|
264
|
-
-
|
265
|
-
-
|
266
|
-
-
|
267
|
-
-
|
268
|
-
-
|
269
|
-
-
|
270
|
-
-
|
271
|
-
-
|
272
|
-
-
|
273
|
-
-
|
274
|
-
-
|
275
|
-
-
|
276
|
-
-
|
277
|
-
-
|
259
|
+
- src/application.rb
|
260
|
+
- src/application_configuration.rb
|
261
|
+
- src/application_service.rb
|
262
|
+
- src/application_status.rb
|
263
|
+
- src/bluesky_service_core.rb
|
264
|
+
- src/cassandra/cassandra_configuration.rb
|
265
|
+
- src/cassandra/cassandra_dao.rb
|
266
|
+
- src/cassandra/cassandra_service.rb
|
267
|
+
- src/cassandra/index.rb
|
268
|
+
- src/config.yml
|
269
|
+
- src/consul/consul_configuration.rb
|
270
|
+
- src/consul/consul_rest.rb
|
271
|
+
- src/consul/consul_service.rb
|
272
|
+
- src/consul/index.rb
|
273
|
+
- src/ioc/container.rb
|
274
|
+
- src/ioc/index.rb
|
275
|
+
- src/ioc/injector.rb
|
276
|
+
- src/models/index.rb
|
277
|
+
- src/models/model.rb
|
278
|
+
- src/rest/errors/common_error.rb
|
279
|
+
- src/rest/index.rb
|
280
|
+
- src/rest/models/response_model.rb
|
281
|
+
- src/rest/rest_base.rb
|
282
|
+
- src/rest/rest_configuration.rb
|
283
|
+
- src/rest/rest_service.rb
|
284
|
+
- src/security/decorators/authenticate.rb
|
285
|
+
- src/security/index.rb
|
286
|
+
- src/security/security_context_holder.rb
|
287
|
+
- src/validation/decorators/valid.rb
|
288
|
+
- src/validation/index.rb
|
278
289
|
homepage: http://rubygems.org/gems/blsk_ruby_core_api
|
279
290
|
licenses:
|
280
291
|
- MIT
|
@@ -282,7 +293,7 @@ metadata: {}
|
|
282
293
|
post_install_message:
|
283
294
|
rdoc_options: []
|
284
295
|
require_paths:
|
285
|
-
-
|
296
|
+
- src
|
286
297
|
required_ruby_version: !ruby/object:Gem::Requirement
|
287
298
|
requirements:
|
288
299
|
- - ">="
|
@@ -298,5 +309,5 @@ rubyforge_project:
|
|
298
309
|
rubygems_version: 2.7.7
|
299
310
|
signing_key:
|
300
311
|
specification_version: 4
|
301
|
-
summary:
|
312
|
+
summary: blsk Service Core
|
302
313
|
test_files: []
|
data/lib/app_service.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
require_relative 'consul/consul_service'
|
2
|
-
require_relative 'cassandra/cassandra_service'
|
3
|
-
|
4
|
-
class AppService
|
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
|
@@ -1,56 +0,0 @@
|
|
1
|
-
require 'securerandom'
|
2
|
-
|
3
|
-
module ApplicationConfiguration
|
4
|
-
def config
|
5
|
-
{
|
6
|
-
rest: config_rest,
|
7
|
-
consul: config_consul,
|
8
|
-
cassandra: config_cassandra
|
9
|
-
}
|
10
|
-
end
|
11
|
-
|
12
|
-
def config_consul
|
13
|
-
config_consul = {
|
14
|
-
port: 8500,
|
15
|
-
path: "http://localhost",
|
16
|
-
consul_service_data: {
|
17
|
-
name: "Service-1",
|
18
|
-
id: "Service1",
|
19
|
-
port: 9292,
|
20
|
-
check: {
|
21
|
-
id: "bluesky-api",
|
22
|
-
name: "HTTP Health Check API on port 9292",
|
23
|
-
http: "http://localhost:9292/health-check-new",
|
24
|
-
tls_skip_verify: false,
|
25
|
-
method: "GET",
|
26
|
-
interval: "10s",
|
27
|
-
timeout: "1s"
|
28
|
-
}
|
29
|
-
}
|
30
|
-
}
|
31
|
-
config_consul[:consul_service_data][:port] = config_rest[:port]
|
32
|
-
config_consul[:consul_service_data][:check][:http] = "http://#{config_rest[:host]}:#{config_rest[:port].to_s}/#{random_name}"
|
33
|
-
config_consul
|
34
|
-
end
|
35
|
-
|
36
|
-
def config_rest
|
37
|
-
{
|
38
|
-
host: 'localhost',
|
39
|
-
port: 9292,
|
40
|
-
path: '/api/v1'
|
41
|
-
}
|
42
|
-
end
|
43
|
-
|
44
|
-
def config_cassandra
|
45
|
-
{
|
46
|
-
# user_name: '', # turn on if any
|
47
|
-
# password: '', # turn on if any
|
48
|
-
hosts: ['172.16.15.3'],
|
49
|
-
port: 9042
|
50
|
-
}
|
51
|
-
end
|
52
|
-
|
53
|
-
def random_name
|
54
|
-
"health-check-#{SecureRandom.urlsafe_base64(8)}"
|
55
|
-
end
|
56
|
-
end
|
data/lib/blsk_ruby_core_api.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
require 'bundler/setup'
|
2
|
-
require 'sinatra/base'
|
3
|
-
require 'request_store'
|
4
|
-
|
5
|
-
require_relative 'application_configuration'
|
6
|
-
require_relative 'app_service'
|
7
|
-
require_relative 'rest/rest_base'
|
8
|
-
require_relative 'decoractors/authenticate'
|
9
|
-
require_relative 'decoractors/valid'
|
10
|
-
require_relative 'model/model'
|
11
|
-
# Dir["#{File.dirname(__FILE__)}/**/*.rb"].each {|file| require file }
|
12
|
-
|
13
|
-
|
14
|
-
class Application
|
15
|
-
extend ApplicationConfiguration
|
16
|
-
|
17
|
-
def initialize(rests=[], config={})
|
18
|
-
@config = config
|
19
|
-
@rests = rests
|
20
|
-
end
|
21
|
-
|
22
|
-
def init_rest_service
|
23
|
-
rests = @rests
|
24
|
-
return Sinatra.new {
|
25
|
-
rests.each {|a| use a}
|
26
|
-
not_found do
|
27
|
-
JSON RestBase.error_response('EntityNotFound')
|
28
|
-
end
|
29
|
-
}
|
30
|
-
end
|
31
|
-
|
32
|
-
def start
|
33
|
-
puts 'start'
|
34
|
-
Container['app_service'].start
|
35
|
-
init_rest_service
|
36
|
-
end
|
37
|
-
|
38
|
-
def stop
|
39
|
-
Container['app_service'].stop
|
40
|
-
end
|
41
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
require 'cassandra'
|
2
|
-
require 'query_builder'
|
3
|
-
require_relative '../ioc/injector'
|
4
|
-
require_relative 'cassandra_configuration'
|
5
|
-
require_relative 'abstracted_cassandra_repository'
|
6
|
-
|
7
|
-
class CassandraService
|
8
|
-
extend CassandraConfiguration
|
9
|
-
include AbstractedCassandraRepository
|
10
|
-
|
11
|
-
injector
|
12
|
-
|
13
|
-
attr_accessor :cassandra_cluster
|
14
|
-
|
15
|
-
def initialize
|
16
|
-
# puts 'init cassandra'
|
17
|
-
self.cassandra_cluster = Cassandra.cluster(CassandraConfiguration::CONFIG)
|
18
|
-
end
|
19
|
-
|
20
|
-
def start
|
21
|
-
# puts 'start cassandra, create cluster'
|
22
|
-
if !cassandra_cluster
|
23
|
-
cassandra_cluster = Cassandra.cluster(CassandraConfiguration::CONFIG)
|
24
|
-
end
|
25
|
-
cassandra_cluster
|
26
|
-
end
|
27
|
-
|
28
|
-
def stop
|
29
|
-
# puts 'stop cassandra'
|
30
|
-
if cassandra_cluster
|
31
|
-
# puts 'closing'
|
32
|
-
cassandra_cluster.close
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,39 +0,0 @@
|
|
1
|
-
require 'diplomat'
|
2
|
-
|
3
|
-
module ConsulConfiguration
|
4
|
-
extend ApplicationConfiguration
|
5
|
-
|
6
|
-
CONFIG = {
|
7
|
-
port: 8500,
|
8
|
-
path: "http://localhost",
|
9
|
-
consul_service_data: {
|
10
|
-
name: "Service-1",
|
11
|
-
id: "Service1",
|
12
|
-
port: 9292,
|
13
|
-
check: {
|
14
|
-
id: "bluesky-api",
|
15
|
-
name: "HTTP Health Check API on port 9292",
|
16
|
-
http: "http://localhost:9292/health-check",
|
17
|
-
tls_skip_verify: false,
|
18
|
-
method: "GET",
|
19
|
-
interval: "10s",
|
20
|
-
timeout: "1s"
|
21
|
-
}
|
22
|
-
}
|
23
|
-
}.merge( ConsulConfiguration.config_consul )
|
24
|
-
|
25
|
-
def configure
|
26
|
-
Diplomat.configure do |config|
|
27
|
-
# Set up a custom Consul URL
|
28
|
-
config.url = "#{CONFIG[:path]}:#{CONFIG[:port].to_s}"
|
29
|
-
# Set up a custom Faraday Middleware
|
30
|
-
# config.middleware = MyCustomMiddleware
|
31
|
-
# Connect into consul with custom access token (ACL)
|
32
|
-
# config.acl_token = "qwertyui-asdf-zxcv-1234-123456789012"
|
33
|
-
# Set extra Faraday configuration options
|
34
|
-
# config.options = {ssl: { version: :TLSv1_2 }}
|
35
|
-
end
|
36
|
-
|
37
|
-
CONFIG[:consul_service_data]
|
38
|
-
end
|
39
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'diplomat'
|
2
|
-
require_relative '../ioc/injector'
|
3
|
-
require_relative 'consul_configuration'
|
4
|
-
require_relative '../rest/rest_base'
|
5
|
-
|
6
|
-
class ConsulService
|
7
|
-
extend ConsulConfiguration
|
8
|
-
|
9
|
-
injector
|
10
|
-
|
11
|
-
attr_accessor :consul_service_data
|
12
|
-
|
13
|
-
def initialize(option=nil)
|
14
|
-
# puts 'init consul'
|
15
|
-
self.consul_service_data = ConsulService.configure
|
16
|
-
end
|
17
|
-
|
18
|
-
def start
|
19
|
-
# puts 'start consul'
|
20
|
-
consul_service_data = ConsulConfiguration::CONFIG[:consul_service_data] if consul_service_data.nil?
|
21
|
-
Diplomat::Service.register(consul_service_data)
|
22
|
-
|
23
|
-
# start url for consul to check health
|
24
|
-
consul_check_route = "/" + consul_service_data[:check][:http].split('/').last
|
25
|
-
RestBase.start_consul_check_http(consul_check_route)
|
26
|
-
end
|
27
|
-
|
28
|
-
def stop
|
29
|
-
# puts 'stop consul'
|
30
|
-
consul_service_id = self.consul_service_data.try(:id) || ConsulConfiguration::CONFIG[:consul_service_data][:id]
|
31
|
-
Diplomat::Service.deregister(consul_service_id)
|
32
|
-
end
|
33
|
-
end
|
data/lib/decoractors/valid.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
require_relative '../rest/rest_base'
|
2
|
-
|
3
|
-
module Validator
|
4
|
-
def validate params={}
|
5
|
-
lambda {|self_model, *args, &blk|
|
6
|
-
# puts "Validate"
|
7
|
-
seed_model = args[0]
|
8
|
-
self_rest = args [1]
|
9
|
-
valid_result = check_validate(seed_model, self_rest) # check validation and return [true]/[false, errors]
|
10
|
-
if valid_result[0]
|
11
|
-
true
|
12
|
-
else
|
13
|
-
valid_result[2].response_with_error(CommonError::InvalidData, valid_result[1])
|
14
|
-
end
|
15
|
-
}
|
16
|
-
end
|
17
|
-
|
18
|
-
def check_validate(seed_model, self_rest)
|
19
|
-
errors = seed_model.validate
|
20
|
-
if errors.empty?
|
21
|
-
[true]
|
22
|
-
else
|
23
|
-
errors_response = convert_errors_response(errors)
|
24
|
-
[false, errors_response, self_rest]
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def convert_errors_response(error_list)
|
29
|
-
errors = []
|
30
|
-
error_list.each do |key, value|
|
31
|
-
errors << {field: key, message: value}
|
32
|
-
end
|
33
|
-
errors
|
34
|
-
end
|
35
|
-
end
|
data/lib/rest/rest_base.rb
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
require 'sinatra/base'
|
2
|
-
require "sinatra/namespace"
|
3
|
-
require_relative 'rest_configuration'
|
4
|
-
require_relative 'rest_error_handler'
|
5
|
-
require_relative 'rest_common_error'
|
6
|
-
require_relative 'rest_response_model'
|
7
|
-
|
8
|
-
|
9
|
-
class RestApplication < Sinatra::Base
|
10
|
-
set :server, :puma
|
11
|
-
disable :raise_errors
|
12
|
-
disable :show_exceptions
|
13
|
-
|
14
|
-
set :dump_errors, true
|
15
|
-
set :show_exceptions, :after_handler
|
16
|
-
|
17
|
-
register Sinatra::Namespace
|
18
|
-
|
19
|
-
configure :production do
|
20
|
-
# ...
|
21
|
-
end
|
22
|
-
|
23
|
-
configure :development do
|
24
|
-
# ...
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
class RestBase < RestApplication
|
29
|
-
extend ::RestConfiguration
|
30
|
-
extend ::RestResponseModel
|
31
|
-
|
32
|
-
before '/*' do
|
33
|
-
# puts 'before routes - set local thread'
|
34
|
-
RequestStore.store[:user] = "Nhuan Lai" #get the user by access_token
|
35
|
-
|
36
|
-
if request.env['CONTENT_TYPE'] == 'application/json'
|
37
|
-
body_content = JSON.load(request.body)
|
38
|
-
request.params[:body] = JSON.parse( body_content ) unless body_content.nil?
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
after '/*' do
|
43
|
-
response.body = response.body.to_json
|
44
|
-
end
|
45
|
-
|
46
|
-
# Health Check for Consul, run when start Consul
|
47
|
-
def self.start_consul_check_http(url)
|
48
|
-
get url do
|
49
|
-
'The service is healthy!'
|
50
|
-
end
|
51
|
-
end
|
52
|
-
# # API route
|
53
|
-
# namespace RestConfiguration::CONFIG[:path] do
|
54
|
-
# get '/seed_1' do
|
55
|
-
# 'Seed 1 API'
|
56
|
-
# end
|
57
|
-
# get '/seed_2' do
|
58
|
-
# 'Seed 2 API'
|
59
|
-
# end
|
60
|
-
# end
|
61
|
-
|
62
|
-
# # Example for handling error
|
63
|
-
# get '/health' do
|
64
|
-
# errors = [
|
65
|
-
# {
|
66
|
-
# field: 'error field',
|
67
|
-
# code: 'error code',
|
68
|
-
# message: 'error message'
|
69
|
-
# }
|
70
|
-
# ]
|
71
|
-
# response_with_error(CommonError::InvalidData, errors)
|
72
|
-
# end
|
73
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'sinatra/base'
|
2
|
-
require_relative 'rest_common_error'
|
3
|
-
require_relative 'rest_response_model'
|
4
|
-
|
5
|
-
|
6
|
-
class RestApplication < Sinatra::Base
|
7
|
-
extend ::RestResponseModel
|
8
|
-
|
9
|
-
error CommonError::InvalidToken do
|
10
|
-
status 401
|
11
|
-
RestApplication.error_response('InvalidToken')
|
12
|
-
end
|
13
|
-
|
14
|
-
error CommonError::PermissionDenied do
|
15
|
-
status 403
|
16
|
-
RestApplication.error_response('PermissionDenied')
|
17
|
-
end
|
18
|
-
|
19
|
-
error CommonError::EntityNotFound do
|
20
|
-
status 404
|
21
|
-
RestApplication.error_response('EntityNotFound')
|
22
|
-
end
|
23
|
-
|
24
|
-
error CommonError::InvalidData do
|
25
|
-
status 422
|
26
|
-
RestApplication.error_response('InvalidData', @errors)
|
27
|
-
end
|
28
|
-
|
29
|
-
def response_with_error(exception_class, errors=[])
|
30
|
-
@errors = errors if !errors.empty?
|
31
|
-
raise exception_class
|
32
|
-
end
|
33
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
module RestResponseModel
|
2
|
-
def success_response(data=nil)
|
3
|
-
response = {
|
4
|
-
status: 'OK'
|
5
|
-
}
|
6
|
-
response[:data] = data if data
|
7
|
-
response
|
8
|
-
end
|
9
|
-
|
10
|
-
def error_response(error_code='FAILED', errors=[])
|
11
|
-
response = {
|
12
|
-
status: error_code
|
13
|
-
}
|
14
|
-
response[:errors] = errors if !errors.nil? && errors.any?
|
15
|
-
response
|
16
|
-
end
|
17
|
-
end
|