midb 1.0.5 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/midb +6 -3
- data/lib/midb/dbengine_model.rb +78 -74
- data/lib/midb/errors_view.rb +26 -27
- data/lib/midb/hooks.rb +13 -0
- data/lib/midb/security_controller.rb +33 -31
- data/lib/midb/server_controller.rb +228 -221
- data/lib/midb/server_model.rb +248 -232
- data/lib/midb/server_view.rb +97 -87
- data/lib/midb/serverengine_controller.rb +113 -105
- data/lib/midb.rb +2 -1
- metadata +3 -2
@@ -11,11 +11,11 @@ require 'json'
|
|
11
11
|
require 'sqlite3'
|
12
12
|
|
13
13
|
module MIDB
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
module API
|
15
|
+
# This controller controls the behavior of the midb server.
|
16
|
+
class Controller
|
17
|
+
# Attribute declaration here
|
18
|
+
#
|
19
19
|
# @!attribute args
|
20
20
|
# @return [Array<String>] Arguments passed to the binary.
|
21
21
|
# @!attribute config
|
@@ -27,260 +27,267 @@ module MIDB
|
|
27
27
|
# @!attribute port
|
28
28
|
# @return [Fixnum] Port where the server will listen.
|
29
29
|
attr_accessor :args, :config, :db, :http_status, :port
|
30
|
-
end
|
31
|
-
# Default values
|
32
|
-
#
|
33
|
-
# @see #http_status
|
34
|
-
# @see #args
|
35
|
-
# @see #config
|
36
|
-
# @see #port
|
37
|
-
@http_status = "200 OK"
|
38
|
-
@args = []
|
39
|
-
@config = Hash.new()
|
40
|
-
@port = 8081
|
41
30
|
|
42
|
-
|
43
|
-
|
44
|
-
|
31
|
+
# Constructor for this controller.
|
32
|
+
#
|
33
|
+
# @param args [Array<String>] Arguments passed to the binary.
|
34
|
+
def initialize(args)
|
35
|
+
# Default values
|
36
|
+
#
|
37
|
+
# @see #http_status
|
38
|
+
# @see #args
|
39
|
+
# @see #config
|
40
|
+
# @see #port
|
41
|
+
@http_status = "200 OK"
|
42
|
+
@args = args
|
43
|
+
@config = Hash.new()
|
44
|
+
@port = 8081
|
45
|
+
end
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
47
|
+
#####################
|
48
|
+
# Server commands #
|
49
|
+
#####################
|
50
|
+
|
51
|
+
# $ midb help
|
52
|
+
#
|
53
|
+
# Show some help for either midb or a command.
|
54
|
+
def do_help()
|
55
|
+
if @args.length > 1
|
56
|
+
case @args[1]
|
57
|
+
when "bootstrap"
|
58
|
+
MIDB::Interface::Server.help(:bootstrap)
|
59
|
+
when "set"
|
60
|
+
MIDB::Interface::Server.help(:set)
|
61
|
+
when "start"
|
62
|
+
MIDB::Interface::Server.help(:start)
|
63
|
+
when "serve"
|
64
|
+
MIDB::Interface::Server.help(:serve)
|
65
|
+
when "unserve"
|
66
|
+
MIDB::Interface::Server.help(:unserve)
|
67
|
+
else
|
68
|
+
MIDB::Interface::Errors.die(:no_help)
|
69
|
+
end
|
62
70
|
else
|
63
|
-
MIDB::
|
71
|
+
MIDB::Interface::Server.help(:list)
|
64
72
|
end
|
65
|
-
else
|
66
|
-
MIDB::ServerView.help(:list)
|
67
73
|
end
|
68
|
-
end
|
69
74
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
75
|
+
# $ midb bootstrap
|
76
|
+
#
|
77
|
+
# Bootstrap a new midb project in the active directory.
|
78
|
+
def do_bootstrap()
|
79
|
+
if File.file?(".midb.yaml")
|
80
|
+
MIDB::Interface::Errors.die(:already_project)
|
81
|
+
else
|
82
|
+
# If the file doesn't exist it, create it with the default stuff
|
83
|
+
@config["serves"] = []
|
84
|
+
@config["status"] = :asleep # The server is initially asleep
|
85
|
+
@config["apikey"] = "midb-api" # This should be changed, it's the private API key
|
86
|
+
@config["dbengine"] = :sqlite3 # SQLite is the default engine
|
87
|
+
# Default DB configuration for MySQL and other engines
|
88
|
+
@config["dbhost"] = "localhost"
|
89
|
+
@config["dbport"] = 3306
|
90
|
+
@config["dbuser"] = "nobody"
|
91
|
+
@config["dbpassword"] = "openaccess"
|
92
|
+
File.open(".midb.yaml", 'w') do |l|
|
93
|
+
l.write @config.to_yaml
|
94
|
+
end
|
95
|
+
# Create json/ and db/ directory if it doesn't exist
|
96
|
+
Dir.mkdir("json") unless File.exists?("json")
|
97
|
+
Dir.mkdir("db") unless File.exists?("db")
|
98
|
+
MIDB::Interface::Server.info(:bootstrap)
|
89
99
|
end
|
90
|
-
# Create json/ and db/ directory if it doesn't exist
|
91
|
-
Dir.mkdir("json") unless File.exists?("json")
|
92
|
-
Dir.mkdir("db") unless File.exists?("db")
|
93
|
-
MIDB::ServerView.info(:bootstrap)
|
94
100
|
end
|
95
|
-
end
|
96
101
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
102
|
+
# $ midb set
|
103
|
+
#
|
104
|
+
# Set the config for the project.
|
105
|
+
# Check syntax
|
106
|
+
def do_set()
|
107
|
+
MIDB::Interface::Errors.die(:syntax) if @args.length < 2
|
108
|
+
subset = @args[1].split(":")[0]
|
109
|
+
subcmd = @args[1].split(":")[1]
|
110
|
+
set = @args.length < 3 ? false : true
|
111
|
+
setter = @args[2] if set
|
112
|
+
case subset
|
113
|
+
when "db"
|
114
|
+
# DB Config
|
115
|
+
case subcmd
|
116
|
+
when "engine"
|
117
|
+
if set
|
118
|
+
@config["dbengine"] = case setter.downcase
|
119
|
+
when "sqlite3" then :sqlite3
|
120
|
+
when "mysql" then :mysql
|
121
|
+
else :undef
|
122
|
+
end
|
123
|
+
if @config["dbengine"] == :undef
|
124
|
+
MIDB::Interface::Errors.die(:unsupported_engine)
|
125
|
+
@config["dbengine"] = :sqlite3
|
126
|
+
end
|
121
127
|
end
|
128
|
+
MIDB::Interface::Server.out_config(:dbengine, @config)
|
129
|
+
when "host"
|
130
|
+
@config["dbhost"] = setter if set
|
131
|
+
MIDB::Interface::Server.out_config(:dbhost, @config)
|
132
|
+
when "port"
|
133
|
+
@config["dbport"] = setter if set
|
134
|
+
MIDB::Interface::Server.out_config(:dbport, @config)
|
135
|
+
when "user"
|
136
|
+
@config["dbuser"] = setter if set
|
137
|
+
MIDB::Interface::Server.out_config(:dbuser, @config)
|
138
|
+
when "password"
|
139
|
+
@config["dbpassword"] = setter if set
|
140
|
+
MIDB::Interface::Server.out_config(:dbpassword, @config)
|
141
|
+
else
|
142
|
+
MIDB::Interface::Errors.die(:syntax)
|
143
|
+
end
|
144
|
+
when "api"
|
145
|
+
case subcmd
|
146
|
+
when "key"
|
147
|
+
@config["apikey"] = setter if set
|
148
|
+
MIDB::Interface::Server.out_config(:apikey, @config)
|
122
149
|
end
|
123
|
-
MIDB::ServerView.out_config(:dbengine)
|
124
|
-
when "host"
|
125
|
-
@config["dbhost"] = setter if set
|
126
|
-
MIDB::ServerView.out_config(:dbhost)
|
127
|
-
when "port"
|
128
|
-
@config["dbport"] = setter if set
|
129
|
-
MIDB::ServerView.out_config(:dbport)
|
130
|
-
when "user"
|
131
|
-
@config["dbuser"] = setter if set
|
132
|
-
MIDB::ServerView.out_config(:dbuser)
|
133
|
-
when "password"
|
134
|
-
@config["dbpassword"] = setter if set
|
135
|
-
MIDB::ServerView.out_config(:dbpassword)
|
136
150
|
else
|
137
|
-
MIDB::
|
151
|
+
MIDB::Interface::Errors.die(:syntax)
|
138
152
|
end
|
139
|
-
when "api"
|
140
|
-
case subcmd
|
141
|
-
when "key"
|
142
|
-
@config["apikey"] = setter if set
|
143
|
-
MIDB::ServerView.out_config(:apikey)
|
144
|
-
end
|
145
|
-
else
|
146
|
-
MIDB::ErrorsView.die(:syntax)
|
147
153
|
end
|
148
|
-
end
|
149
154
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
155
|
+
# $ midb start
|
156
|
+
#
|
157
|
+
# Start the server (call the loop)
|
158
|
+
def do_start()
|
159
|
+
# Check syntax
|
160
|
+
MIDB::Interface::Errors.die(:syntax) if @args.length < 2
|
161
|
+
MMIDB::Interface::Errors.die(:syntax) if @args[1].split(":")[0] != "db"
|
162
|
+
# Is the server already started?
|
163
|
+
MIDB::Interface::Errors.die(:server_already_started) if @config["status"] == :running
|
164
|
+
# Are any files being served?
|
165
|
+
MIDB::Interface::Errors.die(:no_serves) if @config["serves"].length == 0
|
166
|
+
# If it successfully starts, change our status and notify thru view
|
167
|
+
@args.each do |arg|
|
168
|
+
if arg.split(":")[0] == "db"
|
169
|
+
@db = arg.split(":")[1]
|
170
|
+
elsif arg.split(":")[0] == "port"
|
171
|
+
@port = arg.split(":")[1]
|
172
|
+
end
|
167
173
|
end
|
168
|
-
end
|
169
174
|
|
170
|
-
|
171
|
-
|
172
|
-
@
|
173
|
-
|
174
|
-
|
175
|
-
|
175
|
+
# Call the server engine
|
176
|
+
eng = MIDB::API::Engine.new(@db, @http_status, @config)
|
177
|
+
if eng.start(@port)
|
178
|
+
@config["status"] = :running
|
179
|
+
MIDB::Interface::Server.success()
|
180
|
+
else
|
181
|
+
MIDB::Interface::Errors.die(:server_error)
|
182
|
+
end
|
176
183
|
end
|
177
|
-
end
|
178
184
|
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
185
|
+
# $ midb serve
|
186
|
+
#
|
187
|
+
# Serve a JSON file
|
188
|
+
def do_serve()
|
189
|
+
# Check if there's a second argument
|
190
|
+
MIDB::Interface::Errors.die(:syntax) if @args.length < 2
|
191
|
+
# Is the server running? It shouldn't
|
192
|
+
MIDB::Interface::Errors.die(:server_already_started) if @config["status"] == :running
|
193
|
+
# Is there such file as @args[1]?
|
194
|
+
MIDB::Interface::Errors.die(:file_404) unless File.file?("./json/" + @args[1])
|
195
|
+
# Is the file a JSON file?
|
196
|
+
MMIDB::Interface::Errors.die(:not_json) unless File.extname(@args[1]) == ".json"
|
197
|
+
# Is the file already loaded?
|
198
|
+
MIDB::Interface::Errors.die(:json_exists) if @config["serves"].include? @args[1]
|
193
199
|
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
200
|
+
# Tests passed, so let's add the file to the served list!
|
201
|
+
@config["serves"].push @args[1]
|
202
|
+
MIDB::Interface::Server.show_serving(@config)
|
203
|
+
end
|
198
204
|
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
205
|
+
# $ midb unserve
|
206
|
+
#
|
207
|
+
# Stop serving a JSON file
|
208
|
+
def self.do_unserve()
|
209
|
+
# Check if there's a second argument
|
210
|
+
MIDB::Interface::Errors.die(:syntax) if @args.length < 2
|
211
|
+
# Is the server running? It shouldn't
|
212
|
+
MIDB::Interface::Errors.die(:server_already_started) if @config["status"] == :running
|
213
|
+
# Is the file already loaded?
|
214
|
+
MIDB::Interface::Errors.die(:json_not_exists) unless @config["serves"].include? @args[1]
|
209
215
|
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
216
|
+
# Delete it!
|
217
|
+
@config["serves"].delete @args[1]
|
218
|
+
MIDB::Interface::Server.show_serving(@config)
|
219
|
+
end
|
214
220
|
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
+
# $ midb stop
|
222
|
+
#
|
223
|
+
# Stop the server in case it's running in the background.
|
224
|
+
def do_stop()
|
225
|
+
# Is the server running?
|
226
|
+
MIDB::Interface::Errors.die(:server_not_running) unless @config["status"] == :running
|
221
227
|
|
222
|
-
|
223
|
-
|
224
|
-
|
228
|
+
@config["status"] = :asleep
|
229
|
+
MIDB::Interface::Server.server_stopped()
|
230
|
+
end
|
225
231
|
|
226
232
|
|
227
233
|
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
+
# $ midb
|
235
|
+
#
|
236
|
+
# Decide the behavior of the server in function of the arguments.
|
237
|
+
def init()
|
238
|
+
# We should have at least one argument, which can be `run` or `serve`
|
239
|
+
MIDB::Interface::Errors.die(:noargs) if @args.length < 1
|
234
240
|
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
241
|
+
# Load the config
|
242
|
+
if File.file?(".midb.yaml")
|
243
|
+
@config = YAML.load_file(".midb.yaml")
|
244
|
+
else
|
245
|
+
# If the file doesn't exist, we need to bootstrap
|
246
|
+
MIDB::Interface::Errors.die(:bootstrap) if @args[0] != "help" && args[0] != "bootstrap"
|
247
|
+
end
|
242
248
|
|
243
|
-
|
249
|
+
case @args[0]
|
244
250
|
|
245
|
-
|
246
|
-
|
247
|
-
|
251
|
+
# Command: help
|
252
|
+
when "help"
|
253
|
+
self.do_help()
|
248
254
|
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
255
|
+
# Command: bootstrap
|
256
|
+
when "bootstrap"
|
257
|
+
self.do_bootstrap()
|
258
|
+
|
259
|
+
# Command: set
|
260
|
+
when "set"
|
261
|
+
self.do_set()
|
256
262
|
|
257
263
|
|
258
|
-
|
259
|
-
|
260
|
-
|
264
|
+
# Command: start
|
265
|
+
when "start"
|
266
|
+
self.do_start()
|
261
267
|
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
268
|
+
# Command: serve
|
269
|
+
# Serves a JSON file
|
270
|
+
when "serve"
|
271
|
+
self.do_serve()
|
266
272
|
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
273
|
+
# Command: unserve
|
274
|
+
# Stop serving a JSON file.
|
275
|
+
when "unserve"
|
276
|
+
self.do_unserve()
|
271
277
|
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
278
|
+
# Command: stop
|
279
|
+
# Stops the server.
|
280
|
+
when "stop"
|
281
|
+
self.do_stop()
|
282
|
+
end
|
276
283
|
end
|
277
|
-
end
|
278
284
|
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
285
|
+
# Method: save
|
286
|
+
# Saves config to .midb.yaml
|
287
|
+
def save()
|
288
|
+
File.open(".midb.yaml", 'w') do |l|
|
289
|
+
l.write @config.to_yaml
|
290
|
+
end
|
284
291
|
end
|
285
292
|
end
|
286
293
|
end
|