Class: MIDB::ServerController

Inherits:
Object
  • Object
show all
Defined in:
lib/midb/server_controller.rb

Overview

This controller controls the behavior of the midb server.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.argsArray<String>

Returns Arguments passed to the binary.

Returns:

  • (Array<String>)

    Arguments passed to the binary.



28
29
30
# File 'lib/midb/server_controller.rb', line 28

def args
  @args
end

.configHash

Returns Contains the project’s configuration, saved in .midb.yaml.

Returns:

  • (Hash)

    Contains the project’s configuration, saved in .midb.yaml



28
# File 'lib/midb/server_controller.rb', line 28

attr_accessor :args, :config, :db, :http_status, :port

.dbString

Returns Database name (if SQLite is the engine, file name without extension).

Returns:

  • (String)

    Database name (if SQLite is the engine, file name without extension)



28
# File 'lib/midb/server_controller.rb', line 28

attr_accessor :args, :config, :db, :http_status, :port

.http_statusString

Returns HTTP status code and string representation for the header.

Returns:

  • (String)

    HTTP status code and string representation for the header



28
# File 'lib/midb/server_controller.rb', line 28

attr_accessor :args, :config, :db, :http_status, :port

.portFixnum

Returns Port where the server will listen.

Returns:

  • (Fixnum)

    Port where the server will listen.



28
# File 'lib/midb/server_controller.rb', line 28

attr_accessor :args, :config, :db, :http_status, :port

Class Method Details

.initObject

Decide the server’s behavior depending on the arguments.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
131
132
133
134
135
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/midb/server_controller.rb', line 42

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
  # Shows the help
  when "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

  # Command: bootstrap
  # Create config file and initial directories
  when "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

  # Command: set
  # Sets configuration factors.
  when "set"
    # Check syntax
    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


  # Command: start
  # Starts the server
  when "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

    if self.start(@port)
      @config["status"] = :running
      MIDB::ServerView.success()
    else
      MIDB::ErrorsView.die(:server_error)
    end

  # Command: serve
  # Serves a JSON file
  when "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()

  # Command: unserve
  # Stop serving a JSON file.
  when "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()

  # Command: stop
  # Stops the server.
  when "stop"
    # Is the server running?
    MIDB::ErrorsView.die(:server_not_running) unless @config["status"] == :running

    @config["status"] = :asleep
    MIDB::ServerView.server_stopped()
  end
end

.parse_request(req) ⇒ Object

Method: parse_request Parses an HTTP requests and returns an array [method, uri]



335
336
337
# File 'lib/midb/server_controller.rb', line 335

def self.parse_request(req)
  [req.split(" ")[0], req.split(" ")[1]]
end

.saveObject

Method: save Saves config to .midb.yaml



341
342
343
344
345
# File 'lib/midb/server_controller.rb', line 341

def self.save()
  File.open(".midb.yaml", 'w') do |l|
    l.write @config.to_yaml
  end
end

.start(port = 8081) ⇒ Object

Method: start Starts the server on the given port (default: 8080)



226
227
228
229
230
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
278
279
280
281
282
283
284
285
286
287
288
289
290
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
# File 'lib/midb/server_controller.rb', line 226

def self.start(port=8081)
  serv = TCPServer.new("localhost", port)
  MIDB::ServerView.info(:start, port)

  # Manage the requests
  loop do
    socket = serv.accept
    MIDB::ServerView.info(:incoming_request, socket.addr[3])

    request = self.parse_request(socket.gets)

    # Get a hash with the headers
    headers = {}
    while line = socket.gets.split(' ', 2)
      break if line[0] == "" 
      headers[line[0].chop] = line[1].strip
    end
    data = socket.read(headers["Content-Length"].to_i)


    MIDB::ServerView.info(:request, request)
    response_json = Hash.new()

    # Endpoint syntax: ["", FILE, ID, (ACTION)]
    endpoint = request[1].split("/")
    ep_file = endpoint[1]

    method = request[0]
    endpoints = [] # Valid endpoints

    # Load the JSON served files
    @config["serves"].each do |js|
      # The filename is a valid endpoint
      endpoints.push File.basename(js, ".*")
    end

    # Load the endpoints
    found = false
    endpoints.each do |ep|
      if ep_file == ep
        found = true
        MIDB::ServerView.info(:match_json, ep)
        # Analyze the request and pass it to the model
        if method == "GET"
          case endpoint.length
          when 2
            # No ID has been specified. Return all the entries
            # Pass it to the model and get the JSON
            response_json = MIDB::ServerModel.get_all_entries(@db, ep).to_json
          when 3
            # An ID has been specified. Should it exist, return all of its entries.
            response_json = MIDB::ServerModel.get_entries(@db, ep, endpoint[2]).to_json
          end
        else
          # An action has been specified. We're going to need HTTP authentification here.
          MIDB::ServerView.info(:auth_required)

          if (not headers.has_key? "Authentication") ||
             (not MIDB::SecurityController.check?(headers["Authentication"], data, @config["apikey"]))
            @http_status = "401 Unauthorized"
            response_json = MIDB::ServerView.json_error(401, "Unauthorized").to_json
            MIDB::ServerView.info(:no_auth)

          else
            MIDB::ServerView.info(:auth_success)
            if method == "POST"
              response_json = MIDB::ServerModel.post(@db, ep, data).to_json
            else
              if endpoint.length >= 3
                if method == "DELETE"
                  response_json = MIDB::ServerModel.delete(@db, ep, endpoint[2]).to_json 
                elsif method == "PUT"
                  response_json = MIDB::ServerModel.put(@db, ep, endpoint[2], data).to_json
                end
              else
                @http_status = "404 Not Found"
                response_json = MIDB::ServerView.json_error(404, "Must specify an ID.").to_json
              end
            end
          end
        end
        MIDB::ServerView.info(:response, response_json)
        # Return the results via HTTP
        socket.print "HTTP/1.1 #{@http_status}\r\n" +
                    "Content-Type: text/json\r\n" +
                    "Content-Length: #{response_json.size}\r\n" +
                    "Connection: close\r\n"
        socket.print "\r\n"
        socket.print response_json
        socket.print "\r\n"
        MIDB::ServerView.info(:success)
      end
    end
    unless found
      MIDB::ServerView.info(:not_found)
      response = MIDB::ServerView.json_error(404, "Invalid API endpoint.").to_json

      socket.print "HTTP/1.1 404 Not Found\r\n" +
                   "Content-Type: text/json\r\n" +
                   "Content-Length: #{response.size}\r\n" +
                   "Connection: close\r\n"
      socket.print "\r\n"
      socket.print response
    end
  end
end