Class: MIDB::API::Controller
- Inherits:
-
Object
- Object
- MIDB::API::Controller
- Defined in:
- lib/midb/server_controller.rb
Overview
This controller controls the behavior of the midb server.
Instance Attribute Summary collapse
-
#args ⇒ Array<String>
Arguments passed to the binary.
-
#config ⇒ Hash
Contains the project’s configuration, saved in .midb.yaml.
-
#db ⇒ String
Database name (if SQLite is the engine, file name without extension).
-
#h ⇒ Object
MIDB::API::Hooks instance.
-
#hooks ⇒ Object
Attribute declaration here.
-
#http_status ⇒ String
HTTP status code and string representation for the header.
-
#port ⇒ Fixnum
Port where the server will listen.
Class Method Summary collapse
-
.do_unserve ⇒ Object
$ midb unserve.
Instance Method Summary collapse
-
#do_bootstrap ⇒ Object
$ midb bootstrap.
-
#do_help ⇒ Object
$ midb help.
-
#do_serve ⇒ Object
$ midb serve.
-
#do_set ⇒ Object
$ midb set.
-
#do_start ⇒ Object
$ midb start.
-
#do_stop ⇒ Object
$ midb stop.
-
#init ⇒ Object
$ midb.
-
#initialize(args, hooks = nil) ⇒ Controller
constructor
Constructor for this controller.
-
#save ⇒ Object
Method: save Saves config to .midb.yaml.
-
#to_bool(text) ⇒ Object
Convert an on/off string to a boolean.
Constructor Details
#initialize(args, hooks = nil) ⇒ Controller
Constructor for this controller.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/midb/server_controller.rb', line 50 def initialize(args, hooks=nil) # Default values # # @see #http_status # @see #args # @see #config # @see #port @http_status = "200 OK" @args = args @config = Hash.new() @port = 8081 if hooks == nil # At some point someone should register this. @hooks = MIDB::API::Hooks.new else @hooks = hooks end end |
Instance Attribute Details
#args ⇒ Array<String>
Returns Arguments passed to the binary.
32 33 34 |
# File 'lib/midb/server_controller.rb', line 32 def args @args end |
#config ⇒ Hash
Returns Contains the project’s configuration, saved in .midb.yaml.
32 |
# File 'lib/midb/server_controller.rb', line 32 attr_accessor :args, :config, :db, :http_status, :port, :hooks |
#db ⇒ String
Returns Database name (if SQLite is the engine, file name without extension).
32 |
# File 'lib/midb/server_controller.rb', line 32 attr_accessor :args, :config, :db, :http_status, :port, :hooks |
#h ⇒ Object
Returns MIDB::API::Hooks instance.
32 |
# File 'lib/midb/server_controller.rb', line 32 attr_accessor :args, :config, :db, :http_status, :port, :hooks |
#hooks ⇒ Object
Attribute declaration here
32 33 34 |
# File 'lib/midb/server_controller.rb', line 32 def hooks @hooks end |
#http_status ⇒ String
Returns HTTP status code and string representation for the header.
32 |
# File 'lib/midb/server_controller.rb', line 32 attr_accessor :args, :config, :db, :http_status, :port, :hooks |
#port ⇒ Fixnum
Returns Port where the server will listen.
32 |
# File 'lib/midb/server_controller.rb', line 32 attr_accessor :args, :config, :db, :http_status, :port, :hooks |
Class Method Details
.do_unserve ⇒ Object
$ midb unserve
Stop serving a JSON file
262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/midb/server_controller.rb', line 262 def self.do_unserve() # Check if there's a second argument MIDB::Interface::Errors.die(:syntax) if @args.length < 2 # Is the server running? It shouldn't MIDB::Interface::Errors.die(:server_already_started) if @config["status"] == :running # Is the file already loaded? MIDB::Interface::Errors.die(:json_not_exists) unless @config["serves"].include? @args[1] # Delete it! @config["serves"].delete @args[1] MIDB::Interface::Server.show_serving(@config) end |
Instance Method Details
#do_bootstrap ⇒ Object
$ midb bootstrap
Bootstrap a new midb project in the active directory.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/midb/server_controller.rb', line 100 def do_bootstrap() if File.file?(".midb.yaml") MIDB::Interface::Errors.die(:already_project) else # If the file doesn't exist it, create it with the default stuff @config["serves"] = [] @config["status"] = :asleep # The server is initially asleep @config["apikey"] = "midb-api" # This should be changed, it's the private API key # Optional API key for GET methods (void by default) @config["apigetkey"] = nil @config["dbengine"] = :sqlite3 # SQLite is the default engine # Default DB configuration for MySQL and other engines @config["dbhost"] = "localhost" @config["dbport"] = 3306 @config["dbuser"] = "nobody" @config["dbpassword"] = "openaccess" # New settings - privacy @config["privacyget"] = false @config["privacypost"] = true @config["privacyput"] = true @config["privacydelete"] = true File.open(".midb.yaml", 'w') do |l| l.write @config.to_yaml end # Create json/ and db/ directory if it doesn't exist Dir.mkdir("json") unless File.exists?("json") Dir.mkdir("db") unless File.exists?("db") MIDB::Interface::Server.info(:bootstrap) end end |
#do_help ⇒ Object
$ midb help
Show some help for either midb or a command.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/midb/server_controller.rb', line 76 def do_help() if @args.length > 1 case @args[1] when "bootstrap" MIDB::Interface::Server.help(:bootstrap) when "set" MIDB::Interface::Server.help(:set) when "start" MIDB::Interface::Server.help(:start) when "serve" MIDB::Interface::Server.help(:serve) when "unserve" MIDB::Interface::Server.help(:unserve) else MIDB::Interface::Errors.die(:no_help) end else MIDB::Interface::Server.help(:list) end end |
#do_serve ⇒ Object
$ midb serve
Serve a JSON file
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/midb/server_controller.rb', line 242 def do_serve() # Check if there's a second argument MIDB::Interface::Errors.die(:syntax) if @args.length < 2 # Is the server running? It shouldn't MIDB::Interface::Errors.die(:server_already_started) if @config["status"] == :running # Is there such file as @args[1]? MIDB::Interface::Errors.die(:file_404) unless File.file?("./json/" + @args[1]) # Is the file a JSON file? MMIDB::Interface::Errors.die(:not_json) unless File.extname(@args[1]) == ".json" # Is the file already loaded? MIDB::Interface::Errors.die(:json_exists) if @config["serves"].include? @args[1] # Tests passed, so let's add the file to the served list! @config["serves"].push @args[1] MIDB::Interface::Server.show_serving(@config) end |
#do_set ⇒ Object
$ midb set
Set the config for the project. Check syntax
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/midb/server_controller.rb', line 136 def do_set() MIDB::Interface::Errors.die(:syntax) if @args.length < 2 subset = @args[1].split(":")[0] subcmd = @args[1].split(":")[1] set = @args.length < 3 ? false : true setter = @args[2] if set case subset when "db" # DB Config case subcmd when "engine" if set @config["dbengine"] = case setter.downcase when "sqlite3" then :sqlite3 when "mysql" then :mysql else :undef end if @config["dbengine"] == :undef MIDB::Interface::Errors.die(:unsupported_engine) @config["dbengine"] = :sqlite3 end end MIDB::Interface::Server.out_config(:dbengine, @config) when "host" @config["dbhost"] = setter if set MIDB::Interface::Server.out_config(:dbhost, @config) when "port" @config["dbport"] = setter if set MIDB::Interface::Server.out_config(:dbport, @config) when "user" @config["dbuser"] = setter if set MIDB::Interface::Server.out_config(:dbuser, @config) when "password" @config["dbpassword"] = setter if set MIDB::Interface::Server.out_config(:dbpassword, @config) else MIDB::Interface::Errors.die(:syntax) end when "api" case subcmd when "key" @config["apikey"] = setter if set MIDB::Interface::Server.out_config(:apikey, @config) when "getkey" if setter == "nil" @config["apigetkey"] = nil if set else @config["apigetkey"] = setter if set end MIDB::Interface::Server.out_config(:apigetkey, @config) end when "privacy" case subcmd when "get" @config["privacyget"] = self.to_bool(setter) if set MIDB::Interface::Server.out_config(:privacyget, @config) when "post" @config["privacypost"] = self.to_bool(setter) if set MIDB::Interface::Server.out_config(:privacypost, @config) when "put" @config["privacyput"] = self.to_bool(setter) if set MIDB::Interface::Server.out_config(:privacyput, @config) when "delete" @config["privacydelete"] = self.to_bool(setter) if set MIDB::Interface::Server.out_config(:privacydelete, @config) else MIDB::Interface::Errors.die(:syntax) end else MIDB::Interface::Errors.die(:syntax) end end |
#do_start ⇒ Object
$ midb start
Start the server (call the loop)
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/midb/server_controller.rb', line 212 def do_start() # Check syntax MIDB::Interface::Errors.die(:syntax) if @args.length < 2 MMIDB::Interface::Errors.die(:syntax) if @args[1].split(":")[0] != "db" # Is the server already started? MIDB::Interface::Errors.die(:server_already_started) if @config["status"] == :running # Are any files being served? MIDB::Interface::Errors.die(:no_serves) if @config["serves"].length == 0 # If it successfully starts, change our status and notify thru view @args.each do |arg| if arg.split(":")[0] == "db" @db = arg.split(":")[1] elsif arg.split(":")[0] == "port" @port = arg.split(":")[1] end end # Call the server engine and supply the (non)existing hooks eng = MIDB::API::Engine.new(@db, @http_status, @config, @hooks) if eng.start(@port) @config["status"] = :running MIDB::Interface::Server.success() else MIDB::Interface::Errors.die(:server_error) end end |
#do_stop ⇒ Object
$ midb stop
Stop the server in case it’s running in the background.
278 279 280 281 282 283 284 |
# File 'lib/midb/server_controller.rb', line 278 def do_stop() # Is the server running? MIDB::Interface::Errors.die(:server_not_running) unless @config["status"] == :running @config["status"] = :asleep MIDB::Interface::Server.server_stopped() end |
#init ⇒ Object
$ midb
Decide the behavior of the server in function of the arguments.
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/midb/server_controller.rb', line 291 def init() # We should have at least one argument, which can be `run` or `serve` MIDB::Interface::Errors.die(:noargs) if @args.length < 1 # Load the config if File.file?(".midb.yaml") @config = YAML.load_file(".midb.yaml") else # If the file doesn't exist, we need to bootstrap MIDB::Interface::Errors.die(:bootstrap) if @args[0] != "help" && args[0] != "bootstrap" end case @args[0] # Command: help when "help" self.do_help() # Command: bootstrap when "bootstrap" self.do_bootstrap() # Command: set when "set" self.do_set() # Command: start when "start" self.do_start() # Command: serve # Serves a JSON file when "serve" self.do_serve() # Command: unserve # Stop serving a JSON file. when "unserve" self.do_unserve() # Command: stop # Stops the server. when "stop" self.do_stop() end end |
#save ⇒ Object
Method: save Saves config to .midb.yaml
341 342 343 344 345 |
# File 'lib/midb/server_controller.rb', line 341 def save() File.open(".midb.yaml", 'w') do |l| l.write @config.to_yaml end end |
#to_bool(text) ⇒ Object
Convert an on/off string to a boolean
37 38 39 40 41 42 43 44 45 |
# File 'lib/midb/server_controller.rb', line 37 def to_bool(text) if text == "on" || text == "true" || text == "yes" return true elsif text == "off" || text == "false" || text == "no" return false else return nil end end |