This repository has been archived on 2024-04-29. You can view files and clone it, but cannot push or open issues or pull requests.
node-fftcg/backend/routes.coffee

64 lines
1.5 KiB
CoffeeScript

# node libraries
express = (require 'express')
path = (require 'path')
# my libraries
FFTCGDB = (require './db')
logger = (require 'logging').default 'router'
# open fftcg db
fftcgdb = new FFTCGDB path.resolve(__dirname, 'fftcg.db')
module.exports = [
# test
url: '/test'
method: 'POST'
handler: (request, reply) ->
logger.info 'Cookies', request.cookies
logger.info 'Query', request.query
logger.info 'Params', request.params
reply.setCookie 'foo', 'foo'
reply.send
hello: 'world'
,
# register user
url: '/user/register'
method: 'POST'
handler: (request, reply) ->
fftcgdb.register req.body.login, req.body.password
.then (user) ->
# registration successful, return JSON status
reply.send
status: 'ok'
user: user.user
login: user.login
.catch (err) ->
# registration failed, return JSON status
reply.send
status: 'fail'
text: err
,
# log in user
url: '/user/login'
method: 'POST'
handler: (request, reply) ->
fftcgdb.login req.body.login, req.body.password
.then (user) ->
# login successful, save stuff in cookie
reply.setCookie 'user', JSON.stringify user
# return JSON status
res.json
status: 'ok'
user: user.user
login: user.login
.catch (err) ->
# login failed, return JSON status
res.json
status: 'fail'
text: err
,
]