60 lines
1.4 KiB
CoffeeScript
60 lines
1.4 KiB
CoffeeScript
# node libraries
|
|
express = (require 'express')
|
|
helmet = (require 'helmet')
|
|
http = (require 'http')
|
|
path = (require 'path')
|
|
socketio = (require 'socket.io')
|
|
|
|
# my libraries
|
|
FFTCGDB = (require './inc/fftcgdb')
|
|
|
|
fftcgdb = new FFTCGDB path.resolve(__dirname, 'fftcg.db')
|
|
|
|
# express + socket.io framework
|
|
app = express()
|
|
web = http.Server app
|
|
io = socketio web
|
|
|
|
app.use helmet()
|
|
|
|
# Static content
|
|
app.use express.static path.resolve(__dirname, 'public_html')
|
|
|
|
# Templates
|
|
app.set 'view engine', 'pug'
|
|
app.get '/:template.html', (req, res) ->
|
|
res.render (req.params.template + '.pug')
|
|
|
|
# Server logic
|
|
io.on 'connection', (socket) ->
|
|
|
|
console.log 'a user connected'
|
|
|
|
socket.on 'disconnect', ->
|
|
console.log 'a user disconnected'
|
|
return
|
|
|
|
socket.on 'register', (login, password) ->
|
|
console.log 'message:', login, password
|
|
fftcgdb.register login, password
|
|
.then (login) ->
|
|
console.log 'registered "%s"', login
|
|
.catch (err) ->
|
|
console.error 'error: "%s"', err
|
|
|
|
socket.on 'login', (uname, password) ->
|
|
console.log 'message:', uname, password
|
|
fftcgdb.login uname, password
|
|
.then (login) ->
|
|
console.log 'Login OK "%s"', login
|
|
.catch (err) ->
|
|
console.error 'error: "%s"', err
|
|
|
|
# Create server
|
|
web.listen 3000, ->
|
|
console.log '[FFTCG] Listening on port 3000 ...'
|
|
|
|
# Handle termination
|
|
process.on 'SIGINT', ->
|
|
console.log '[FFTCG] shutting down after SIGINT'
|
|
process.exit()
|