midb 1.0.5 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/midb +6 -3
- data/lib/midb/dbengine_model.rb +78 -74
- data/lib/midb/errors_view.rb +26 -27
- data/lib/midb/hooks.rb +13 -0
- data/lib/midb/security_controller.rb +33 -31
- data/lib/midb/server_controller.rb +228 -221
- data/lib/midb/server_model.rb +248 -232
- data/lib/midb/server_view.rb +97 -87
- data/lib/midb/serverengine_controller.rb +113 -105
- data/lib/midb.rb +2 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1800c9b95f115f1c96c363a245c6ba28fbc0541
|
4
|
+
data.tar.gz: 77177bd1d17209dd9cd304ae8a1259851cc93291
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3faa266d28b6965fe4b97e3b4e74cbdcb8b9f538ea76c5381e7d4a28ca614cd0f1704ec20038d9d4272e8a512f2315902bf466991c4edf9c38c42ae6e43c395b
|
7
|
+
data.tar.gz: a418745028a7f5770d66ae4a18ce7a327d5160aa39a1542f0cb5c5072f9413372c4c96d3351c61e3a0243563d7196b835ae240b0299b6e5548900989301b9aa7
|
data/bin/midb
CHANGED
@@ -4,11 +4,14 @@
|
|
4
4
|
# 08/31/15, unrar
|
5
5
|
require 'midb'
|
6
6
|
|
7
|
+
api = MIDB::API::Controller.new(ARGV)
|
8
|
+
api.init()
|
9
|
+
api.save()
|
7
10
|
# Pass the arguments to the controler, we don't want the action here ;-)
|
8
|
-
MIDB::ServerController.args = ARGV
|
11
|
+
#MIDB::ServerController.args = ARGV
|
9
12
|
|
10
13
|
# And start the server
|
11
|
-
MIDB::ServerController.init()
|
14
|
+
#MIDB::ServerController.init()
|
12
15
|
|
13
16
|
# Save data in case we didn't actually start the server but change the configuration
|
14
|
-
MIDB::ServerController.save()
|
17
|
+
#!!MIDB::ServerController.save()
|
data/lib/midb/dbengine_model.rb
CHANGED
@@ -1,91 +1,95 @@
|
|
1
|
-
require 'midb/server_controller'
|
2
1
|
require 'sqlite3'
|
3
2
|
require 'mysql2'
|
4
3
|
|
5
4
|
module MIDB
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
module API
|
6
|
+
# @author unrar
|
7
|
+
# This class handles engine-dependent database operations
|
8
|
+
class Dbengine
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
10
|
+
# @!attribute engine
|
11
|
+
# @return [Symbol] The database engine being used
|
12
|
+
# @!attribute host
|
13
|
+
# @return [String] Host name where the database is located
|
14
|
+
# @!attribute uname
|
15
|
+
# @return [String] Username for the database
|
16
|
+
# @!attribute pwd
|
17
|
+
# @return [String] Password for the database.
|
18
|
+
# @!attribute port
|
19
|
+
# @return [Fixnum] Port for the database
|
20
|
+
# @!attribute db
|
21
|
+
# @return [String] Name of the database
|
22
|
+
attr_accessor :engine, :host, :uname, :pwd, :port, :db
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
@
|
27
|
-
@
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
24
|
+
# Constructor - initializes the attributes with the configuration from ServerController
|
25
|
+
#
|
26
|
+
# @param cnf [Array<String>] The configuration array.
|
27
|
+
# @param db [String] Database name.
|
28
|
+
def initialize(cnf, db)
|
29
|
+
@engine = cnf["dbengine"]
|
30
|
+
@host = cnf["dbhost"]
|
31
|
+
@port = cnf["dbport"]
|
32
|
+
@uname = cnf["dbuser"]
|
33
|
+
@pwd = cnf["dbpassword"]
|
34
|
+
@db = db
|
35
|
+
end
|
33
36
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
37
|
+
# Connect to the specified database
|
38
|
+
#
|
39
|
+
# @return [SQLite3::Database, Mysql2::Client] A resource referencing to the database
|
40
|
+
def connect()
|
41
|
+
# Connect to an SQLite3 database
|
42
|
+
if @engine == :sqlite3
|
43
|
+
sq = SQLite3::Database.open("./db/#{@db}.db")
|
44
|
+
sq.results_as_hash = true
|
45
|
+
return sq
|
46
|
+
# Connect to a MySQL database
|
47
|
+
elsif @engine == :mysql
|
48
|
+
return Mysql2::Client.new(:host => @host, :username => @uname, :password => @pwd, :database => @db)
|
49
|
+
end
|
46
50
|
end
|
47
|
-
end
|
48
51
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
52
|
+
# Perform a query to the database.
|
53
|
+
#
|
54
|
+
# @param res [SQLite3::Database, Mysql2::Client] An existing database resource.
|
55
|
+
# @param query [String] The SQL query to be ran.
|
56
|
+
#
|
57
|
+
# @return [Array, Hash] Returns an array of hashes for SQLite3 or a hash for MySQL
|
58
|
+
def query(res, query)
|
59
|
+
if @engine == :sqlite3
|
60
|
+
return res.execute(query)
|
61
|
+
elsif @engine == :mysql
|
62
|
+
return res.query(query)
|
63
|
+
end
|
60
64
|
end
|
61
|
-
end
|
62
65
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
66
|
+
# Extract a field from a query, because different engines return different types (see #query)
|
67
|
+
#
|
68
|
+
# @param result [Array, Hash] The result of a query obtained via #query
|
69
|
+
# @param field [String] The name of the field to be extracted.
|
70
|
+
#
|
71
|
+
# @return [String, Fixnum] The field extracted from a query
|
72
|
+
def extract(result, field)
|
73
|
+
if @engine == :sqlite3
|
74
|
+
return result[0][field] || result[field]
|
75
|
+
elsif @engine == :mysql
|
76
|
+
result.each do |row|
|
77
|
+
return row[field]
|
78
|
+
end
|
75
79
|
end
|
76
80
|
end
|
77
|
-
end
|
78
81
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
82
|
+
# Returns the length of a result, because different engines return diferent types (see #query)
|
83
|
+
#
|
84
|
+
# @param result [Array, Hash] The result of a query obtained via #query
|
85
|
+
#
|
86
|
+
# @return [Fixnum] Length of the result.
|
87
|
+
def length(result)
|
88
|
+
if @engine == :sqlite3
|
89
|
+
return result.length
|
90
|
+
elsif @engine == :mysql
|
91
|
+
return result.count
|
92
|
+
end
|
89
93
|
end
|
90
94
|
end
|
91
95
|
end
|
data/lib/midb/errors_view.rb
CHANGED
@@ -1,31 +1,30 @@
|
|
1
|
-
require 'midb/server_controller'
|
2
|
-
|
3
1
|
module MIDB
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
2
|
+
module Interface
|
3
|
+
# A view that outputs errors.
|
4
|
+
class Errors
|
5
|
+
# Handles fatal errors that will cause the application to abrort.
|
6
|
+
#
|
7
|
+
# @param err [Symbol] The ID of the error that's to be reported.
|
8
|
+
def self.die(err)
|
9
|
+
errmsg = case err
|
10
|
+
when :noargs then "No command supplied. See `midb help`."
|
11
|
+
when :server_already_started then "The server has already been started and is running."
|
12
|
+
when :server_not_running then "The server isn't running."
|
13
|
+
when :server_error then "Error while starting server."
|
14
|
+
when :no_serves then "No files are being served. Try running `midb serve file.json`"
|
15
|
+
when :syntax then "Syntax error. See `midb help`"
|
16
|
+
when :file_404 then "File not found."
|
17
|
+
when :not_json then "Specified file isn't JSON!"
|
18
|
+
when :json_exists then "Specified file is already being served."
|
19
|
+
when :json_not_exists then "The JSON file isn't being served."
|
20
|
+
when :unsupported_engine then "The specified database engine isn't supported by midb."
|
21
|
+
when :already_project then "This directory already contains a midb project."
|
22
|
+
when :bootstrap then "midb hasn't been bootstraped in this folder. Run `midb bootstrap`."
|
23
|
+
when :no_help then "No help available for this command. See a list of commands with `midb help`."
|
24
|
+
else "Unknown error: #{err.to_s}"
|
25
|
+
end
|
26
|
+
abort("Fatal error: #{errmsg}")
|
27
|
+
end
|
29
28
|
end
|
30
29
|
end
|
31
30
|
end
|
data/lib/midb/hooks.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# ADDED in midb-2.0.0a by unrar #
|
2
|
+
#
|
3
|
+
# The "hooks" part of the MIDB::API module allows programmers to customize their API.
|
4
|
+
# Hooks are methods that are run in the API; overriding them allows you to run a custom API.
|
5
|
+
#
|
6
|
+
module MIDB
|
7
|
+
module API
|
8
|
+
class Hooks
|
9
|
+
def self.after_get_all_entries()
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -3,40 +3,42 @@ require 'base64'
|
|
3
3
|
require 'cgi'
|
4
4
|
|
5
5
|
module MIDB
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
module API
|
7
|
+
# Controller that handles API HMAC authentication.
|
8
|
+
#
|
9
|
+
# @note This will probably become a separate project soon.
|
10
|
+
class Security
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
# Checks if an HTTP header is the authorization one
|
13
|
+
#
|
14
|
+
# @deprecated It's no longer used but kept for historical reasons.
|
15
|
+
# @param header [String] A line of an HTTP header.
|
16
|
+
# @return [Boolean] Whether it's an auth header or not.
|
17
|
+
def self.is_auth?(header)
|
18
|
+
return header.split(":")[0].downcase == "authentication"
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
21
|
+
# Parses an authentication header so to get the HMAC digest.
|
22
|
+
#
|
23
|
+
# @param header [String] A line of an HTTP header (should have been checked
|
24
|
+
# to be an auth header)
|
25
|
+
# @return [String] The HMAC digest as a string.
|
26
|
+
def self.parse_auth(header)
|
27
|
+
return header.split(" ")[1]
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
30
|
+
# Checks if an HMAC digest is properly authenticated.
|
31
|
+
#
|
32
|
+
# @param header [String] A line of an HTTP header (see #parse_auth)
|
33
|
+
# @param params [String] The data passed via the HTTP request.
|
34
|
+
# @param key [String] The private API key.
|
35
|
+
#
|
36
|
+
# @return [Boolean] Whether the given digest matches the correct one or not.
|
37
|
+
def self.check?(header, params, key)
|
38
|
+
hmac = HMAC::SHA1.new(key)
|
39
|
+
hmac.update(params)
|
40
|
+
return self.parse_auth(header) == CGI.escape(Base64.encode64("#{hmac.digest}"))
|
41
|
+
end
|
40
42
|
end
|
41
43
|
end
|
42
44
|
end
|