2018-12-07 09:38:46 +00:00
|
|
|
# node libraries
|
2019-02-16 18:03:21 +00:00
|
|
|
# (require 'debug').enable 'FFTCG'
|
2019-02-07 16:03:20 +00:00
|
|
|
logger = (require 'logging').default 'FFTCG'
|
2019-02-16 18:03:21 +00:00
|
|
|
fastify = (require 'fastify')
|
|
|
|
logger: level: 'warn'
|
2019-02-13 16:25:35 +00:00
|
|
|
|
2019-02-19 20:48:25 +00:00
|
|
|
# fastify and plugin framework
|
2019-02-13 16:25:35 +00:00
|
|
|
fastify.register (require 'fastify-cookie')
|
|
|
|
fastify.register (require 'fastify-ws'), library: 'uws'
|
2019-05-06 14:17:13 +00:00
|
|
|
fastify.register (require 'fastify-cors')
|
2019-02-19 20:48:25 +00:00
|
|
|
|
|
|
|
# temporary dev frontend; to be uninstalled
|
|
|
|
path = (require 'path')
|
2019-02-15 12:06:16 +00:00
|
|
|
fastify.register (require 'fastify-static'), root: (path.join __dirname, 'tmpfront')
|
2019-02-13 16:25:35 +00:00
|
|
|
|
2019-02-19 20:48:25 +00:00
|
|
|
# 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()
|
|
|
|
|
|
|
|
# finalize loadup
|
2019-02-13 16:25:35 +00:00
|
|
|
fastify.ready()
|
|
|
|
.then ->
|
2019-02-19 20:48:25 +00:00
|
|
|
# create websocket on successful load
|
|
|
|
socket = (require './socket')
|
2019-02-14 08:54:41 +00:00
|
|
|
fastify.ws.on 'connection', socket
|
2019-02-13 16:25:35 +00:00
|
|
|
|
|
|
|
.catch (err) ->
|
2019-02-19 20:48:25 +00:00
|
|
|
# abort on load failure
|
2019-02-13 16:25:35 +00:00
|
|
|
logger.error err
|
|
|
|
process.exit 1
|
|
|
|
|
2019-02-19 20:48:25 +00:00
|
|
|
# start server
|
2019-02-13 16:25:35 +00:00
|
|
|
fastify.listen 3001, '0.0.0.0'
|
|
|
|
.catch (err) ->
|
|
|
|
logger.error err
|
|
|
|
|
2018-12-07 09:38:46 +00:00
|
|
|
# Handle termination
|
|
|
|
process.on 'SIGINT', ->
|
2018-12-14 12:31:07 +00:00
|
|
|
socket.close()
|
2019-02-07 16:03:20 +00:00
|
|
|
logger.info 'shutting down after SIGINT'
|
2018-12-07 09:38:46 +00:00
|
|
|
process.exit()
|