routes maintainability
This commit is contained in:
parent
e78570f298
commit
d0180f3b38
5 changed files with 113 additions and 111 deletions
|
@ -1,118 +1,24 @@
|
||||||
# node libraries
|
# local libraries
|
||||||
(require 'debug').enable 'routes'
|
# (require 'debug').enable 'routes'
|
||||||
logger = (require 'logging').default 'routes'
|
|
||||||
path = (require 'path')
|
path = (require 'path')
|
||||||
|
|
||||||
# my libraries
|
# shared libraries
|
||||||
FFTCGDB = (require './db')
|
SHARE =
|
||||||
FFTCGSESSION = (require './session')
|
logger: (require 'logging').default 'routes'
|
||||||
|
jsonschemas:
|
||||||
|
user: (require './routes/schema/user.schema')()
|
||||||
|
|
||||||
# open fftcg.db (persistent data)
|
# fftcg.db (persistent data)
|
||||||
fftcgdb = new FFTCGDB path.resolve(__dirname, 'fftcg.db'), true
|
fftcgdb: new (require './db') path.resolve(__dirname, 'fftcg.db'), true
|
||||||
# open session storage (volatile data)
|
# session storage (volatile data)
|
||||||
session = new FFTCGSESSION
|
session: new (require './session')
|
||||||
|
|
||||||
JSONSCHEMA =
|
|
||||||
user: ->
|
|
||||||
body:
|
|
||||||
login: type: 'string'
|
|
||||||
password: type: 'string'
|
|
||||||
|
|
||||||
response:
|
|
||||||
200:
|
|
||||||
type: 'object'
|
|
||||||
required: ['success']
|
|
||||||
properties:
|
|
||||||
success: type: 'boolean'
|
|
||||||
err: type: 'string'
|
|
||||||
user:
|
|
||||||
type: 'object'
|
|
||||||
required: ['user', 'login']
|
|
||||||
properties:
|
|
||||||
user: type: 'number'
|
|
||||||
login: type: 'string'
|
|
||||||
# user is required iff success
|
|
||||||
# err is required otherwise
|
|
||||||
if:
|
|
||||||
properties:
|
|
||||||
success:
|
|
||||||
const: true
|
|
||||||
then:
|
|
||||||
required: ['user']
|
|
||||||
else:
|
|
||||||
required: ['err']
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = [
|
module.exports = [
|
||||||
# test
|
# test
|
||||||
url: '/test'
|
(require './routes/test') SHARE
|
||||||
method: 'POST'
|
|
||||||
handler: (request, reply) ->
|
|
||||||
logger.info 'Cookies', request.cookies
|
|
||||||
logger.info 'Body', request.body
|
|
||||||
logger.info 'Query', request.query
|
|
||||||
logger.info 'Params', request.params
|
|
||||||
|
|
||||||
reply.setCookie 'foo', 'foo'
|
|
||||||
reply.send
|
|
||||||
hello: 'world'
|
|
||||||
,
|
|
||||||
# register user
|
# register user
|
||||||
url: '/user/register'
|
(require './routes/user/register') SHARE
|
||||||
method: 'POST'
|
|
||||||
schema: JSONSCHEMA.user()
|
|
||||||
|
|
||||||
handler: (request, reply) ->
|
|
||||||
fftcgdb.register(request.body.login, request.body.password)
|
|
||||||
.then (user) ->
|
|
||||||
logger.info "OK registration '#{request.body.login}'"
|
|
||||||
reply.send
|
|
||||||
success: true
|
|
||||||
user: user
|
|
||||||
return
|
|
||||||
|
|
||||||
.catch (err) ->
|
|
||||||
logger.debug "FAIL registration '#{request.body.login}'"
|
|
||||||
reply.send
|
|
||||||
success: false
|
|
||||||
err: err
|
|
||||||
,
|
|
||||||
# log in user
|
# log in user
|
||||||
url: '/user/login'
|
(require './routes/user/login') SHARE
|
||||||
method: 'POST'
|
|
||||||
schema: JSONSCHEMA.user()
|
|
||||||
|
|
||||||
handler: (request, reply) ->
|
|
||||||
session_id = request.cookies.session ? ''
|
|
||||||
|
|
||||||
new Promise (resolve) ->
|
|
||||||
session.check session_id
|
|
||||||
.then (user) ->
|
|
||||||
# active session found
|
|
||||||
logger.debug "OK '#{user.login}' resumed session '#{session_id}'"
|
|
||||||
resolve user
|
|
||||||
|
|
||||||
.catch ->
|
|
||||||
fftcgdb.login request.body.login, request.body.password
|
|
||||||
.then (user) ->
|
|
||||||
# login successful: start new session
|
|
||||||
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
|
|
||||||
logger.info "FAIL login for '#{request.body.login}'"
|
|
||||||
reply.send
|
|
||||||
success: false
|
|
||||||
err: err
|
|
||||||
|
|
||||||
.then (user) ->
|
|
||||||
# tell about the user who logged in
|
|
||||||
reply.send
|
|
||||||
success: true
|
|
||||||
user: user
|
|
||||||
,
|
|
||||||
]
|
]
|
||||||
|
|
28
backend/routes/schema/user.schema.coffee
Normal file
28
backend/routes/schema/user.schema.coffee
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
module.exports = ->
|
||||||
|
body:
|
||||||
|
login: type: 'string'
|
||||||
|
password: type: 'string'
|
||||||
|
|
||||||
|
response:
|
||||||
|
200:
|
||||||
|
type: 'object'
|
||||||
|
required: ['success']
|
||||||
|
properties:
|
||||||
|
success: type: 'boolean'
|
||||||
|
err: type: 'string'
|
||||||
|
user:
|
||||||
|
type: 'object'
|
||||||
|
required: ['user', 'login']
|
||||||
|
properties:
|
||||||
|
user: type: 'number'
|
||||||
|
login: type: 'string'
|
||||||
|
# user is required iff success
|
||||||
|
# err is required otherwise
|
||||||
|
if:
|
||||||
|
properties:
|
||||||
|
success:
|
||||||
|
const: true
|
||||||
|
then:
|
||||||
|
required: ['user']
|
||||||
|
else:
|
||||||
|
required: ['err']
|
12
backend/routes/test.coffee
Normal file
12
backend/routes/test.coffee
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
module.exports = (SHARE) ->
|
||||||
|
url: '/test'
|
||||||
|
method: 'POST'
|
||||||
|
handler: (request, reply) ->
|
||||||
|
logger.info 'Cookies', request.cookies
|
||||||
|
logger.info 'Body', request.body
|
||||||
|
logger.info 'Query', request.query
|
||||||
|
logger.info 'Params', request.params
|
||||||
|
|
||||||
|
reply.setCookie 'foo', 'foo'
|
||||||
|
reply.send
|
||||||
|
hello: 'world'
|
37
backend/routes/user/login.coffee
Normal file
37
backend/routes/user/login.coffee
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
module.exports = (SHARE) ->
|
||||||
|
url: '/user/login'
|
||||||
|
method: 'POST'
|
||||||
|
schema: SHARE.jsonschemas.user
|
||||||
|
|
||||||
|
handler: (request, reply) ->
|
||||||
|
session_id = request.cookies.session ? ''
|
||||||
|
|
||||||
|
new Promise (resolve) ->
|
||||||
|
SHARE.session.check session_id
|
||||||
|
.then (user) ->
|
||||||
|
# active session found
|
||||||
|
SHARE.logger.debug "OK '#{user.login}' resumed session '#{session_id}'"
|
||||||
|
resolve user
|
||||||
|
|
||||||
|
.catch ->
|
||||||
|
SHARE.fftcgdb.login request.body.login, request.body.password
|
||||||
|
.then (user) ->
|
||||||
|
# login successful: start new session
|
||||||
|
SHARE.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}'"
|
||||||
|
reply.send
|
||||||
|
success: false
|
||||||
|
err: err
|
||||||
|
|
||||||
|
.then (user) ->
|
||||||
|
# tell about the user who logged in
|
||||||
|
reply.send
|
||||||
|
success: true
|
||||||
|
user: user
|
19
backend/routes/user/register.coffee
Normal file
19
backend/routes/user/register.coffee
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
module.exports = (SHARE) ->
|
||||||
|
url: '/user/register'
|
||||||
|
method: 'POST'
|
||||||
|
schema: SHARE.jsonschemas.user
|
||||||
|
|
||||||
|
handler: (request, reply) ->
|
||||||
|
SHARE.fftcgdb.register(request.body.login, request.body.password)
|
||||||
|
.then (user) ->
|
||||||
|
SHARE.logger.info "OK registration '#{request.body.login}'"
|
||||||
|
reply.send
|
||||||
|
success: true
|
||||||
|
user: user
|
||||||
|
return
|
||||||
|
|
||||||
|
.catch (err) ->
|
||||||
|
SHARE.logger.debug "FAIL registration '#{request.body.login}'"
|
||||||
|
reply.send
|
||||||
|
success: false
|
||||||
|
err: err
|
Reference in a new issue