mysql -> sqlite DB, own db module

This commit is contained in:
Jörn-Michael Miehe 2018-12-07 10:38:46 +01:00
parent 5e05996acd
commit 492950544e
6 changed files with 69 additions and 87 deletions

View file

@ -1,28 +1,6 @@
version: "2" version: "2"
volumes:
mariadata:
services: 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: fftcg:
build: . build: .
command: "yarn debug" command: "yarn debug"
@ -30,5 +8,6 @@ services:
volumes: volumes:
- "${PWD}/src:/app/src" - "${PWD}/src:/app/src"
- "${PWD}/public_html:/app/public_html" - "${PWD}/public_html:/app/public_html"
- "${PWD}/fftcg.db:/app/fftcg.db"
ports: ports:
- "8001:8080" - "3000:3000"

BIN
fftcg.db Normal file

Binary file not shown.

43
inc/fftcgdb.coffee Normal file
View file

@ -0,0 +1,43 @@
# libraries
bcrypt = (require 'bcrypt')
sqlite3 = (require 'sqlite3').verbose()
FFTCGDB =
open: (filename) ->
@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()

View file

@ -44,9 +44,9 @@
"coffeescript": "^2.3.2", "coffeescript": "^2.3.2",
"express": "^4.16.4", "express": "^4.16.4",
"helmet": "^3.15.0", "helmet": "^3.15.0",
"mysql": "^2.16.0",
"pug": "^2.0.3", "pug": "^2.0.3",
"socket.io": "^2.2.0", "socket.io": "^2.2.0",
"socket.io-client": "^2.2.0" "socket.io-client": "^2.2.0",
"sqlite3": "^4.0.4"
} }
} }

View file

@ -1,21 +1,18 @@
# libraries # node libraries
bcrypt = require('bcrypt') express = (require 'express')
express = require('express') helmet = (require 'helmet')
helmet = require('helmet') http = (require 'http')
http = require('http') path = (require 'path')
mysql = require('mysql') socketio = (require 'socket.io')
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() app = express()
web = http.Server(app) web = http.Server app
io = socketio(web) io = socketio web
# Listen server
web.listen 8080, ->
console.log 'Node.js listening on port 8080'
return
app.use helmet() app.use helmet()
@ -27,39 +24,24 @@ app.set 'view engine', 'pug'
app.get '/:template.html', (req, res) -> app.get '/:template.html', (req, res) ->
res.render (req.params.template + '.pug') 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 # Server logic
io.on 'connection', (socket) -> io.on 'connection', (socket) ->
console.log 'a user connected' console.log 'a user connected'
socket.on 'disconnect', -> socket.on 'disconnect', ->
# console.log('user disconnected'); console.log 'a user disconnected'
return return
socket.on 'login', (msg) -> socket.on 'login', (msg) ->
console.log 'message:', msg.uname, msg.password console.log 'message:', msg.uname, msg.password
saltRounds = 13 fftcgdb.adduser msg.uname, msg.password
bcrypt.hash msg.password, saltRounds, (err, hash) -> # Create server
con.query 'INSERT INTO `users` (nick, pwd_hash) VALUES (\'' + msg.uname + '\', \'' + hash + '\');', (err, result) -> web.listen 3000, ->
if err console.log '[FFTCG] Listening on port 3000 ...'
throw err
console.log 'Result: ' + result
return
return
return # Handle termination
process.on 'SIGINT', ->
return console.log '[FFTCG] shutting down after SIGINT'
process.exit()

View file

@ -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