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'
|
logger = (require 'logging').default 'FFTCG'
|
||||||
fastify = (require 'fastify')
|
fastify = (require 'fastify')
|
||||||
logger: level: 'warn'
|
logger: level: 'warn'
|
||||||
path = (require 'path')
|
|
||||||
|
|
||||||
# my libraries
|
# fastify and plugin framework
|
||||||
socket = (require './socket')
|
|
||||||
# FFTCGSESSION = (require './session')
|
|
||||||
routes = (require './routes')
|
|
||||||
|
|
||||||
# fastify framework
|
|
||||||
fastify.register (require 'fastify-cookie')
|
fastify.register (require 'fastify-cookie')
|
||||||
fastify.register (require 'fastify-ws'), library: 'uws'
|
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.register (require 'fastify-static'), root: (path.join __dirname, 'tmpfront')
|
||||||
|
|
||||||
fastify.ready()
|
# API routes
|
||||||
.then ->
|
fastify.route (require "./routes/#{route}") for route in [
|
||||||
fastify.ws.on 'connection', socket
|
# test route
|
||||||
|
'test'
|
||||||
.catch (err) ->
|
# log in user
|
||||||
logger.error err
|
'user/login'
|
||||||
process.exit 1
|
# register user
|
||||||
|
'user/register'
|
||||||
|
]
|
||||||
|
|
||||||
|
# request logging
|
||||||
fastify.addHook 'onRequest', (req, res, next) ->
|
fastify.addHook 'onRequest', (req, res, next) ->
|
||||||
logger.debug 'requested', req.url
|
logger.debug 'requested', req.url
|
||||||
next()
|
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'
|
fastify.listen 3001, '0.0.0.0'
|
||||||
.catch (err) ->
|
.catch (err) ->
|
||||||
logger.error err
|
logger.error err
|
||||||
|
|
||||||
|
|
||||||
# Handle termination
|
# Handle termination
|
||||||
process.on 'SIGINT', ->
|
process.on 'SIGINT', ->
|
||||||
socket.close()
|
socket.close()
|
||||||
|
|
|
@ -5,15 +5,15 @@ logger = (require 'logging').default 'socket'
|
||||||
# my libraries
|
# my libraries
|
||||||
|
|
||||||
module.exports = (socket) ->
|
module.exports = (socket) ->
|
||||||
logger.info 'Client connected.'
|
logger.info 'OK connect'
|
||||||
|
|
||||||
socket.on 'message', (msg) ->
|
socket.on 'message', (msg) ->
|
||||||
# echo server
|
# echo server
|
||||||
logger.info "Echo '#{msg}'."
|
logger.info "OK received '#{msg}'"
|
||||||
socket.send "Re: #{msg}"
|
socket.send "Re: #{msg}"
|
||||||
|
|
||||||
socket.on 'close', ->
|
socket.on 'close', ->
|
||||||
logger.info 'Client disconnected.'
|
logger.info 'OK disconnect'
|
||||||
|
|
||||||
# FFTCGSOCKET = (http, session) ->
|
# FFTCGSOCKET = (http, session) ->
|
||||||
# that = @
|
# that = @
|
||||||
|
|
Reference in a new issue