This repository has been archived on 2024-04-29. You can view files and clone it, but cannot push or open issues or pull requests.
node-fftcg/backend/server.coffee

47 lines
1,017 B
CoffeeScript

# node libraries
# (require 'debug').enable 'FFTCG'
logger = (require 'logging').default 'FFTCG'
fastify = (require 'fastify')
logger: level: 'warn'
# fastify and plugin framework
fastify.register (require 'fastify-ws'), library: 'uws'
fastify.register (require 'fastify-cors')
# 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
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()
logger.info 'shutting down after SIGINT'
process.exit()