basic route module without schemata, basic session agnostic socket
This commit is contained in:
parent
2ea24ad1da
commit
678a30e94d
5 changed files with 109 additions and 102 deletions
|
@ -1,60 +0,0 @@
|
|||
# node libraries
|
||||
express = (require 'express')
|
||||
path = (require 'path')
|
||||
|
||||
# my libraries
|
||||
FFTCGDB = (require './db')
|
||||
logger = (require 'logging').default 'router'
|
||||
|
||||
# open fftcg db
|
||||
fftcgdb = new FFTCGDB path.resolve(__dirname, 'fftcg.db')
|
||||
|
||||
# create router
|
||||
FFTCGROUTER = express.Router()
|
||||
|
||||
# request logging
|
||||
FFTCGROUTER.use (req, res, next) ->
|
||||
if req.session.user
|
||||
logger.debug "user '#{req.session.user.login}' requested '#{req.url}'"
|
||||
else
|
||||
logger.debug "requested '#{req.url}'"
|
||||
|
||||
next()
|
||||
|
||||
# register user
|
||||
FFTCGROUTER.post '/register', (req, res) ->
|
||||
fftcgdb.register req.body.login, req.body.password
|
||||
.then (user) ->
|
||||
# registration successful, return JSON status
|
||||
res.json
|
||||
status: 'ok'
|
||||
user: user.user
|
||||
login: user.login
|
||||
|
||||
.catch (err) ->
|
||||
# registration failed, return JSON status
|
||||
res.json
|
||||
status: 'fail'
|
||||
text: err
|
||||
|
||||
# log in user
|
||||
FFTCGROUTER.post '/login', (req, res) ->
|
||||
fftcgdb.login req.body.login, req.body.password
|
||||
.then (user) ->
|
||||
# login successful, save stuff in session
|
||||
req.session.user = user
|
||||
req.session.save()
|
||||
|
||||
# return JSON status
|
||||
res.json
|
||||
status: 'ok'
|
||||
user: user.user
|
||||
login: user.login
|
||||
|
||||
.catch (err) ->
|
||||
# login failed, return JSON status
|
||||
res.json
|
||||
status: 'fail'
|
||||
text: err
|
||||
|
||||
module.exports = FFTCGROUTER
|
64
backend/routes.coffee
Normal file
64
backend/routes.coffee
Normal file
|
@ -0,0 +1,64 @@
|
|||
# node libraries
|
||||
express = (require 'express')
|
||||
path = (require 'path')
|
||||
|
||||
# my libraries
|
||||
FFTCGDB = (require './db')
|
||||
logger = (require 'logging').default 'router'
|
||||
|
||||
# open fftcg db
|
||||
fftcgdb = new FFTCGDB path.resolve(__dirname, 'fftcg.db')
|
||||
|
||||
module.exports = [
|
||||
# test
|
||||
url: '/test'
|
||||
method: 'POST'
|
||||
handler: (request, reply) ->
|
||||
logger.info 'Cookies', request.cookies
|
||||
logger.info 'Query', request.query
|
||||
logger.info 'Params', request.params
|
||||
|
||||
reply.setCookie 'foo', 'foo'
|
||||
reply.send
|
||||
hello: 'world'
|
||||
,
|
||||
# register user
|
||||
url: '/user/register'
|
||||
method: 'POST'
|
||||
handler: (request, reply) ->
|
||||
fftcgdb.register req.body.login, req.body.password
|
||||
.then (user) ->
|
||||
# registration successful, return JSON status
|
||||
reply.send
|
||||
status: 'ok'
|
||||
user: user.user
|
||||
login: user.login
|
||||
|
||||
.catch (err) ->
|
||||
# registration failed, return JSON status
|
||||
reply.send
|
||||
status: 'fail'
|
||||
text: err
|
||||
,
|
||||
# log in user
|
||||
url: '/user/login'
|
||||
method: 'POST'
|
||||
handler: (request, reply) ->
|
||||
fftcgdb.login req.body.login, req.body.password
|
||||
.then (user) ->
|
||||
# login successful, save stuff in cookie
|
||||
reply.setCookie 'user', JSON.stringify user
|
||||
|
||||
# return JSON status
|
||||
res.json
|
||||
status: 'ok'
|
||||
user: user.user
|
||||
login: user.login
|
||||
|
||||
.catch (err) ->
|
||||
# login failed, return JSON status
|
||||
res.json
|
||||
status: 'fail'
|
||||
text: err
|
||||
,
|
||||
]
|
|
@ -4,9 +4,9 @@ fastify = (require 'fastify') logger: logger
|
|||
path = (require 'path')
|
||||
|
||||
# my libraries
|
||||
# FFTCGSOCKET = (require './socket')
|
||||
socket = (require './socket')
|
||||
# FFTCGSESSION = (require './session')
|
||||
# FFTCGROUTER = (require './router')
|
||||
routes = (require './routes')
|
||||
|
||||
# fastify framework
|
||||
fastify.register (require 'fastify-cookie')
|
||||
|
@ -16,29 +16,15 @@ fastify.register (require 'fastify-static'), root: path.join __dirname, 'tmpfron
|
|||
|
||||
fastify.ready()
|
||||
.then ->
|
||||
fastify.ws.on 'connection', (socket) ->
|
||||
logger.info 'Client connected.'
|
||||
|
||||
socket.on 'message', (msg) ->
|
||||
# echo server
|
||||
logger.info "Echo '#{msg}'."
|
||||
socket.send "Re: #{msg}"
|
||||
|
||||
socket.on 'close', ->
|
||||
logger.info 'Client disconnected.'
|
||||
fastify.ws.on 'connection', socket
|
||||
|
||||
.catch (err) ->
|
||||
logger.error err
|
||||
process.exit 1
|
||||
|
||||
|
||||
fastify.get '/api', (request, reply) ->
|
||||
logger.info request.cookies
|
||||
|
||||
reply
|
||||
.setCookie 'foo', 'bar'
|
||||
.send hello: 'world'
|
||||
|
||||
for route in routes
|
||||
fastify.route route
|
||||
|
||||
fastify.listen 3001, '0.0.0.0'
|
||||
.catch (err) ->
|
||||
|
|
|
@ -1,32 +1,42 @@
|
|||
# node libraries
|
||||
socketio = (require 'socket.io')
|
||||
path = (require 'path')
|
||||
logger = (require 'logging').default 'socket'
|
||||
|
||||
# my libraries
|
||||
|
||||
FFTCGSOCKET = (http, session) ->
|
||||
that = @
|
||||
module.exports = (socket) ->
|
||||
logger.info 'Client connected.'
|
||||
|
||||
# create server socket
|
||||
@io = socketio http
|
||||
@io.use session
|
||||
socket.on 'message', (msg) ->
|
||||
# echo server
|
||||
logger.info "Echo '#{msg}'."
|
||||
socket.send "Re: #{msg}"
|
||||
|
||||
# on new connection
|
||||
@io.on 'connection', (socket) ->
|
||||
@session = socket.handshake.session
|
||||
logger.debug "session '#{@session.id}' connected"
|
||||
logger.debug "is user '#{@session.userID}'" if @session.userID
|
||||
socket.on 'close', ->
|
||||
logger.info 'Client disconnected.'
|
||||
|
||||
socket.on 'disconnect', ->
|
||||
logger.debug "session '#{that.session.id}' disconnected"
|
||||
logger.debug "is user '#{that.session.userID}'" if that.session.userID
|
||||
|
||||
return
|
||||
|
||||
|
||||
FFTCGSOCKET::close = ->
|
||||
logger.info 'shutting down'
|
||||
|
||||
|
||||
module.exports = FFTCGSOCKET
|
||||
# FFTCGSOCKET = (http, session) ->
|
||||
# that = @
|
||||
#
|
||||
# # create server socket
|
||||
# @io = socketio http
|
||||
# @io.use session
|
||||
#
|
||||
# # on new connection
|
||||
# @io.on 'connection', (socket) ->
|
||||
# @session = socket.handshake.session
|
||||
# logger.debug "session '#{@session.id}' connected"
|
||||
# logger.debug "is user '#{@session.userID}'" if @session.userID
|
||||
#
|
||||
# socket.on 'disconnect', ->
|
||||
# logger.debug "session '#{that.session.id}' disconnected"
|
||||
# logger.debug "is user '#{that.session.userID}'" if that.session.userID
|
||||
#
|
||||
# return
|
||||
#
|
||||
#
|
||||
# FFTCGSOCKET::close = ->
|
||||
# logger.info 'shutting down'
|
||||
#
|
||||
#
|
||||
# module.exports = FFTCGSOCKET
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
</head>
|
||||
<body>
|
||||
Hello World
|
||||
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
|
||||
<script type="text/javascript">
|
||||
const host = location.origin.replace(/^http/, 'ws')
|
||||
const ws = new WebSocket(host)
|
||||
|
@ -17,6 +18,12 @@
|
|||
}
|
||||
|
||||
console.log(JSON.stringify(document.cookie))
|
||||
axios.post('/test',{
|
||||
x: 123
|
||||
})
|
||||
.then( (response) => {
|
||||
console.log(response)
|
||||
})
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Reference in a new issue