Class: MIDB::API::Engine
- Inherits:
-
Object
- Object
- MIDB::API::Engine
- Defined in:
- lib/midb/serverengine_controller.rb
Overview
This class handles runs the server engine using sockets and a loop.
Instance Attribute Summary collapse
-
#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.
Instance Method Summary collapse
-
#initialize(db, stat, cnf) ⇒ Engine
constructor
Constructor.
-
#parse_request(req) ⇒ Object
Method: parse_request Parses an HTTP requests and returns an array [method, uri].
-
#start(port = 8081) ⇒ Object
Starts the server on a given port (default: 8081).
Constructor Details
#initialize(db, stat, cnf) ⇒ Engine
Constructor
32 33 34 35 36 |
# File 'lib/midb/serverengine_controller.rb', line 32 def initialize(db, stat, cnf) @config = cnf @db = db @http_status = stat end |
Instance Attribute Details
#config ⇒ Hash
Returns Contains the project’s configuration, saved in .midb.yaml.
25 26 27 |
# File 'lib/midb/serverengine_controller.rb', line 25 def config @config end |
#db ⇒ String
Returns Database name (if SQLite is the engine, file name without extension).
25 |
# File 'lib/midb/serverengine_controller.rb', line 25 attr_accessor :config, :db, :http_status |
#http_status ⇒ String
Returns HTTP status code and string representation for the header.
25 |
# File 'lib/midb/serverengine_controller.rb', line 25 attr_accessor :config, :db, :http_status |
Instance Method Details
#parse_request(req) ⇒ Object
Method: parse_request Parses an HTTP requests and returns an array [method, uri]
152 153 154 |
# File 'lib/midb/serverengine_controller.rb', line 152 def parse_request(req) [req.split(" ")[0], req.split(" ")[1]] end |
#start(port = 8081) ⇒ Object
Starts the server on a given port (default: 8081)
41 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 |
# File 'lib/midb/serverengine_controller.rb', line 41 def start(port=8081) serv = TCPServer.new("localhost", port) MIDB::Interface::Server.info(:start, port) # Manage the requests loop do socket = serv.accept MIDB::Interface::Server.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::Interface::Server.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::Interface::Server.info(:match_json, ep) # Create the model dbop = MIDB::API::Model.new(ep, @db, self) # 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 = dbop.get_all_entries().to_json when 3 # An ID has been specified. Should it exist, return all of its entries. response_json = dbop.get_entries(endpoint[2]).to_json end else # An action has been specified. We're going to need HTTP authentification here. MIDB::Interface::Server.info(:auth_required) if (not headers.has_key? "Authentication") || (not MIDB::API::Security.check?(headers["Authentication"], data, @config["apikey"])) @http_status = "401 Unauthorized" response_json = MIDB::Interface::Server.json_error(401, "Unauthorized").to_json MIDB::Interface::Server.info(:no_auth) else MIDB::Interface::Server.info(:auth_success) if method == "POST" response_json = dbop.post(data).to_json else if endpoint.length >= 3 if method == "DELETE" response_json = dbop.delete(endpoint[2]).to_json elsif method == "PUT" response_json = dbop.put(endpoint[2], data).to_json end else @http_status = "404 Not Found" response_json = MIDB::Interface::Server.json_error(404, "Must specify an ID.").to_json end end end end MIDB::Interface::Server.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::Interface::Server.info(:success) end end unless found MIDB::Interface::Server.info(:not_found) response = MIDB::Interface::Server.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 |