midb 1.0.5 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,11 +11,11 @@ require 'json'
11
11
  require 'sqlite3'
12
12
 
13
13
  module MIDB
14
- # This controller controls the behavior of the midb server.
15
- class ServerController
16
-
17
- # Attribute declaration here
18
- class << self
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
- # Server commands #
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
- # $ midb help
47
- #
48
- # Show some help for either midb or a command.
49
- def self.do_help()
50
- if @args.length > 1
51
- case @args[1]
52
- when "bootstrap"
53
- MIDB::ServerView.help(:bootstrap)
54
- when "set"
55
- MIDB::ServerView.help(:set)
56
- when "start"
57
- MIDB::ServerView.help(:start)
58
- when "serve"
59
- MIDB::ServerView.help(:serve)
60
- when "unserve"
61
- MIDB::ServerView.help(:unserve)
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::ErrorsView.die(:no_help)
71
+ MIDB::Interface::Server.help(:list)
64
72
  end
65
- else
66
- MIDB::ServerView.help(:list)
67
73
  end
68
- end
69
74
 
70
- # $ midb bootstrap
71
- #
72
- # Bootstrap a new midb project in the active directory.
73
- def self.do_bootstrap()
74
- if File.file?(".midb.yaml")
75
- MIDB::ErrorsView.die(:already_project)
76
- else
77
- # If the file doesn't exist it, create it with the default stuff
78
- @config["serves"] = []
79
- @config["status"] = :asleep # The server is initially asleep
80
- @config["apikey"] = "midb-api" # This should be changed, it's the private API key
81
- @config["dbengine"] = :sqlite3 # SQLite is the default engine
82
- # Default DB configuration for MySQL and other engines
83
- @config["dbhost"] = "localhost"
84
- @config["dbport"] = 3306
85
- @config["dbuser"] = "nobody"
86
- @config["dbpassword"] = "openaccess"
87
- File.open(".midb.yaml", 'w') do |l|
88
- l.write @config.to_yaml
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
- # $ midb set
98
- #
99
- # Set the config for the project.
100
- # Check syntax
101
- def self.do_set()
102
- MIDB::ErrorsView.die(:syntax) if @args.length < 2
103
- subset = @args[1].split(":")[0]
104
- subcmd = @args[1].split(":")[1]
105
- set = @args.length < 3 ? false : true
106
- setter = @args[2] if set
107
- case subset
108
- when "db"
109
- # DB Config
110
- case subcmd
111
- when "engine"
112
- if set
113
- @config["dbengine"] = case setter.downcase
114
- when "sqlite3" then :sqlite3
115
- when "mysql" then :mysql
116
- else :undef
117
- end
118
- if @config["dbengine"] == :undef
119
- MIDB::ErrorsView.die(:unsupported_engine)
120
- @config["dbengine"] = :sqlite3
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::ErrorsView.die(:synax)
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
- # $ midb start
151
- #
152
- # Start the server (call the loop)
153
- def self.do_start()
154
- # Check syntax
155
- MIDB::ErrorsView.die(:syntax) if @args.length < 2
156
- MIDB::ErrorsView.die(:syntax) if @args[1].split(":")[0] != "db"
157
- # Is the server already started?
158
- MIDB::ErrorsView.die(:server_already_started) if @config["status"] == :running
159
- # Are any files being served?
160
- MIDB::ErrorsView.die(:no_serves) if @config["serves"].length == 0
161
- # If it successfully starts, change our status and notify thru view
162
- @args.each do |arg|
163
- if arg.split(":")[0] == "db"
164
- @db = arg.split(":")[1]
165
- elsif arg.split(":")[0] == "port"
166
- @port = arg.split(":")[1]
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
- # Call the server engine
171
- if MIDB::ServerEngineController.start(@port)
172
- @config["status"] = :running
173
- MIDB::ServerView.success()
174
- else
175
- MIDB::ErrorsView.die(:server_error)
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
- # $ midb serve
180
- #
181
- # Serve a JSON file
182
- def self.do_serve()
183
- # Check if there's a second argument
184
- MIDB::ErrorsView.die(:syntax) if @args.length < 2
185
- # Is the server running? It shouldn't
186
- MIDB::ErrorsView.die(:server_already_started) if @config["status"] == :running
187
- # Is there such file as @args[1]?
188
- MIDB::ErrorsView.die(:file_404) unless File.file?("./json/" + @args[1])
189
- # Is the file a JSON file?
190
- MIDB::ErrorsView.die(:not_json) unless File.extname(@args[1]) == ".json"
191
- # Is the file already loaded?
192
- MIDB::ErrorsView.die(:json_exists) if @config["serves"].include? @args[1]
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
- # Tests passed, so let's add the file to the served list!
195
- @config["serves"].push @args[1]
196
- MIDB::ServerView.show_serving()
197
- end
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
- # $ midb unserve
200
- #
201
- # Stop serving a JSON file
202
- def self.do_unserve()
203
- # Check if there's a second argument
204
- MIDB::ErrorsView.die(:syntax) if @args.length < 2
205
- # Is the server running? It shouldn't
206
- MIDB::ErrorsView.die(:server_already_started) if @config["status"] == :running
207
- # Is the file already loaded?
208
- MIDB::ErrorsView.die(:json_not_exists) unless @config["serves"].include? @args[1]
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
- # Delete it!
211
- @config["serves"].delete @args[1]
212
- MIDB::ServerView.show_serving()
213
- end
216
+ # Delete it!
217
+ @config["serves"].delete @args[1]
218
+ MIDB::Interface::Server.show_serving(@config)
219
+ end
214
220
 
215
- # $ midb stop
216
- #
217
- # Stop the server in case it's running in the background.
218
- def self.do_stop()
219
- # Is the server running?
220
- MIDB::ErrorsView.die(:server_not_running) unless @config["status"] == :running
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
- @config["status"] = :asleep
223
- MIDB::ServerView.server_stopped()
224
- end
228
+ @config["status"] = :asleep
229
+ MIDB::Interface::Server.server_stopped()
230
+ end
225
231
 
226
232
 
227
233
 
228
- # $ midb
229
- #
230
- # Decide the behavior of the server in function of the arguments.
231
- def self.init()
232
- # We should have at least one argument, which can be `run` or `serve`
233
- MIDB::ErrorsView.die(:noargs) if @args.length < 1
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
- # Load the config
236
- if File.file?(".midb.yaml")
237
- @config = YAML.load_file(".midb.yaml")
238
- else
239
- # If the file doesn't exist, we need to bootstrap
240
- MIDB::ErrorsView.die(:bootstrap) if @args[0] != "help" && args[0] != "bootstrap"
241
- end
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
- case @args[0]
249
+ case @args[0]
244
250
 
245
- # Command: help
246
- when "help"
247
- self.do_help()
251
+ # Command: help
252
+ when "help"
253
+ self.do_help()
248
254
 
249
- # Command: bootstrap
250
- when "bootstrap"
251
- self.do_bootstrap()
252
-
253
- # Command: set
254
- when "set"
255
- self.do_set()
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
- # Command: start
259
- when "start"
260
- self.do_start()
264
+ # Command: start
265
+ when "start"
266
+ self.do_start()
261
267
 
262
- # Command: serve
263
- # Serves a JSON file
264
- when "serve"
265
- self.do_serve()
268
+ # Command: serve
269
+ # Serves a JSON file
270
+ when "serve"
271
+ self.do_serve()
266
272
 
267
- # Command: unserve
268
- # Stop serving a JSON file.
269
- when "unserve"
270
- self.do_unserve()
273
+ # Command: unserve
274
+ # Stop serving a JSON file.
275
+ when "unserve"
276
+ self.do_unserve()
271
277
 
272
- # Command: stop
273
- # Stops the server.
274
- when "stop"
275
- self.do_stop()
278
+ # Command: stop
279
+ # Stops the server.
280
+ when "stop"
281
+ self.do_stop()
282
+ end
276
283
  end
277
- end
278
284
 
279
- # Method: save
280
- # Saves config to .midb.yaml
281
- def self.save()
282
- File.open(".midb.yaml", 'w') do |l|
283
- l.write @config.to_yaml
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