singletons
This commit is contained in:
parent
7221dd31af
commit
60edcf86b7
9 changed files with 38 additions and 39 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
**/node_modules
|
||||
**/fftcg.db
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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')
|
||||
]
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
module.exports = (SHARE) ->
|
||||
module.exports =
|
||||
url: '/test'
|
||||
method: 'POST'
|
||||
handler: (request, reply) ->
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
module.exports = ->
|
||||
module.exports =
|
||||
body:
|
||||
login: type: 'string'
|
||||
password: type: 'string'
|
|
@ -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
|
||||
|
|
Reference in a new issue