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

66 lines
1.3 KiB
CoffeeScript
Raw Permalink Normal View History

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-19 20:48:25 +00:00
# fastify and plugin framework
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
# API routes
fastify.route (require "./routes/#{route}") for route in [
# test route
'test'
# log in user
'user/login'
2019-05-09 12:31:54 +00:00
# user info
'user/info'
2019-05-07 20:15:18 +00:00
# log out user
'user/logout'
2019-02-19 20:48:25 +00:00
# register user
'user/register'
2019-05-10 12:09:49 +00:00
# list decks
'decks/list'
2019-05-24 11:30:26 +00:00
# add deck
'decks/add'
2019-05-20 15:02:39 +00:00
# modify deck
'decks/modify'
2019-05-24 11:41:48 +00:00
# delete deck
'decks/delete'
2019-06-04 15:00:09 +00:00
# list games
'games/list'
2019-02-19 20:48:25 +00:00
]
# request logging
fastify.addHook 'onRequest', (req, res, next) ->
logger.debug 'requested', req.url
next()
# finalize loadup
fastify.ready()
.then ->
2019-02-19 20:48:25 +00:00
# create websocket on successful load
socket = (require './socket')
fastify.ws.on 'connection', socket
.catch (err) ->
2019-02-19 20:48:25 +00:00
# abort on load failure
logger.error err
process.exit 1
2019-02-19 20:48:25 +00:00
# start server
fastify.listen 3001, '0.0.0.0'
.catch (err) ->
logger.error err
2018-12-07 09:38:46 +00:00
# Handle termination
2019-06-04 14:58:44 +00:00
process.on 'SIGUSR2', ->
2019-06-04 16:01:32 +00:00
(require './db').close()
.then ->
logger.info 'shutting down normally after SIGINT'
.catch ->
logger.info 'error shutting down after SIGINT'
.finally ->
process.exit()