Class: MIDB::ServerController
- Inherits:
-
Object
- Object
- MIDB::ServerController
- Defined in:
- lib/midb/server_controller.rb
Overview
This controller controls the behavior of the midb server.
Class 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_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.
-
.do_unserve ⇒ Object
$ midb unserve.
-
.init ⇒ Object
$ midb.
-
.save ⇒ Object
Method: save Saves config to .midb.yaml.
Class 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_bootstrap ⇒ Object
$ midb bootstrap
Bootstrap a new midb project in the active directory.
73 74 75 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 73 def self.do_bootstrap() if File.file?(".midb.yaml") MIDB::ErrorsView.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::ServerView.info(:bootstrap) end end |
.do_help ⇒ Object
$ midb help
Show some help for either midb or a command.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/midb/server_controller.rb', line 49 def self.do_help() if @args.length > 1 case @args[1] when "bootstrap" MIDB::ServerView.help(:bootstrap) when "set" MIDB::ServerView.help(:set) when "start" MIDB::ServerView.help(:start) when "serve" MIDB::ServerView.help(:serve) when "unserve" MIDB::ServerView.help(:unserve) else MIDB::ErrorsView.die(:no_help) end else MIDB::ServerView.help(:list) end end |
.do_serve ⇒ Object
$ midb serve
Serve a JSON file
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/midb/server_controller.rb', line 182 def self.do_serve() # Check if there's a second argument MIDB::ErrorsView.die(:syntax) if @args.length < 2 # Is the server running? It shouldn't MIDB::ErrorsView.die(:server_already_started) if @config["status"] == :running # Is there such file as @args[1]? MIDB::ErrorsView.die(:file_404) unless File.file?("./json/" + @args[1]) # Is the file a JSON file? MIDB::ErrorsView.die(:not_json) unless File.extname(@args[1]) == ".json" # Is the file already loaded? MIDB::ErrorsView.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::ServerView.show_serving() end |
.do_set ⇒ Object
$ midb set
Set the config for the project. Check syntax
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/midb/server_controller.rb', line 101 def self.do_set() MIDB::ErrorsView.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::ErrorsView.die(:unsupported_engine) @config["dbengine"] = :sqlite3 end end MIDB::ServerView.out_config(:dbengine) when "host" @config["dbhost"] = setter if set MIDB::ServerView.out_config(:dbhost) when "port" @config["dbport"] = setter if set MIDB::ServerView.out_config(:dbport) when "user" @config["dbuser"] = setter if set MIDB::ServerView.out_config(:dbuser) when "password" @config["dbpassword"] = setter if set MIDB::ServerView.out_config(:dbpassword) else MIDB::ErrorsView.die(:synax) end when "api" case subcmd when "key" @config["apikey"] = setter if set MIDB::ServerView.out_config(:apikey) end else MIDB::ErrorsView.die(:syntax) end end |
.do_start ⇒ Object
$ midb start
Start the server (call the loop)
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 |
# File 'lib/midb/server_controller.rb', line 153 def self.do_start() # Check syntax MIDB::ErrorsView.die(:syntax) if @args.length < 2 MIDB::ErrorsView.die(:syntax) if @args[1].split(":")[0] != "db" # Is the server already started? MIDB::ErrorsView.die(:server_already_started) if @config["status"] == :running # Are any files being served? MIDB::ErrorsView.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 if MIDB::ServerEngineController.start(@port) @config["status"] = :running MIDB::ServerView.success() else MIDB::ErrorsView.die(:server_error) end end |
.do_stop ⇒ Object
$ midb stop
Stop the server in case it’s running in the background.
218 219 220 221 222 223 224 |
# File 'lib/midb/server_controller.rb', line 218 def self.do_stop() # Is the server running? MIDB::ErrorsView.die(:server_not_running) unless @config["status"] == :running @config["status"] = :asleep MIDB::ServerView.server_stopped() end |
.do_unserve ⇒ Object
$ midb unserve
Stop serving a JSON file
202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/midb/server_controller.rb', line 202 def self.do_unserve() # Check if there's a second argument MIDB::ErrorsView.die(:syntax) if @args.length < 2 # Is the server running? It shouldn't MIDB::ErrorsView.die(:server_already_started) if @config["status"] == :running # Is the file already loaded? MIDB::ErrorsView.die(:json_not_exists) unless @config["serves"].include? @args[1] # Delete it! @config["serves"].delete @args[1] MIDB::ServerView.show_serving() end |
.init ⇒ Object
$ midb
Decide the behavior of the server in function of the arguments.
231 232 233 234 235 236 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 |
# File 'lib/midb/server_controller.rb', line 231 def self.init() # We should have at least one argument, which can be `run` or `serve` MIDB::ErrorsView.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::ErrorsView.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
281 282 283 284 285 |
# File 'lib/midb/server_controller.rb', line 281 def self.save() File.open(".midb.yaml", 'w') do |l| l.write @config.to_yaml end end |