From 60edcf86b770d3e0ab7792e952c8d6ffd6f222c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Tue, 19 Feb 2019 19:57:22 +0100 Subject: [PATCH] singletons --- .gitignore | 1 + backend/db.coffee | 5 ++-- backend/fftcg.db | 0 backend/routes.coffee | 22 +++------------- backend/routes/test.coffee | 2 +- backend/routes/user/login.coffee | 26 ++++++++++++------- backend/routes/user/register.coffee | 15 +++++++---- .../{schema => user}/user.schema.coffee | 2 +- backend/session.coffee | 4 ++- 9 files changed, 38 insertions(+), 39 deletions(-) delete mode 100644 backend/fftcg.db rename backend/routes/{schema => user}/user.schema.coffee (96%) diff --git a/.gitignore b/.gitignore index cf70988..8380c6b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ **/node_modules +**/fftcg.db diff --git a/backend/db.coffee b/backend/db.coffee index 1c9ca8f..edf11bd 100644 --- a/backend/db.coffee +++ b/backend/db.coffee @@ -1,7 +1,8 @@ # node libraries bcrypt = (require 'bcrypt') -sqlite3 = (require 'sqlite3').verbose() logger = (require 'logging').default 'db' +path = (require 'path') +sqlite3 = (require 'sqlite3').verbose() # bruteforce countermeasure saltRounds = 13 @@ -221,4 +222,4 @@ FFTCGDB::delDeck = (deckID) -> resolve deckID -module.exports = FFTCGDB +module.exports = new FFTCGDB path.resolve(__dirname, 'fftcg.db'), true diff --git a/backend/fftcg.db b/backend/fftcg.db deleted file mode 100644 index e69de29..0000000 diff --git a/backend/routes.coffee b/backend/routes.coffee index 8979f03..d0419e6 100644 --- a/backend/routes.coffee +++ b/backend/routes.coffee @@ -1,24 +1,8 @@ -# local libraries -# (require 'debug').enable 'routes' -path = (require 'path') - -# shared libraries -SHARE = - logger: (require 'logging').default 'routes' - jsonschemas: - user: (require './routes/schema/user.schema')() - - # fftcg.db (persistent data) - fftcgdb: new (require './db') path.resolve(__dirname, 'fftcg.db'), true - # session storage (volatile data) - session: new (require './session') - - module.exports = [ # test - (require './routes/test') SHARE + (require './routes/test') # register user - (require './routes/user/register') SHARE + (require './routes/user/register') # log in user - (require './routes/user/login') SHARE + (require './routes/user/login') ] diff --git a/backend/routes/test.coffee b/backend/routes/test.coffee index e73dddb..325ecc0 100644 --- a/backend/routes/test.coffee +++ b/backend/routes/test.coffee @@ -1,4 +1,4 @@ -module.exports = (SHARE) -> +module.exports = url: '/test' method: 'POST' handler: (request, reply) -> diff --git a/backend/routes/user/login.coffee b/backend/routes/user/login.coffee index 1dcf365..a288e4b 100644 --- a/backend/routes/user/login.coffee +++ b/backend/routes/user/login.coffee @@ -1,31 +1,37 @@ -module.exports = (SHARE) -> +logger = (require 'logging').default 'login' + +# session storage (volatile data) +session = (require '../../session') +# fftcg.db (persistent data) +fftcgdb = (require '../../db') + +module.exports = url: '/user/login' method: 'POST' - schema: SHARE.jsonschemas.user + schema: (require './user.schema') handler: (request, reply) -> - session_id = request.cookies.session ? '' - new Promise (resolve) -> - SHARE.session.check session_id + session_id = request.cookies.session ? '' + session.check session_id .then (user) -> # active session found - SHARE.logger.debug "OK '#{user.login}' resumed session '#{session_id}'" + logger.debug "OK '#{user.login}' resumed session '#{session_id}'" resolve user .catch -> - SHARE.fftcgdb.login request.body.login, request.body.password + fftcgdb.login request.body.login, request.body.password .then (user) -> # login successful: start new session - SHARE.session.start user + logger.info "OK '#{request.body.login}'" + session.start user .then (session_id) -> - logger.info "OK '#{user.login}' created session '#{session_id}'" reply.setCookie 'session', session_id resolve user .catch (err) -> # login failed - SHARE.logger.info "FAIL login for '#{request.body.login}'" + logger.info "FAIL '#{request.body.login}'" reply.send success: false err: err diff --git a/backend/routes/user/register.coffee b/backend/routes/user/register.coffee index 39a70c7..9b3178d 100644 --- a/backend/routes/user/register.coffee +++ b/backend/routes/user/register.coffee @@ -1,19 +1,24 @@ -module.exports = (SHARE) -> +logger = (require 'logging').default 'register' + +# fftcg.db (persistent data) +fftcgdb = (require '../../db') + +module.exports = url: '/user/register' method: 'POST' - schema: SHARE.jsonschemas.user + schema: (require './user.schema') handler: (request, reply) -> - SHARE.fftcgdb.register(request.body.login, request.body.password) + fftcgdb.register(request.body.login, request.body.password) .then (user) -> - SHARE.logger.info "OK registration '#{request.body.login}'" + logger.info "OK '#{request.body.login}'" reply.send success: true user: user return .catch (err) -> - SHARE.logger.debug "FAIL registration '#{request.body.login}'" + logger.debug "FAIL '#{request.body.login}'" reply.send success: false err: err diff --git a/backend/routes/schema/user.schema.coffee b/backend/routes/user/user.schema.coffee similarity index 96% rename from backend/routes/schema/user.schema.coffee rename to backend/routes/user/user.schema.coffee index 0c05989..1624546 100644 --- a/backend/routes/schema/user.schema.coffee +++ b/backend/routes/user/user.schema.coffee @@ -1,4 +1,4 @@ -module.exports = -> +module.exports = body: login: type: 'string' password: type: 'string' diff --git a/backend/session.coffee b/backend/session.coffee index afcc7ab..fbfa336 100644 --- a/backend/session.coffee +++ b/backend/session.coffee @@ -30,6 +30,7 @@ class FFTCGSESSION # push (hash, data) into DB for the configured timespan @db.setex digest, EXPIRY.login, (JSON.stringify data), (err) -> + logger.info "OK '#{digest}' created" resolve digest check: (digest) -> @@ -41,6 +42,7 @@ class FFTCGSESSION else @db.get digest, (err, res) -> + logger.info "OK '#{digest}' resumed" resolve JSON.parse res -module.exports = FFTCGSESSION +module.exports = new FFTCGSESSION