From 492950544e6949dd16fe2a74b72fcb2f474d3739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Fri, 7 Dec 2018 10:38:46 +0100 Subject: [PATCH] mysql -> sqlite DB, own db module --- docker-compose.yml | 25 ++---------------- fftcg.db | Bin 0 -> 8192 bytes inc/fftcgdb.coffee | 43 +++++++++++++++++++++++++++++++ package.json | 4 +-- server.coffee | 62 ++++++++++++++++----------------------------- users.sql | 22 ---------------- 6 files changed, 69 insertions(+), 87 deletions(-) create mode 100644 fftcg.db create mode 100644 inc/fftcgdb.coffee delete mode 100644 users.sql diff --git a/docker-compose.yml b/docker-compose.yml index 4ecd485..0d4e427 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,28 +1,6 @@ version: "2" -volumes: - mariadata: - services: - tcgsql: - image: mariadb:10 - restart: "no" - environment: - - "MYSQL_ROOT_PASSWORD=Oonov0caichohth8leiyienguoho8cho" - volumes: - - "mariadata:/var/lib/mysql" - - adminer: - image: adminer:standalone - restart: "no" - environment: - - "ADMINER_DESIGN=rmsoft" - - "ADMINER_DEFAULT_SERVER=tcgsql" - ports: - - "8002:8080" - - # dbuser: fftcg - # dbpass: juchie5OhH6eiQuujaoquievezoe9iXe fftcg: build: . command: "yarn debug" @@ -30,5 +8,6 @@ services: volumes: - "${PWD}/src:/app/src" - "${PWD}/public_html:/app/public_html" + - "${PWD}/fftcg.db:/app/fftcg.db" ports: - - "8001:8080" + - "3000:3000" diff --git a/fftcg.db b/fftcg.db new file mode 100644 index 0000000000000000000000000000000000000000..52522d0c8f97dbbb202eaf1e6ab0ac67651acba4 GIT binary patch literal 8192 zcmeI#O$x#=5C!0g2!(*?t{WG#R1`N}z$)E^idqj)YYL@mrH$y;TX+nQXj&)~#0$t9 z@-um4_3dW2QK`|K + @filename = filename + + @db = new sqlite3.Database @filename, (err) -> + if err + console.error err.message + console.log 'Connected to', @filename + + @db.run """ + CREATE TABLE IF NOT EXISTS users ( + nick text NOT NULL, + pwd text NOT NULL, + socket text + ); + """ + + close: -> + db.close (err) -> + if err + console.error err.message + console.log 'Closed', @filename + + adduser: (name, password) -> + saltRounds = 13 + that = @ + + bcrypt.hash password, saltRounds, (err, hash) -> + that.db.run 'INSERT INTO users (nick, pwd) VALUES (\'' + name + '\', \'' + hash + '\');', (err, result) -> + if err + console.error err.message + console.log 'Result:', result + +module.exports = FFTCGDB + +# Handle termination +process.on 'SIGINT', -> + console.log '[FFTCG-DB] shutting down' + FFTCGDB.close() diff --git a/package.json b/package.json index 5cdd0e4..2540967 100644 --- a/package.json +++ b/package.json @@ -44,9 +44,9 @@ "coffeescript": "^2.3.2", "express": "^4.16.4", "helmet": "^3.15.0", - "mysql": "^2.16.0", "pug": "^2.0.3", "socket.io": "^2.2.0", - "socket.io-client": "^2.2.0" + "socket.io-client": "^2.2.0", + "sqlite3": "^4.0.4" } } diff --git a/server.coffee b/server.coffee index c9a871f..503daf0 100644 --- a/server.coffee +++ b/server.coffee @@ -1,21 +1,18 @@ -# libraries -bcrypt = require('bcrypt') -express = require('express') -helmet = require('helmet') -http = require('http') -mysql = require('mysql') -path = require('path') -socketio = require('socket.io') +# node libraries +express = (require 'express') +helmet = (require 'helmet') +http = (require 'http') +path = (require 'path') +socketio = (require 'socket.io') -# socket.io framework +# my libraries +fftcgdb = (require './inc/fftcgdb') +fftcgdb.open path.resolve(__dirname, 'fftcg.db') + +# express + socket.io framework app = express() -web = http.Server(app) -io = socketio(web) - -# Listen server -web.listen 8080, -> - console.log 'Node.js listening on port 8080' - return +web = http.Server app +io = socketio web app.use helmet() @@ -27,39 +24,24 @@ app.set 'view engine', 'pug' app.get '/:template.html', (req, res) -> res.render (req.params.template + '.pug') -con = mysql.createConnection - host: 'tcgsql' - user: 'fftcg' - database: 'fftcg' - password: 'juchie5OhH6eiQuujaoquievezoe9iXe' - -con.connect (err) -> - if err - throw err - console.log 'Connected to TCGSQL!' - return - # Server logic io.on 'connection', (socket) -> console.log 'a user connected' socket.on 'disconnect', -> - # console.log('user disconnected'); + console.log 'a user disconnected' return socket.on 'login', (msg) -> console.log 'message:', msg.uname, msg.password - saltRounds = 13 + fftcgdb.adduser msg.uname, msg.password - bcrypt.hash msg.password, saltRounds, (err, hash) -> - con.query 'INSERT INTO `users` (nick, pwd_hash) VALUES (\'' + msg.uname + '\', \'' + hash + '\');', (err, result) -> - if err - throw err - console.log 'Result: ' + result - return - return +# Create server +web.listen 3000, -> + console.log '[FFTCG] Listening on port 3000 ...' - return - - return +# Handle termination +process.on 'SIGINT', -> + console.log '[FFTCG] shutting down after SIGINT' + process.exit() diff --git a/users.sql b/users.sql deleted file mode 100644 index 46e0328..0000000 --- a/users.sql +++ /dev/null @@ -1,22 +0,0 @@ --- Adminer 4.6.3 MySQL dump - -SET NAMES utf8; -SET time_zone = '+00:00'; -SET foreign_key_checks = 0; -SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO'; - -DROP DATABASE IF EXISTS `fftcg`; -CREATE DATABASE `fftcg` /*!40100 DEFAULT CHARACTER SET utf8 */; -USE `fftcg`; - -DROP TABLE IF EXISTS `users`; -CREATE TABLE `users` ( - `id` int(10) unsigned NOT NULL AUTO_INCREMENT, - `nick` varchar(100) NOT NULL, - `pwd_hash` char(64) NOT NULL, - `socket_id` varchar(64) DEFAULT NULL, - PRIMARY KEY (`id`) -) ENGINE=Aria DEFAULT CHARSET=utf8 PAGE_CHECKSUM=1; - - --- 2018-10-04 00:24:51