server: switch from mysql to sqlite

This commit is contained in:
King Kévin 2023-02-26 12:11:27 +01:00
parent b02832b8d3
commit 4d891f17e7
1 changed files with 6 additions and 9 deletions

View File

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