main file cleanup

This commit is contained in:
Jörn-Michael Miehe 2018-12-16 03:02:57 +01:00
parent 30de3b4497
commit 2825e84c48
3 changed files with 52 additions and 39 deletions

44
inc/fftcgrouter.coffee Normal file
View file

@ -0,0 +1,44 @@
# node libraries
FFTCGROUTER = (require 'express').Router()
path = (require 'path')
# my libraries
FFTCGDB = (require './fftcgdb')
# open fftcg db
fftcgdb = new FFTCGDB path.resolve(__dirname, '../fftcg.db')
# register user
FFTCGROUTER.post '/register', (req, res) ->
fftcgdb.register req.body.login, req.body.password
.then (userid) ->
console.log "registered '#{req.body.login}'"
res.json
status: 'ok'
uid: userid
text: req.body.login
.catch (err) ->
console.log "failed to register '#{req.body.login}'"
res.json
status: 'fail'
text: err
# log in user
FFTCGROUTER.post '/login', (req, res) ->
fftcgdb.login req.body.login, req.body.password
.then (userid) ->
req.session.userID = userid
req.session.userLogin = req.body.login
req.session.save()
console.log "logged in '#{req.body.login}'"
res.json
status: 'ok'
uid: userid
text: req.body.login
.catch (err) ->
console.log "failed to login '#{req.body.login}'"
res.json
status: 'fail'
text: err
module.exports = FFTCGROUTER

View file

@ -1,6 +1,6 @@
# node libraries
expressSession = (require 'express-session')
RedisStore = require('connect-redis')(expressSession)
RedisStore = (require 'connect-redis')(expressSession)
module.exports = (app) ->
session =

View file

@ -8,8 +8,8 @@ path = (require 'path')
# my libraries
FFTCGSOCKET = (require './inc/fftcgsocket')
FFTCGDB = (require './inc/fftcgdb')
FFTCGSESSION = (require './inc/fftcgsession')
FFTCGROUTER = (require './inc/fftcgrouter')
# express framework
app = express()
@ -21,39 +21,8 @@ app.use bodyParser.urlencoded
sessionMiddleware = FFTCGSESSION(app)
app.use sessionMiddleware
# open fftcg db
fftcgdb = new FFTCGDB path.resolve(__dirname, './fftcg.db')
app.post '/register', (req, res) ->
fftcgdb.register req.body.login, req.body.password
.then (userid) ->
console.log "registered '#{req.body.login}'"
res.json
status: 'ok'
uid: userid
text: req.body.login
.catch (err) ->
console.log "failed to register '#{req.body.login}'"
res.json
status: 'fail'
text: err
app.post '/login', (req, res) ->
fftcgdb.login req.body.login, req.body.password
.then (userid) ->
req.session.userID = userid
req.session.userLogin = req.body.login
req.session.save()
console.log "logged in '#{req.body.login}'"
res.json
status: 'ok'
uid: userid
text: req.body.login
.catch (err) ->
console.log "failed to login '#{req.body.login}'"
res.json
status: 'fail'
text: err
# REST routes
app.use FFTCGROUTER
# Static content
app.use express.static path.resolve(__dirname, 'public_html')
@ -65,14 +34,14 @@ app.get '/:template.html', (req, res) ->
console.log "logged in as '#{req.session.userLogin}'"
res.render (req.params.template + '.pug')
# Create server
# socket.io
web = http.Server app
socket = new FFTCGSOCKET web, sharedSession sessionMiddleware
# Create server
web.listen 3000, ->
console.log '[FFTCG] Listening on port 3000 ...'
# socket.io
socket = new FFTCGSOCKET web, sharedSession sessionMiddleware
# Handle termination
process.on 'SIGINT', ->
socket.close()