diff --git a/inc/fftcgrouter.coffee b/inc/fftcgrouter.coffee new file mode 100644 index 0000000..c4ebb39 --- /dev/null +++ b/inc/fftcgrouter.coffee @@ -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 diff --git a/inc/fftcgsession.coffee b/inc/fftcgsession.coffee index fc48414..10bf6ad 100644 --- a/inc/fftcgsession.coffee +++ b/inc/fftcgsession.coffee @@ -1,6 +1,6 @@ # node libraries expressSession = (require 'express-session') -RedisStore = require('connect-redis')(expressSession) +RedisStore = (require 'connect-redis')(expressSession) module.exports = (app) -> session = diff --git a/server.coffee b/server.coffee index e692c7e..f81072f 100644 --- a/server.coffee +++ b/server.coffee @@ -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()