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')
|
path = (require 'path')
|
||||||
|
|
||||||
# my libraries
|
# my libraries
|
||||||
# FFTCGSOCKET = (require './socket')
|
socket = (require './socket')
|
||||||
# FFTCGSESSION = (require './session')
|
# FFTCGSESSION = (require './session')
|
||||||
# FFTCGROUTER = (require './router')
|
routes = (require './routes')
|
||||||
|
|
||||||
# fastify framework
|
# fastify framework
|
||||||
fastify.register (require 'fastify-cookie')
|
fastify.register (require 'fastify-cookie')
|
||||||
|
@ -16,29 +16,15 @@ fastify.register (require 'fastify-static'), root: path.join __dirname, 'tmpfron
|
||||||
|
|
||||||
fastify.ready()
|
fastify.ready()
|
||||||
.then ->
|
.then ->
|
||||||
fastify.ws.on 'connection', (socket) ->
|
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.'
|
|
||||||
|
|
||||||
.catch (err) ->
|
.catch (err) ->
|
||||||
logger.error err
|
logger.error err
|
||||||
process.exit 1
|
process.exit 1
|
||||||
|
|
||||||
|
|
||||||
fastify.get '/api', (request, reply) ->
|
for route in routes
|
||||||
logger.info request.cookies
|
fastify.route route
|
||||||
|
|
||||||
reply
|
|
||||||
.setCookie 'foo', 'bar'
|
|
||||||
.send hello: 'world'
|
|
||||||
|
|
||||||
|
|
||||||
fastify.listen 3001, '0.0.0.0'
|
fastify.listen 3001, '0.0.0.0'
|
||||||
.catch (err) ->
|
.catch (err) ->
|
||||||
|
|
|
@ -1,32 +1,42 @@
|
||||||
# node libraries
|
# node libraries
|
||||||
socketio = (require 'socket.io')
|
|
||||||
path = (require 'path')
|
path = (require 'path')
|
||||||
logger = (require 'logging').default 'socket'
|
logger = (require 'logging').default 'socket'
|
||||||
|
|
||||||
# my libraries
|
# my libraries
|
||||||
|
|
||||||
FFTCGSOCKET = (http, session) ->
|
module.exports = (socket) ->
|
||||||
that = @
|
logger.info 'Client connected.'
|
||||||
|
|
||||||
# create server socket
|
socket.on 'message', (msg) ->
|
||||||
@io = socketio http
|
# echo server
|
||||||
@io.use session
|
logger.info "Echo '#{msg}'."
|
||||||
|
socket.send "Re: #{msg}"
|
||||||
|
|
||||||
# on new connection
|
socket.on 'close', ->
|
||||||
@io.on 'connection', (socket) ->
|
logger.info 'Client disconnected.'
|
||||||
@session = socket.handshake.session
|
|
||||||
logger.debug "session '#{@session.id}' connected"
|
|
||||||
logger.debug "is user '#{@session.userID}'" if @session.userID
|
|
||||||
|
|
||||||
socket.on 'disconnect', ->
|
# FFTCGSOCKET = (http, session) ->
|
||||||
logger.debug "session '#{that.session.id}' disconnected"
|
# that = @
|
||||||
logger.debug "is user '#{that.session.userID}'" if that.session.userID
|
#
|
||||||
|
# # create server socket
|
||||||
return
|
# @io = socketio http
|
||||||
|
# @io.use session
|
||||||
|
#
|
||||||
FFTCGSOCKET::close = ->
|
# # on new connection
|
||||||
logger.info 'shutting down'
|
# @io.on 'connection', (socket) ->
|
||||||
|
# @session = socket.handshake.session
|
||||||
|
# logger.debug "session '#{@session.id}' connected"
|
||||||
module.exports = FFTCGSOCKET
|
# 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>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
Hello World
|
Hello World
|
||||||
|
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
const host = location.origin.replace(/^http/, 'ws')
|
const host = location.origin.replace(/^http/, 'ws')
|
||||||
const ws = new WebSocket(host)
|
const ws = new WebSocket(host)
|
||||||
|
@ -17,6 +18,12 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(JSON.stringify(document.cookie))
|
console.log(JSON.stringify(document.cookie))
|
||||||
|
axios.post('/test',{
|
||||||
|
x: 123
|
||||||
|
})
|
||||||
|
.then( (response) => {
|
||||||
|
console.log(response)
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Reference in a new issue