server: switch from mysql to sqlite
This commit is contained in:
parent
b02832b8d3
commit
4d891f17e7
15
server.rb
15
server.rb
|
@ -12,7 +12,7 @@ sudo pacman -S ruby-sinatra ruby-webrick
|
||||||
pikaur -S ruby-mysql2
|
pikaur -S ruby-mysql2
|
||||||
=end
|
=end
|
||||||
require 'set'
|
require 'set'
|
||||||
require 'mysql2'
|
require 'sqlite3'
|
||||||
require 'json'
|
require 'json'
|
||||||
require 'sinatra'
|
require 'sinatra'
|
||||||
require 'uri'
|
require 'uri'
|
||||||
|
@ -23,8 +23,9 @@ require 'cgi'
|
||||||
DEBUG = false
|
DEBUG = false
|
||||||
# maximum number of parts returned
|
# maximum number of parts returned
|
||||||
PARTS_LIMIT = 100
|
PARTS_LIMIT = 100
|
||||||
# credentials for database
|
# database file
|
||||||
CREDENTIALS = "credentials.json"
|
DB_PATH = "partdb.db"
|
||||||
|
raise "DB file #{DB_PATH} does not exist" unless File.file? DB_PATH
|
||||||
# folder name for served pages
|
# folder name for served pages
|
||||||
PUBLIC = "public"
|
PUBLIC = "public"
|
||||||
# folder name for part attachments (in PUBLIC)
|
# folder name for part attachments (in PUBLIC)
|
||||||
|
@ -32,8 +33,6 @@ ATTACHMENTS = "attachments"
|
||||||
# port for this service
|
# port for this service
|
||||||
PORT = 4245
|
PORT = 4245
|
||||||
|
|
||||||
raise "database information #{CREDENTIALS} do not exist" unless File.file? CREDENTIALS
|
|
||||||
|
|
||||||
# open server
|
# open server
|
||||||
configure do
|
configure do
|
||||||
if DEBUG then
|
if DEBUG then
|
||||||
|
@ -63,10 +62,8 @@ before do
|
||||||
# all replies are only JSON
|
# all replies are only JSON
|
||||||
content_type 'application/json'
|
content_type 'application/json'
|
||||||
# open database
|
# open database
|
||||||
credentials = {}
|
@db = SQLite3::Database.new(DB_PATH)
|
||||||
JSON.parse(IO.read(CREDENTIALS)).each {|key,value| credentials[key.to_sym] = value}
|
@db.results_as_hash = true
|
||||||
Mysql2::Client.default_query_options.merge!(:as => :hash)
|
|
||||||
@db = Mysql2::Client.new(credentials)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
|
|
Loading…
Reference in New Issue