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

65 lines
1.3 KiB
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'
# user info
'user/info'
# log out user
'user/logout'
# register user
'user/register'
# list decks
'decks/list'
# add deck
'decks/add'
# modify deck
'decks/modify'
# delete deck
'decks/delete'
# list games
'games/list'
]
# 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 'SIGUSR2', ->
(require './db').close()
.then ->
logger.info 'shutting down normally after SIGINT'
.catch ->
logger.info 'error shutting down after SIGINT'
.finally ->
process.exit()