2018-12-16 02:02:57 +00:00
|
|
|
# node libraries
|
2018-12-16 21:51:08 +00:00
|
|
|
express = (require 'express')
|
2018-12-16 02:02:57 +00:00
|
|
|
path = (require 'path')
|
|
|
|
|
|
|
|
# my libraries
|
|
|
|
FFTCGDB = (require './fftcgdb')
|
|
|
|
|
|
|
|
# open fftcg db
|
|
|
|
fftcgdb = new FFTCGDB path.resolve(__dirname, '../fftcg.db')
|
|
|
|
|
2018-12-16 21:51:08 +00:00
|
|
|
# create router
|
|
|
|
FFTCGROUTER = express.Router()
|
2018-12-26 23:16:59 +00:00
|
|
|
|
|
|
|
# request logging
|
|
|
|
FFTCGROUTER.use (req, res, next) ->
|
|
|
|
if req.session.user
|
2018-12-27 02:02:21 +00:00
|
|
|
console.log "[FFTCGROUTER] user '#{req.session.user.login}' requested '#{req.url}'"
|
2018-12-26 23:16:59 +00:00
|
|
|
else
|
2018-12-27 02:02:21 +00:00
|
|
|
console.log "[FFTCGROUTER] requested '#{req.url}'"
|
2018-12-26 23:16:59 +00:00
|
|
|
|
|
|
|
next()
|
|
|
|
|
|
|
|
# static content
|
2018-12-16 21:51:08 +00:00
|
|
|
FFTCGROUTER.use express.static path.resolve(__dirname, '../public_html')
|
|
|
|
|
2018-12-16 02:02:57 +00:00
|
|
|
# register user
|
|
|
|
FFTCGROUTER.post '/register', (req, res) ->
|
|
|
|
fftcgdb.register req.body.login, req.body.password
|
2018-12-16 21:51:08 +00:00
|
|
|
.then (user) ->
|
|
|
|
# registration successful, return JSON status
|
2018-12-16 02:02:57 +00:00
|
|
|
res.json
|
|
|
|
status: 'ok'
|
2018-12-27 02:03:22 +00:00
|
|
|
user: user.user
|
2018-12-16 21:51:08 +00:00
|
|
|
login: user.login
|
|
|
|
|
2018-12-16 02:02:57 +00:00
|
|
|
.catch (err) ->
|
2018-12-16 21:51:08 +00:00
|
|
|
# registration failed, return JSON status
|
2018-12-16 02:02:57 +00:00
|
|
|
res.json
|
|
|
|
status: 'fail'
|
|
|
|
text: err
|
|
|
|
|
|
|
|
# log in user
|
|
|
|
FFTCGROUTER.post '/login', (req, res) ->
|
|
|
|
fftcgdb.login req.body.login, req.body.password
|
2018-12-16 21:51:08 +00:00
|
|
|
.then (user) ->
|
|
|
|
# login successful, save stuff in session
|
|
|
|
req.session.user = user
|
2018-12-16 02:02:57 +00:00
|
|
|
req.session.save()
|
2018-12-16 21:51:08 +00:00
|
|
|
|
|
|
|
# return JSON status
|
2018-12-16 02:02:57 +00:00
|
|
|
res.json
|
|
|
|
status: 'ok'
|
2018-12-27 02:03:22 +00:00
|
|
|
user: user.user
|
2018-12-16 21:51:08 +00:00
|
|
|
login: user.login
|
|
|
|
|
2018-12-16 02:02:57 +00:00
|
|
|
.catch (err) ->
|
2018-12-16 21:51:08 +00:00
|
|
|
# login failed, return JSON status
|
2018-12-16 02:02:57 +00:00
|
|
|
res.json
|
|
|
|
status: 'fail'
|
|
|
|
text: err
|
|
|
|
|
2018-12-26 23:16:59 +00:00
|
|
|
# Templates
|
|
|
|
FFTCGROUTER.get '/:template.html', (req, res) ->
|
2018-12-27 00:24:14 +00:00
|
|
|
# redirect logged-in users to user cp
|
2018-12-26 23:16:59 +00:00
|
|
|
if req.session.user and req.params.template == 'index'
|
2018-12-27 00:24:14 +00:00
|
|
|
return res.redirect '/usercp.html'
|
2018-12-26 23:16:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
# render requested template
|
|
|
|
res.render (req.params.template + '.pug'), (err, html) ->
|
|
|
|
# redirect invalid requests to index
|
|
|
|
if err
|
|
|
|
return res.redirect '/index.html'
|
|
|
|
|
|
|
|
# actual response
|
|
|
|
res.send html
|
|
|
|
|
|
|
|
# default route
|
|
|
|
FFTCGROUTER.use (req, res) ->
|
|
|
|
return res.redirect '/index.html'
|
|
|
|
|
2018-12-16 02:02:57 +00:00
|
|
|
module.exports = FFTCGROUTER
|