singletons

This commit is contained in:
Jörn-Michael Miehe 2019-02-19 19:57:22 +01:00
parent 7221dd31af
commit 60edcf86b7
9 changed files with 38 additions and 39 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
**/node_modules **/node_modules
**/fftcg.db

View file

@ -1,7 +1,8 @@
# node libraries # node libraries
bcrypt = (require 'bcrypt') bcrypt = (require 'bcrypt')
sqlite3 = (require 'sqlite3').verbose()
logger = (require 'logging').default 'db' logger = (require 'logging').default 'db'
path = (require 'path')
sqlite3 = (require 'sqlite3').verbose()
# bruteforce countermeasure # bruteforce countermeasure
saltRounds = 13 saltRounds = 13
@ -221,4 +222,4 @@ FFTCGDB::delDeck = (deckID) ->
resolve deckID resolve deckID
module.exports = FFTCGDB module.exports = new FFTCGDB path.resolve(__dirname, 'fftcg.db'), true

View file

View file

@ -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 = [ module.exports = [
# test # test
(require './routes/test') SHARE (require './routes/test')
# register user # register user
(require './routes/user/register') SHARE (require './routes/user/register')
# log in user # log in user
(require './routes/user/login') SHARE (require './routes/user/login')
] ]

View file

@ -1,4 +1,4 @@
module.exports = (SHARE) -> module.exports =
url: '/test' url: '/test'
method: 'POST' method: 'POST'
handler: (request, reply) -> handler: (request, reply) ->

View file

@ -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' url: '/user/login'
method: 'POST' method: 'POST'
schema: SHARE.jsonschemas.user schema: (require './user.schema')
handler: (request, reply) -> handler: (request, reply) ->
session_id = request.cookies.session ? ''
new Promise (resolve) -> new Promise (resolve) ->
SHARE.session.check session_id session_id = request.cookies.session ? ''
session.check session_id
.then (user) -> .then (user) ->
# active session found # active session found
SHARE.logger.debug "OK '#{user.login}' resumed session '#{session_id}'" logger.debug "OK '#{user.login}' resumed session '#{session_id}'"
resolve user resolve user
.catch -> .catch ->
SHARE.fftcgdb.login request.body.login, request.body.password fftcgdb.login request.body.login, request.body.password
.then (user) -> .then (user) ->
# login successful: start new session # login successful: start new session
SHARE.session.start user logger.info "OK '#{request.body.login}'"
session.start user
.then (session_id) -> .then (session_id) ->
logger.info "OK '#{user.login}' created session '#{session_id}'"
reply.setCookie 'session', session_id reply.setCookie 'session', session_id
resolve user resolve user
.catch (err) -> .catch (err) ->
# login failed # login failed
SHARE.logger.info "FAIL login for '#{request.body.login}'" logger.info "FAIL '#{request.body.login}'"
reply.send reply.send
success: false success: false
err: err err: err

View file

@ -1,19 +1,24 @@
module.exports = (SHARE) -> logger = (require 'logging').default 'register'
# fftcg.db (persistent data)
fftcgdb = (require '../../db')
module.exports =
url: '/user/register' url: '/user/register'
method: 'POST' method: 'POST'
schema: SHARE.jsonschemas.user schema: (require './user.schema')
handler: (request, reply) -> handler: (request, reply) ->
SHARE.fftcgdb.register(request.body.login, request.body.password) fftcgdb.register(request.body.login, request.body.password)
.then (user) -> .then (user) ->
SHARE.logger.info "OK registration '#{request.body.login}'" logger.info "OK '#{request.body.login}'"
reply.send reply.send
success: true success: true
user: user user: user
return return
.catch (err) -> .catch (err) ->
SHARE.logger.debug "FAIL registration '#{request.body.login}'" logger.debug "FAIL '#{request.body.login}'"
reply.send reply.send
success: false success: false
err: err err: err

View file

@ -1,4 +1,4 @@
module.exports = -> module.exports =
body: body:
login: type: 'string' login: type: 'string'
password: type: 'string' password: type: 'string'

View file

@ -30,6 +30,7 @@ class FFTCGSESSION
# push (hash, data) into DB for the configured timespan # push (hash, data) into DB for the configured timespan
@db.setex digest, EXPIRY.login, (JSON.stringify data), (err) -> @db.setex digest, EXPIRY.login, (JSON.stringify data), (err) ->
logger.info "OK '#{digest}' created"
resolve digest resolve digest
check: (digest) -> check: (digest) ->
@ -41,6 +42,7 @@ class FFTCGSESSION
else else
@db.get digest, (err, res) -> @db.get digest, (err, res) ->
logger.info "OK '#{digest}' resumed"
resolve JSON.parse res resolve JSON.parse res
module.exports = FFTCGSESSION module.exports = new FFTCGSESSION