arkaan 0.7.4 → 0.7.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/arkaan/utils/controller.rb +13 -9
- data/lib/arkaan/utils/micro_service.rb +92 -48
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc727a94560a7d9ab37e10b788a28e322ba0b346
|
4
|
+
data.tar.gz: 75877dfb1faff014c23f8e3afb828de8edfb071f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0636b1c58ce076e7594afc504232f7014e3a17021b337c96dd36a74702c98a45618516b3df20dd5ab7f21acc9d74e27eb1562c45272dd76f4951b871fe6f870c
|
7
|
+
data.tar.gz: 991da6be9cd21c206ab54c320eddb089be6a62e0c6eb84de842c6a98402fab5340e017c42fc9f6731dc43c251409e8215cd9ae228f002a675314013be506c135
|
@@ -20,24 +20,28 @@ module Arkaan
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
# Creates a premium route whithin the Sinatra application, and registers it in the database if it does not already exists.
|
24
|
+
# @param verb [String] the HTTP method used to create this route.
|
25
|
+
# @param path [String] the path, beginning with a /, of the route to create.
|
27
26
|
def self.declare_route(verb, path, &block)
|
28
27
|
self.declare_route_with(verb, path, false, &block)
|
29
28
|
end
|
30
29
|
|
30
|
+
# Creates a non premium route whithin the Sinatra application, and registers it in the database if it does not already exists.
|
31
|
+
# @param verb [String] the HTTP method used to create this route.
|
32
|
+
# @param path [String] the path, beginning with a /, of the route to create.
|
31
33
|
def self.declare_premium_route(verb, path, &block)
|
32
34
|
self.declare_route_with(verb, path, true, &block)
|
33
35
|
end
|
34
36
|
|
37
|
+
# Creates a route whithin the Sinatra application, and registers it in the database if it does not already exists.
|
38
|
+
# @param verb [String] the HTTP method used to create this route.
|
39
|
+
# @param path [String] the path, beginning with a /, of the route to create.
|
40
|
+
# @param premium [Boolean] TRUE to make the route premium, FALSE otherwise.
|
35
41
|
def self.declare_route_with(verb, path, premium, &block)
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
@@micro_service.save!
|
40
|
-
end
|
42
|
+
service = Arkaan::Utils::MicroService.instance.service
|
43
|
+
unless service.nil? || !service.routes.where(path: path, verb: verb).first.nil?
|
44
|
+
Arkaan::Monitoring::Route.create(path: path, verb: verb, premium: premium, service: service)
|
41
45
|
end
|
42
46
|
self.public_send(verb, path, &block)
|
43
47
|
end
|
@@ -1,68 +1,112 @@
|
|
1
1
|
module Arkaan
|
2
2
|
module Utils
|
3
|
-
#
|
3
|
+
# This class is a singleton to load and save parameters for the whole application.
|
4
4
|
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
5
5
|
class MicroService
|
6
|
+
include Singleton
|
6
7
|
|
7
|
-
# @!attribute [r] root
|
8
|
-
# @return [String] the root path of the application, from where each path is deduced.
|
9
|
-
attr_reader :root
|
10
8
|
# @!attribute [r] name
|
11
|
-
# @return [String] the name of the service
|
12
|
-
attr_reader :name
|
13
|
-
# @!attribute [r] test_mode
|
14
|
-
# @return [Boolean] TRUE if the micro service is initialized from a test suite, FALSE otherwise.
|
15
|
-
attr_reader :test_mode
|
16
|
-
# @!attribute [r] service
|
17
|
-
# @return [Arkaan::Monitoring::Service] the service stored in the database corresponding to this application.
|
9
|
+
# @return [String] the name of the service you want to load.
|
18
10
|
attr_reader :service
|
11
|
+
# @!attribute [r] location
|
12
|
+
# @return [String] the path to the file loading the whole application, used to deduce the loading paths.
|
13
|
+
attr_reader :location
|
19
14
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
def initialize(name:, root:, test_mode: false)
|
24
|
-
@root = test_mode ? File.join(root, '..') : root
|
25
|
-
@name = name
|
26
|
-
@test_mode = test_mode
|
27
|
-
require_mongoid_config(root)
|
28
|
-
@service = register_service
|
15
|
+
def initialize
|
16
|
+
@location = false
|
17
|
+
@service = false
|
29
18
|
end
|
30
19
|
|
31
|
-
#
|
32
|
-
# @
|
33
|
-
def
|
34
|
-
|
35
|
-
require_folder(root, 'controllers')
|
36
|
-
if test_mode
|
37
|
-
require_folder(root, 'spec', 'support')
|
38
|
-
require_folder(root, 'spec', 'shared')
|
39
|
-
end
|
40
|
-
return
|
20
|
+
# Determines if the application can be loaded (all the parameters have been correctly set)
|
21
|
+
# @return [Boolean] TRUE if the application can be safely loaded, FALSE otherwise.
|
22
|
+
def loadable?
|
23
|
+
return !!(service && location)
|
41
24
|
end
|
42
25
|
|
43
|
-
#
|
44
|
-
# @return [
|
45
|
-
def
|
46
|
-
service
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
26
|
+
# Getter for the path on which the service is mapped.
|
27
|
+
# @return [String, Boolean] the absolute path in the URL on which the service is mapped upon, or FALSE if it's not set already.
|
28
|
+
def path
|
29
|
+
return service ? service.path : false
|
30
|
+
end
|
31
|
+
|
32
|
+
# Getter for the name of the service.
|
33
|
+
# @return [String, Boolean] the name of the service as it is registered in the database, or FALSE if it's not set already.
|
34
|
+
def name
|
35
|
+
return service ? service.key : false
|
36
|
+
end
|
37
|
+
|
38
|
+
# Look for the service and sets it if it's found in the database, or set it to nil if not found.
|
39
|
+
# @param [String] service_name - the name of the service to look for in the database.
|
40
|
+
# @return [Arkaan::utils::MicroService] the instance of the micro-service to chain other calls.
|
41
|
+
def register_as(service_name)
|
42
|
+
@service = Arkaan::Monitoring::Service.where(key: service_name).first
|
43
|
+
register_service if @service.nil?
|
44
|
+
register_instance
|
45
|
+
return self
|
46
|
+
end
|
47
|
+
|
48
|
+
# Sets the location of the file calling the micro service and initializing it so that it's used as root.
|
49
|
+
# @param filename [String] the full naame of the file with the extension.
|
50
|
+
# @return [Arkaan::utils::MicroService] the instance of the micro-service to chain other calls.
|
51
|
+
def from_location(filename)
|
52
|
+
@location = File.dirname(filename)
|
53
|
+
return self
|
54
|
+
end
|
55
|
+
|
56
|
+
# Loads the application in standard (production/development) mode, without the test files.
|
57
|
+
# @return [Arkaan::utils::MicroService] the instance of the micro-service to chain other calls.
|
58
|
+
def in_standard_mode
|
59
|
+
return load_application(test_mode: false)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Loads the application in test mode, by adding the needed files to run the test suite to the standard loading process.
|
63
|
+
# @return [Arkaan::utils::MicroService] the instance of the micro-service to chain other calls.
|
64
|
+
def in_test_mode
|
65
|
+
@location = File.join(location, '..')
|
66
|
+
return load_application(test_mode: true)
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def register_service(key)
|
72
|
+
@service = Arkaan::Service.create(key: key, path: "/#{key}")
|
73
|
+
end
|
74
|
+
|
75
|
+
def register_instance
|
76
|
+
instance = @service.instances.where(url: ENV['SERVICE_URL']).first
|
77
|
+
if instance.nil?
|
78
|
+
instance = Arkaan::Monitoring::Instance.create(service: @service, url: ENV['SERVICE_URL'])
|
52
79
|
end
|
53
|
-
return
|
80
|
+
return instance
|
81
|
+
end
|
82
|
+
|
83
|
+
def load_application(test_mode: false)
|
84
|
+
@location = File.join(location, '..')
|
85
|
+
load_mongoid_configuration
|
86
|
+
load_standard_files
|
87
|
+
load_test_files if test_mode
|
88
|
+
return self
|
89
|
+
end
|
90
|
+
|
91
|
+
def load_mongoid_configuration
|
92
|
+
Mongoid.load!(File.join(location, 'config', 'mongoid.yml'))
|
54
93
|
end
|
55
94
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
Dir[File.join(paths_elements, '**', '*.rb')].each { |filename| require filename }
|
95
|
+
def load_standard_files
|
96
|
+
require_folder('decorators')
|
97
|
+
require_folder('controllers')
|
60
98
|
end
|
61
99
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
100
|
+
def load_test_files
|
101
|
+
require_folder('spec', 'support')
|
102
|
+
require_folder('spec', 'factories')
|
103
|
+
require_folder('spec', 'shared')
|
104
|
+
end
|
105
|
+
|
106
|
+
def require_folder(*folders)
|
107
|
+
Dir[File.join(location, folders)].each do |filename|
|
108
|
+
require filename
|
109
|
+
end
|
66
110
|
end
|
67
111
|
end
|
68
112
|
end
|