diff --git a/backend/routes.coffee b/backend/routes.coffee deleted file mode 100644 index d0419e6..0000000 --- a/backend/routes.coffee +++ /dev/null @@ -1,8 +0,0 @@ -module.exports = [ - # test - (require './routes/test') - # register user - (require './routes/user/register') - # log in user - (require './routes/user/login') -] diff --git a/backend/server.coffee b/backend/server.coffee index 0937a61..723d048 100644 --- a/backend/server.coffee +++ b/backend/server.coffee @@ -3,37 +3,47 @@ logger = (require 'logging').default 'FFTCG' fastify = (require 'fastify') logger: level: 'warn' -path = (require 'path') -# my libraries -socket = (require './socket') -# FFTCGSESSION = (require './session') -routes = (require './routes') - -# fastify framework +# fastify and plugin framework fastify.register (require 'fastify-cookie') fastify.register (require 'fastify-ws'), library: 'uws' + +# temporary dev frontend; to be uninstalled +path = (require 'path') fastify.register (require 'fastify-static'), root: (path.join __dirname, 'tmpfront') -fastify.ready() - .then -> - fastify.ws.on 'connection', socket - - .catch (err) -> - logger.error err - process.exit 1 +# API routes +fastify.route (require "./routes/#{route}") for route in [ + # test route + 'test' + # log in user + 'user/login' + # register user + 'user/register' +] +# request logging fastify.addHook 'onRequest', (req, res, next) -> logger.debug 'requested', req.url next() -fastify.route route for route in routes +# finalize loadup +fastify.ready() + .then -> + # create websocket on successful load + socket = (require './socket') + fastify.ws.on 'connection', socket + .catch (err) -> + # abort on load failure + logger.error err + process.exit 1 + +# start server fastify.listen 3001, '0.0.0.0' .catch (err) -> logger.error err - # Handle termination process.on 'SIGINT', -> socket.close() diff --git a/backend/socket.coffee b/backend/socket.coffee index cb63309..8fe7530 100644 --- a/backend/socket.coffee +++ b/backend/socket.coffee @@ -5,15 +5,15 @@ logger = (require 'logging').default 'socket' # my libraries module.exports = (socket) -> - logger.info 'Client connected.' + logger.info 'OK connect' socket.on 'message', (msg) -> # echo server - logger.info "Echo '#{msg}'." + logger.info "OK received '#{msg}'" socket.send "Re: #{msg}" socket.on 'close', -> - logger.info 'Client disconnected.' + logger.info 'OK disconnect' # FFTCGSOCKET = (http, session) -> # that = @