From 678a30e94def9c4e5492d13bf2624e6152ca6008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Thu, 14 Feb 2019 09:54:41 +0100 Subject: [PATCH] basic route module without schemata, basic session agnostic socket --- backend/router.coffee | 60 ---------------------------------- backend/routes.coffee | 64 +++++++++++++++++++++++++++++++++++++ backend/server.coffee | 24 +++----------- backend/socket.coffee | 56 +++++++++++++++++++------------- backend/tmpfront/index.html | 7 ++++ 5 files changed, 109 insertions(+), 102 deletions(-) delete mode 100644 backend/router.coffee create mode 100644 backend/routes.coffee diff --git a/backend/router.coffee b/backend/router.coffee deleted file mode 100644 index d0c1d78..0000000 --- a/backend/router.coffee +++ /dev/null @@ -1,60 +0,0 @@ -# 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') - -# create router -FFTCGROUTER = express.Router() - -# request logging -FFTCGROUTER.use (req, res, next) -> - if req.session.user - logger.debug "user '#{req.session.user.login}' requested '#{req.url}'" - else - logger.debug "requested '#{req.url}'" - - next() - -# register user -FFTCGROUTER.post '/register', (req, res) -> - fftcgdb.register req.body.login, req.body.password - .then (user) -> - # registration successful, return JSON status - res.json - status: 'ok' - user: user.user - login: user.login - - .catch (err) -> - # registration failed, return JSON status - res.json - status: 'fail' - text: err - -# log in user -FFTCGROUTER.post '/login', (req, res) -> - fftcgdb.login req.body.login, req.body.password - .then (user) -> - # login successful, save stuff in session - req.session.user = user - req.session.save() - - # 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 - -module.exports = FFTCGROUTER diff --git a/backend/routes.coffee b/backend/routes.coffee new file mode 100644 index 0000000..22c2bbc --- /dev/null +++ b/backend/routes.coffee @@ -0,0 +1,64 @@ +# 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 +, +] diff --git a/backend/server.coffee b/backend/server.coffee index 0c6e4e8..8f7edb7 100644 --- a/backend/server.coffee +++ b/backend/server.coffee @@ -4,9 +4,9 @@ fastify = (require 'fastify') logger: logger path = (require 'path') # my libraries -# FFTCGSOCKET = (require './socket') +socket = (require './socket') # FFTCGSESSION = (require './session') -# FFTCGROUTER = (require './router') +routes = (require './routes') # fastify framework fastify.register (require 'fastify-cookie') @@ -16,29 +16,15 @@ fastify.register (require 'fastify-static'), root: path.join __dirname, 'tmpfron fastify.ready() .then -> - fastify.ws.on 'connection', (socket) -> - logger.info 'Client connected.' - - socket.on 'message', (msg) -> - # echo server - logger.info "Echo '#{msg}'." - socket.send "Re: #{msg}" - - socket.on 'close', -> - logger.info 'Client disconnected.' + fastify.ws.on 'connection', socket .catch (err) -> logger.error err process.exit 1 -fastify.get '/api', (request, reply) -> - logger.info request.cookies - - reply - .setCookie 'foo', 'bar' - .send hello: 'world' - +for route in routes + fastify.route route fastify.listen 3001, '0.0.0.0' .catch (err) -> diff --git a/backend/socket.coffee b/backend/socket.coffee index cd8c4a8..cb63309 100644 --- a/backend/socket.coffee +++ b/backend/socket.coffee @@ -1,32 +1,42 @@ # node libraries -socketio = (require 'socket.io') path = (require 'path') logger = (require 'logging').default 'socket' # my libraries -FFTCGSOCKET = (http, session) -> - that = @ +module.exports = (socket) -> + logger.info 'Client connected.' - # create server socket - @io = socketio http - @io.use session + socket.on 'message', (msg) -> + # echo server + logger.info "Echo '#{msg}'." + socket.send "Re: #{msg}" - # on new connection - @io.on 'connection', (socket) -> - @session = socket.handshake.session - logger.debug "session '#{@session.id}' connected" - logger.debug "is user '#{@session.userID}'" if @session.userID + socket.on 'close', -> + logger.info 'Client disconnected.' - socket.on 'disconnect', -> - logger.debug "session '#{that.session.id}' disconnected" - logger.debug "is user '#{that.session.userID}'" if that.session.userID - - return - - -FFTCGSOCKET::close = -> - logger.info 'shutting down' - - -module.exports = FFTCGSOCKET +# FFTCGSOCKET = (http, session) -> +# that = @ +# +# # create server socket +# @io = socketio http +# @io.use session +# +# # on new connection +# @io.on 'connection', (socket) -> +# @session = socket.handshake.session +# logger.debug "session '#{@session.id}' connected" +# logger.debug "is user '#{@session.userID}'" if @session.userID +# +# socket.on 'disconnect', -> +# logger.debug "session '#{that.session.id}' disconnected" +# logger.debug "is user '#{that.session.userID}'" if that.session.userID +# +# return +# +# +# FFTCGSOCKET::close = -> +# logger.info 'shutting down' +# +# +# module.exports = FFTCGSOCKET diff --git a/backend/tmpfront/index.html b/backend/tmpfront/index.html index 7d9bb4b..e387ced 100644 --- a/backend/tmpfront/index.html +++ b/backend/tmpfront/index.html @@ -6,6 +6,7 @@ Hello World +