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).
-
#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) ⇒ Controller
constructor
Constructor for this controller.
-
#save ⇒ Object
Method: save Saves config to .midb.yaml.
Constructor Details
#initialize(args) ⇒ Controller
Constructor for this controller.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/midb/server_controller.rb', line 34 def initialize(args) # Default values # # @see #http_status # @see #args # @see #config # @see #port @http_status = "200 OK" @args = args @config = Hash.new() @port = 8081 end |
Instance Attribute Details
#args ⇒ Array<String>
Returns Arguments passed to the binary.
29 30 31 |
# File 'lib/midb/server_controller.rb', line 29 def args @args end |
#config ⇒ Hash
Returns Contains the project’s configuration, saved in .midb.yaml.
29 |
# File 'lib/midb/server_controller.rb', line 29 attr_accessor :args, :config, :db, :http_status, :port |
#db ⇒ String
Returns Database name (if SQLite is the engine, file name without extension).
29 |
# File 'lib/midb/server_controller.rb', line 29 attr_accessor :args, :config, :db, :http_status, :port |
#http_status ⇒ String
Returns HTTP status code and string representation for the header.
29 |
# File 'lib/midb/server_controller.rb', line 29 attr_accessor :args, :config, :db, :http_status, :port |
#port ⇒ Fixnum
Returns Port where the server will listen.
29 |
# File 'lib/midb/server_controller.rb', line 29 attr_accessor :args, :config, :db, :http_status, :port |
Class Method Details
.do_unserve ⇒ Object
$ midb unserve
Stop serving a JSON file
208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/midb/server_controller.rb', line 208 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.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/midb/server_controller.rb', line 78 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 @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" 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.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/midb/server_controller.rb', line 54 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
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/midb/server_controller.rb', line 188 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
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/midb/server_controller.rb', line 106 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) end else MIDB::Interface::Errors.die(:syntax) end end |
#do_start ⇒ Object
$ midb start
Start the server (call the loop)
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 |
# File 'lib/midb/server_controller.rb', line 158 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 eng = MIDB::API::Engine.new(@db, @http_status, @config) 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.
224 225 226 227 228 229 230 |
# File 'lib/midb/server_controller.rb', line 224 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.
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/midb/server_controller.rb', line 237 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
287 288 289 290 291 |
# File 'lib/midb/server_controller.rb', line 287 def save() File.open(".midb.yaml", 'w') do |l| l.write @config.to_yaml end end |