blsk_service_core 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/src/application.rb +38 -0
- data/src/application_configuration.rb +17 -0
- data/src/application_service.rb +18 -0
- data/src/application_status.rb +0 -0
- data/src/bluesky_service_core.rb +13 -0
- data/src/cassandra/cassandra_configuration.rb +11 -0
- data/src/cassandra/cassandra_dao.rb +59 -0
- data/src/cassandra/cassandra_service.rb +33 -0
- data/src/config.yml +25 -0
- data/src/consul/consul_configuration.rb +53 -0
- data/src/consul/consul_service.rb +45 -0
- data/src/ioc/container.rb +5 -0
- data/src/ioc/injector.rb +54 -0
- data/src/models/model.rb +28 -0
- data/src/rest/errors/common_error.rb +29 -0
- data/src/rest/models/response_model.rb +14 -0
- data/src/rest/rest_application.rb +30 -0
- data/src/rest/rest_base.rb +56 -0
- data/src/rest/rest_configuration.rb +13 -0
- data/src/security/decorators/authenticate.rb +28 -0
- data/src/security/security_context_holder.rb +5 -0
- data/src/validation/decorators/valid.rb +37 -0
- metadata +305 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c5fc96d9680eecb5d8c8ce26a877a455023687e87cb5006a67632c39cc9fc1a0
|
4
|
+
data.tar.gz: 0d37ac8d7b796e085d0b84fa529d16ae9f16877958ed7c032aa9944921bc88c4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 94766c7d4ab17324ee1ed6f1600ef140ead7cf76c2a1b8d201f70d534a909c73abcf06f31adce7e7abd2906528abd65eda9a782e30d815126eefd4005b9ef565
|
7
|
+
data.tar.gz: 7e28e161511f72a40c6904c02fc4580e56fc9dbce48a1a5d44b773df80794b7d7f1a9fbd59402677d36526f8003fe26bebf86bebc65fb2837371fbed5e881524
|
data/src/application.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
class Application
|
2
|
+
def initialize(config_path='', rests=[], services=[])
|
3
|
+
@config = get_config(config_path)
|
4
|
+
@rests = rests
|
5
|
+
end
|
6
|
+
|
7
|
+
def init_rest_service
|
8
|
+
rests = @rests
|
9
|
+
return Sinatra.new {
|
10
|
+
rests.each {|a| use a}
|
11
|
+
not_found do
|
12
|
+
ResponseModel
|
13
|
+
.new({status: 'FAILED', status_code: 404})
|
14
|
+
.error(CommonError::EntityNotFound)
|
15
|
+
end
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_config(config_path='')
|
20
|
+
file = File.expand_path( config_path )
|
21
|
+
unless File.file? file
|
22
|
+
file = File.expand_path( File.join File.dirname(__FILE__), "./config.yml" )
|
23
|
+
end
|
24
|
+
|
25
|
+
YAML.load_file(file)
|
26
|
+
end
|
27
|
+
|
28
|
+
def start
|
29
|
+
puts 'start'
|
30
|
+
Container['application_configuration'].new(@config)
|
31
|
+
Container['application_service'].start
|
32
|
+
init_rest_service
|
33
|
+
end
|
34
|
+
|
35
|
+
def stop
|
36
|
+
Container['application_service'].stop
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
require_relative 'cassandra/cassandra_configuration'
|
3
|
+
require_relative 'consul/consul_configuration'
|
4
|
+
require_relative 'rest/rest_configuration'
|
5
|
+
|
6
|
+
class ApplicationConfiguration < Model
|
7
|
+
|
8
|
+
attribute :cassandra, CassandraConfiguration
|
9
|
+
attribute :consul, ConsulConfiguration
|
10
|
+
attribute :rest, RestConfiguration
|
11
|
+
|
12
|
+
injector
|
13
|
+
|
14
|
+
def random_name
|
15
|
+
"health-check-#{SecureRandom.urlsafe_base64(8)}"
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require_relative 'consul/consul_service'
|
2
|
+
require_relative 'cassandra/cassandra_service'
|
3
|
+
|
4
|
+
class ApplicationService
|
5
|
+
include Inject['consul_service', 'cassandra_service']
|
6
|
+
|
7
|
+
injector
|
8
|
+
|
9
|
+
def start
|
10
|
+
consul_service.start
|
11
|
+
cassandra_service.start
|
12
|
+
end
|
13
|
+
|
14
|
+
def stop
|
15
|
+
consul_service.stop
|
16
|
+
cassandra_service.stop
|
17
|
+
end
|
18
|
+
end
|
File without changes
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'sinatra/base'
|
3
|
+
require 'request_store'
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
require_relative 'models/model'
|
7
|
+
require_relative 'ioc/injector'
|
8
|
+
require_relative 'application_configuration'
|
9
|
+
require_relative 'application_service'
|
10
|
+
require_relative 'rest/rest_base'
|
11
|
+
require_relative 'security/decorators/authenticate'
|
12
|
+
require_relative 'validation/decorators/valid'
|
13
|
+
require_relative 'application'
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module CassandraDao
|
2
|
+
def find_by_id(id=nil, keyspace_name=nil, table_name=nil)
|
3
|
+
# puts 'find_by_id'
|
4
|
+
if keyspace_name && table_name
|
5
|
+
session = cassandra_cluster.connect(keyspace_name)
|
6
|
+
result = session.execute("SELECT * FROM #{table_name} WHERE id = ?", arguments: [id])
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def find_all(keyspace_name=nil, table_name=nil)
|
11
|
+
# puts 'find_all'
|
12
|
+
if keyspace_name && table_name
|
13
|
+
session = cassandra_cluster.connect(keyspace_name)
|
14
|
+
result = session.execute("SELECT * FROM #{table_name}")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def create(cql_query='', keyspace_name=nil, table_name=nil)
|
19
|
+
# puts 'create'
|
20
|
+
if keyspace_name && table_name
|
21
|
+
session = cassandra_cluster.connect(keyspace_name)
|
22
|
+
# Building the cql using 'INSERT INTO' with 'IF NOT EXISTS'
|
23
|
+
result = session.execute(cql_query)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def update(id=nil, value_hashes={}, keyspace_name=nil, table_name=nil)
|
28
|
+
# puts 'update'
|
29
|
+
if keyspace_name && table_name
|
30
|
+
session = cassandra_cluster.connect(keyspace_name)
|
31
|
+
# Building the cql using UPDATE
|
32
|
+
result = session.execute(cql_query)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def save(value_hashes={}, keyspace_name=nil, table_name=nil)
|
37
|
+
# puts 'save'
|
38
|
+
if keyspace_name && table_name
|
39
|
+
session = cassandra_cluster.connect(keyspace_name)
|
40
|
+
# Building the cql using 'INSERT INTO' without 'IF NOT EXISTS'
|
41
|
+
result = session.execute(cql_query)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def delete(id=nil, keyspace_name=nil, table_name=nil)
|
46
|
+
# puts 'delete'
|
47
|
+
if keyspace_name && table_name
|
48
|
+
session = cassandra_cluster.connect(keyspace_name)
|
49
|
+
result = session.execute("DELETE FROM #{table_name} WHERE id = ?", arguments: [id])
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def run_query(cql_query='', keyspace_name=nil, table_name=nil)
|
54
|
+
if keyspace_name && table_name
|
55
|
+
session = cassandra_cluster.connect(keyspace_name)
|
56
|
+
result = session.execute(cql_query)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'cassandra'
|
2
|
+
require 'query_builder'
|
3
|
+
require_relative 'cassandra_dao'
|
4
|
+
|
5
|
+
class CassandraService
|
6
|
+
include Inject["application_configuration"]
|
7
|
+
include CassandraDao
|
8
|
+
|
9
|
+
injector
|
10
|
+
|
11
|
+
attr_accessor :cassandra_cluster
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
# puts 'init cassandra'
|
15
|
+
self.cassandra_cluster = Cassandra.cluster(application_configuration.cassandra.attributes)
|
16
|
+
end
|
17
|
+
|
18
|
+
def start
|
19
|
+
# puts 'start cassandra, create cluster'
|
20
|
+
if !cassandra_cluster
|
21
|
+
cassandra_cluster = Cassandra.cluster(application_configuration.cassandra.attributes)
|
22
|
+
end
|
23
|
+
cassandra_cluster
|
24
|
+
end
|
25
|
+
|
26
|
+
def stop
|
27
|
+
# puts 'stop cassandra'
|
28
|
+
if cassandra_cluster
|
29
|
+
# puts 'closing'
|
30
|
+
cassandra_cluster.close
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/src/config.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
cassandra:
|
2
|
+
hosts: []
|
3
|
+
port: 9042
|
4
|
+
consul:
|
5
|
+
port: 8500
|
6
|
+
path: "http://localhost"
|
7
|
+
consul_service_data:
|
8
|
+
name: "Service-1"
|
9
|
+
id: "Service1"
|
10
|
+
port: 8080
|
11
|
+
check:
|
12
|
+
id: "bluesky-api"
|
13
|
+
name: "HTTP Health Check API on port 8080"
|
14
|
+
http: "http://localhost:8080/health-check"
|
15
|
+
tls_skip_verify: false
|
16
|
+
method: "GET"
|
17
|
+
interval: "10s"
|
18
|
+
timeout: "1s"
|
19
|
+
rest:
|
20
|
+
host: 'localhost'
|
21
|
+
port: 8080
|
22
|
+
path: '/api/v1'
|
23
|
+
|
24
|
+
|
25
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
class ConsulServiceDataCheck < Model
|
2
|
+
attribute :id, String.default("bluesky-api")
|
3
|
+
attribute :name, String.default("HTTP Health Check API on port 8080")
|
4
|
+
attribute :http, String.default("http://localhost:8080/health-check")
|
5
|
+
attribute :tls_skip_verify, Bool.default(false)
|
6
|
+
attribute :method, String.default("GET")
|
7
|
+
attribute :interval, String.default("10s")
|
8
|
+
attribute :timeout, String.default("1s")
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
class ConsulServiceData < Model
|
13
|
+
attribute :name, String.default("Service-1")
|
14
|
+
attribute :id, String.default("Service1")
|
15
|
+
attribute :port, Integer.default(8080)
|
16
|
+
attribute :check, ConsulServiceDataCheck
|
17
|
+
|
18
|
+
def get_attributes
|
19
|
+
{
|
20
|
+
name: self.name,
|
21
|
+
id: self.id,
|
22
|
+
port: self.port,
|
23
|
+
check: self.check.attributes
|
24
|
+
}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class ConsulConfiguration < Model
|
29
|
+
attribute :port, Integer.default(8500)
|
30
|
+
attribute :path, String.default("http://localhost")
|
31
|
+
attribute :consul_service_data, ConsulServiceData
|
32
|
+
|
33
|
+
# # Sample
|
34
|
+
# {
|
35
|
+
# port: 8500,
|
36
|
+
# path: "http://localhost",
|
37
|
+
# consul_service_data: {
|
38
|
+
# name: "Service-1",
|
39
|
+
# id: "Service1",
|
40
|
+
# port: 8080,
|
41
|
+
# check: {
|
42
|
+
# id: "bluesky-api",
|
43
|
+
# name: "HTTP Health Check API on port 8080",
|
44
|
+
# http: "http://localhost:8080/health-check",
|
45
|
+
# tls_skip_verify: false,
|
46
|
+
# method: "GET",
|
47
|
+
# interval: "10s",
|
48
|
+
# timeout: "1s"
|
49
|
+
# }
|
50
|
+
# }
|
51
|
+
# }
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'diplomat'
|
2
|
+
require_relative 'consul_configuration'
|
3
|
+
require_relative '../rest/rest_base'
|
4
|
+
|
5
|
+
class ConsulService
|
6
|
+
# extend ConsulConfiguration
|
7
|
+
include Inject["application_configuration"]
|
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 = application_configuration.consul.consul_service_data
|
16
|
+
end
|
17
|
+
|
18
|
+
def start
|
19
|
+
# puts 'start consul'
|
20
|
+
consul_configuration = application_configuration.consul
|
21
|
+
consul_service_data = consul_configuration.consul_service_data if consul_service_data.nil?
|
22
|
+
|
23
|
+
Diplomat.configure do |config|
|
24
|
+
# Set up a custom Consul URL
|
25
|
+
config.url = "#{consul_configuration.path}:#{consul_configuration.port.to_s}"
|
26
|
+
# Set up a custom Faraday Middleware
|
27
|
+
# config.middleware = MyCustomMiddleware
|
28
|
+
# Connect into consul with custom access token (ACL)
|
29
|
+
# config.acl_token = "qwertyui-asdf-zxcv-1234-123456789012"
|
30
|
+
# Set extra Faraday configuration options
|
31
|
+
# config.options = {ssl: { version: :TLSv1_2 }}
|
32
|
+
end
|
33
|
+
Diplomat::Service.register(consul_service_data.get_attributes)
|
34
|
+
|
35
|
+
# start url for consul to check health
|
36
|
+
consul_check_route = "/" + consul_service_data.check.http.split('/').last
|
37
|
+
RestBase.start_consul_check_http(consul_check_route)
|
38
|
+
end
|
39
|
+
|
40
|
+
def stop
|
41
|
+
# puts 'stop consul'
|
42
|
+
consul_service_id = application_configuration.consul.consul_service_data.id
|
43
|
+
Diplomat::Service.deregister(consul_service_id)
|
44
|
+
end
|
45
|
+
end
|
data/src/ioc/injector.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require_relative "container"
|
2
|
+
require "dry/inflector"
|
3
|
+
|
4
|
+
Inflector = Dry::Inflector.new
|
5
|
+
|
6
|
+
def injector name = nil
|
7
|
+
name = name.nil? ? self.name[0, 1].downcase + self.name[1..-1] : name
|
8
|
+
Container.register(Inflector.underscore(name), self.extend(Injector).new)
|
9
|
+
end
|
10
|
+
|
11
|
+
module Injector
|
12
|
+
@@decorators = Hash.new
|
13
|
+
|
14
|
+
def decorators
|
15
|
+
@@decorators
|
16
|
+
end
|
17
|
+
|
18
|
+
def decorate name, decorators
|
19
|
+
if @@decorators[name].nil?
|
20
|
+
@@decorators[name] = []
|
21
|
+
end
|
22
|
+
@@decorators[name] = {:is_processed => false, :decorators => decorators}
|
23
|
+
end
|
24
|
+
|
25
|
+
def method_added(name)
|
26
|
+
super
|
27
|
+
orig_method = instance_method(name)
|
28
|
+
decorators = self.decorators[name]
|
29
|
+
return if decorators.nil? || decorators[:is_processed] == true
|
30
|
+
|
31
|
+
if private_method_defined?(name);
|
32
|
+
visibility = :private
|
33
|
+
elsif protected_method_defined?(name);
|
34
|
+
visibility = :protected
|
35
|
+
else
|
36
|
+
visibility = :public
|
37
|
+
end
|
38
|
+
|
39
|
+
decorators[:is_processed] = true
|
40
|
+
define_method(name) do |*args, &blk|
|
41
|
+
decorators[:decorators].each do |decor|
|
42
|
+
decor.call(self, *args, &blk)
|
43
|
+
end
|
44
|
+
orig_method.bind(self).call(*args, &blk)
|
45
|
+
end
|
46
|
+
|
47
|
+
case visibility
|
48
|
+
when :protected;
|
49
|
+
protected name
|
50
|
+
when :private;
|
51
|
+
private name
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/src/models/model.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'dry-types'
|
2
|
+
require 'dry-struct'
|
3
|
+
require 'dry-validation'
|
4
|
+
|
5
|
+
class Model < Dry::Struct
|
6
|
+
include Dry::Types.module
|
7
|
+
transform_keys(&:to_sym)
|
8
|
+
|
9
|
+
@@validation_schema
|
10
|
+
|
11
|
+
def self.validates &blk
|
12
|
+
@@validation_schema = Dry::Validation.Schema &blk
|
13
|
+
end
|
14
|
+
|
15
|
+
def validate
|
16
|
+
@@validation_schema.call(attributes).messages
|
17
|
+
end
|
18
|
+
|
19
|
+
def to clazz
|
20
|
+
new_attributes = attributes.merge(clazz.different_attributes(attributes, self.class))
|
21
|
+
clazz.new new_attributes
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_json
|
25
|
+
attributes
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module CommonError
|
2
|
+
class InvalidToken < Exception
|
3
|
+
# Status Code 401
|
4
|
+
def initialize(msg = 'InvalidToken')
|
5
|
+
super
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class PermissionDenied < Exception
|
10
|
+
# Status Code 403
|
11
|
+
def initialize(msg = 'PermissionDenied')
|
12
|
+
super
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class EntityNotFound < Exception
|
17
|
+
# Status Code 404
|
18
|
+
def initialize(msg = 'EntityNotFound')
|
19
|
+
super
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class InvalidData < Exception
|
24
|
+
# Status Code 422
|
25
|
+
def initialize(msg = 'InvalidData')
|
26
|
+
super
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class ResponseModel < Model
|
2
|
+
attribute :status, String.optional
|
3
|
+
attribute :status_code, Integer.optional
|
4
|
+
|
5
|
+
def success(data=nil)
|
6
|
+
attributes[:data] = data unless data.nil?
|
7
|
+
attributes
|
8
|
+
end
|
9
|
+
|
10
|
+
def error(exception_class, errors=nil)
|
11
|
+
attributes[:errors] = errors if !errors.nil? && errors.any?
|
12
|
+
RestApplication.init_and_raise_error(exception_class, self.status_code, attributes)
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require "sinatra/namespace"
|
3
|
+
|
4
|
+
class RestApplication < Sinatra::Base
|
5
|
+
set :server, :puma
|
6
|
+
disable :raise_errors
|
7
|
+
disable :show_exceptions
|
8
|
+
|
9
|
+
set :dump_errors, true
|
10
|
+
set :show_exceptions, :after_handler
|
11
|
+
|
12
|
+
register Sinatra::Namespace
|
13
|
+
|
14
|
+
configure :production do
|
15
|
+
# ...
|
16
|
+
end
|
17
|
+
|
18
|
+
configure :development do
|
19
|
+
# ...
|
20
|
+
end
|
21
|
+
|
22
|
+
# for handling error
|
23
|
+
def self.init_and_raise_error(exception_class, status_code, response_model)
|
24
|
+
error exception_class do
|
25
|
+
status status_code
|
26
|
+
response_model
|
27
|
+
end
|
28
|
+
raise exception_class
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require_relative 'rest_application'
|
2
|
+
require_relative 'errors/common_error'
|
3
|
+
require_relative 'models/response_model'
|
4
|
+
|
5
|
+
|
6
|
+
class RestBase < RestApplication
|
7
|
+
before '/*' do
|
8
|
+
# puts 'before routes - set local thread'
|
9
|
+
RequestStore.store[:user] = "Nhuan Lai" #get the user by access_token
|
10
|
+
|
11
|
+
if request.env['CONTENT_TYPE'] == 'application/json'
|
12
|
+
body_content = JSON.load(request.body)
|
13
|
+
request.params[:body] = JSON.parse( body_content ) unless body_content.nil?
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
after '/*' do
|
18
|
+
response.body = response.body.to_json
|
19
|
+
end
|
20
|
+
|
21
|
+
# Health Check for Consul, run when start Consul
|
22
|
+
def self.start_consul_check_http(url)
|
23
|
+
get url do
|
24
|
+
'The service is healthy!'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# # Example API route
|
29
|
+
# namespace Container["application_configuration"].rest.path do
|
30
|
+
# get '/seed_1' do
|
31
|
+
# 'Seed 1 API'
|
32
|
+
# end
|
33
|
+
# get '/seed_2' do
|
34
|
+
# 'Seed 2 API'
|
35
|
+
# end
|
36
|
+
# end
|
37
|
+
|
38
|
+
# # Example for handling response
|
39
|
+
# get '/health' do
|
40
|
+
# # success case
|
41
|
+
# ResponseModel
|
42
|
+
# .new({status: 'OK', status_code: 200})
|
43
|
+
# .success
|
44
|
+
# # error case
|
45
|
+
# errors = [
|
46
|
+
# {
|
47
|
+
# field: 'error field',
|
48
|
+
# code: 'error code',
|
49
|
+
# message: 'error message'
|
50
|
+
# }
|
51
|
+
# ]
|
52
|
+
# ResponseModel
|
53
|
+
# .new({status: 'FAILED', status_code: 422})
|
54
|
+
# .error(CommonError::InvalidData, errors)
|
55
|
+
# end
|
56
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class RestConfiguration < Model
|
2
|
+
attribute :host, String.default('localhost')
|
3
|
+
attribute :port, Integer.default(8080)
|
4
|
+
attribute :path, String.default('/api/v1')
|
5
|
+
|
6
|
+
# Sample
|
7
|
+
# {
|
8
|
+
# host: 'localhost',
|
9
|
+
# port: 8080,
|
10
|
+
# path: '/api/v1'
|
11
|
+
# }
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative '../../rest/rest_base'
|
2
|
+
require_relative '../security_context_holder'
|
3
|
+
|
4
|
+
module Authenticator
|
5
|
+
def has_roles roles
|
6
|
+
lambda {|*args, &blk|
|
7
|
+
# puts 'has_roles'
|
8
|
+
check_role = true # check role here and return true/false
|
9
|
+
if check_role
|
10
|
+
true
|
11
|
+
else
|
12
|
+
false
|
13
|
+
end
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def authenticate isAuthorized
|
18
|
+
lambda {|self_model, *args, &blk|
|
19
|
+
# puts "isAuthorized"
|
20
|
+
# check authentication and authorization here, then return true/false
|
21
|
+
if isAuthorized.call(*args, &blk)
|
22
|
+
true
|
23
|
+
else
|
24
|
+
raise CommonError::PermissionDenied
|
25
|
+
end
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,37 @@
|
|
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
|
+
ResponseModel
|
14
|
+
.new({status: 'FAILED', status_code: 422})
|
15
|
+
.error(CommonError::InvalidData, valid_result[1])
|
16
|
+
end
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def check_validate(seed_model, self_rest)
|
21
|
+
errors = seed_model.validate
|
22
|
+
if errors.empty?
|
23
|
+
[true]
|
24
|
+
else
|
25
|
+
errors_response = convert_errors_response(errors)
|
26
|
+
[false, errors_response, self_rest]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def convert_errors_response(error_list)
|
31
|
+
errors = []
|
32
|
+
error_list.each do |key, value|
|
33
|
+
errors << {field: key, message: value}
|
34
|
+
end
|
35
|
+
errors
|
36
|
+
end
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,305 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: blsk_service_core
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- blsk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-06-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-system
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.10.0
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.10.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.10.0
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.10.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: sinatra
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 2.0.1
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 2.0.1
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 2.0.1
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 2.0.1
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: sinatra-contrib
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 2.0.1
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 2.0.1
|
63
|
+
type: :runtime
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 2.0.1
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 2.0.1
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
name: puma
|
75
|
+
requirement: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - "~>"
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 3.11.4
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.11.4
|
83
|
+
type: :runtime
|
84
|
+
prerelease: false
|
85
|
+
version_requirements: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 3.11.4
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: 3.11.4
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
name: cassandra-driver
|
95
|
+
requirement: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - "~>"
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: 3.2.2
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: 3.2.2
|
103
|
+
type: :runtime
|
104
|
+
prerelease: false
|
105
|
+
version_requirements: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 3.2.2
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: 3.2.2
|
113
|
+
- !ruby/object:Gem::Dependency
|
114
|
+
name: query_builder
|
115
|
+
requirement: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - "~>"
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: 0.0.4
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: 0.0.4
|
123
|
+
type: :runtime
|
124
|
+
prerelease: false
|
125
|
+
version_requirements: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - "~>"
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: 0.0.4
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: 0.0.4
|
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
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: dry-validation
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - "~>"
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: 0.12.0
|
160
|
+
- - ">="
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: 0.12.0
|
163
|
+
type: :runtime
|
164
|
+
prerelease: false
|
165
|
+
version_requirements: !ruby/object:Gem::Requirement
|
166
|
+
requirements:
|
167
|
+
- - "~>"
|
168
|
+
- !ruby/object:Gem::Version
|
169
|
+
version: 0.12.0
|
170
|
+
- - ">="
|
171
|
+
- !ruby/object:Gem::Version
|
172
|
+
version: 0.12.0
|
173
|
+
- !ruby/object:Gem::Dependency
|
174
|
+
name: bundler
|
175
|
+
requirement: !ruby/object:Gem::Requirement
|
176
|
+
requirements:
|
177
|
+
- - "~>"
|
178
|
+
- !ruby/object:Gem::Version
|
179
|
+
version: 1.16.2
|
180
|
+
- - ">="
|
181
|
+
- !ruby/object:Gem::Version
|
182
|
+
version: 1.16.2
|
183
|
+
type: :development
|
184
|
+
prerelease: false
|
185
|
+
version_requirements: !ruby/object:Gem::Requirement
|
186
|
+
requirements:
|
187
|
+
- - "~>"
|
188
|
+
- !ruby/object:Gem::Version
|
189
|
+
version: 1.16.2
|
190
|
+
- - ">="
|
191
|
+
- !ruby/object:Gem::Version
|
192
|
+
version: 1.16.2
|
193
|
+
- !ruby/object:Gem::Dependency
|
194
|
+
name: rake-test
|
195
|
+
requirement: !ruby/object:Gem::Requirement
|
196
|
+
requirements:
|
197
|
+
- - "~>"
|
198
|
+
- !ruby/object:Gem::Version
|
199
|
+
version: 1.0.0
|
200
|
+
- - ">="
|
201
|
+
- !ruby/object:Gem::Version
|
202
|
+
version: 1.0.0
|
203
|
+
type: :development
|
204
|
+
prerelease: false
|
205
|
+
version_requirements: !ruby/object:Gem::Requirement
|
206
|
+
requirements:
|
207
|
+
- - "~>"
|
208
|
+
- !ruby/object:Gem::Version
|
209
|
+
version: 1.0.0
|
210
|
+
- - ">="
|
211
|
+
- !ruby/object:Gem::Version
|
212
|
+
version: 1.0.0
|
213
|
+
- !ruby/object:Gem::Dependency
|
214
|
+
name: rake
|
215
|
+
requirement: !ruby/object:Gem::Requirement
|
216
|
+
requirements:
|
217
|
+
- - "~>"
|
218
|
+
- !ruby/object:Gem::Version
|
219
|
+
version: 2.5.0
|
220
|
+
- - ">="
|
221
|
+
- !ruby/object:Gem::Version
|
222
|
+
version: 2.5.0
|
223
|
+
type: :development
|
224
|
+
prerelease: false
|
225
|
+
version_requirements: !ruby/object:Gem::Requirement
|
226
|
+
requirements:
|
227
|
+
- - "~>"
|
228
|
+
- !ruby/object:Gem::Version
|
229
|
+
version: 2.5.0
|
230
|
+
- - ">="
|
231
|
+
- !ruby/object:Gem::Version
|
232
|
+
version: 2.5.0
|
233
|
+
- !ruby/object:Gem::Dependency
|
234
|
+
name: rspec
|
235
|
+
requirement: !ruby/object:Gem::Requirement
|
236
|
+
requirements:
|
237
|
+
- - "~>"
|
238
|
+
- !ruby/object:Gem::Version
|
239
|
+
version: 3.7.0
|
240
|
+
- - ">="
|
241
|
+
- !ruby/object:Gem::Version
|
242
|
+
version: 3.7.0
|
243
|
+
type: :development
|
244
|
+
prerelease: false
|
245
|
+
version_requirements: !ruby/object:Gem::Requirement
|
246
|
+
requirements:
|
247
|
+
- - "~>"
|
248
|
+
- !ruby/object:Gem::Version
|
249
|
+
version: 3.7.0
|
250
|
+
- - ">="
|
251
|
+
- !ruby/object:Gem::Version
|
252
|
+
version: 3.7.0
|
253
|
+
description: Code Skeleton for BLSK Service Core
|
254
|
+
email: support@blsk.com
|
255
|
+
executables: []
|
256
|
+
extensions: []
|
257
|
+
extra_rdoc_files: []
|
258
|
+
files:
|
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/config.yml
|
268
|
+
- src/consul/consul_configuration.rb
|
269
|
+
- src/consul/consul_service.rb
|
270
|
+
- src/ioc/container.rb
|
271
|
+
- src/ioc/injector.rb
|
272
|
+
- src/models/model.rb
|
273
|
+
- src/rest/errors/common_error.rb
|
274
|
+
- src/rest/models/response_model.rb
|
275
|
+
- src/rest/rest_application.rb
|
276
|
+
- src/rest/rest_base.rb
|
277
|
+
- src/rest/rest_configuration.rb
|
278
|
+
- src/security/decorators/authenticate.rb
|
279
|
+
- src/security/security_context_holder.rb
|
280
|
+
- src/validation/decorators/valid.rb
|
281
|
+
homepage: http://rubygems.org/gems/blsk_service_core
|
282
|
+
licenses:
|
283
|
+
- MIT
|
284
|
+
metadata: {}
|
285
|
+
post_install_message:
|
286
|
+
rdoc_options: []
|
287
|
+
require_paths:
|
288
|
+
- src
|
289
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
290
|
+
requirements:
|
291
|
+
- - ">="
|
292
|
+
- !ruby/object:Gem::Version
|
293
|
+
version: 2.3.0
|
294
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
295
|
+
requirements:
|
296
|
+
- - ">="
|
297
|
+
- !ruby/object:Gem::Version
|
298
|
+
version: '0'
|
299
|
+
requirements: []
|
300
|
+
rubyforge_project:
|
301
|
+
rubygems_version: 2.7.7
|
302
|
+
signing_key:
|
303
|
+
specification_version: 4
|
304
|
+
summary: BLSK Service Core
|
305
|
+
test_files: []
|