routing cleanup
This commit is contained in:
parent
c3fd13e358
commit
59b5f68b8b
3 changed files with 29 additions and 27 deletions
|
@ -1,8 +0,0 @@
|
|||
module.exports = [
|
||||
# test
|
||||
(require './routes/test')
|
||||
# register user
|
||||
(require './routes/user/register')
|
||||
# log in user
|
||||
(require './routes/user/login')
|
||||
]
|
|
@ -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()
|
||||
|
|
|
@ -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 = @
|
||||
|
|
Reference in a new issue