Commit 56e097f8 authored by Bryan Tong's avatar Bryan Tong

Merge branch 'addMysql' into 'master'

Add MySQL backed sessions.

See merge request kado/kado-web!56
parents bb0618c3 39b6e4eb
......@@ -32,16 +32,53 @@ if (process.env.command !== 'session' && cluster.isMaster()) {
} else {
// declare our application
const Application = require('kado/lib/Application')
const Database = require('kado/lib/Database')
const HyperText = require('kado/lib/HyperText')
const Log = require('kado/lib/Log')
const Main = require('./lib/Main')
const Parser = require('kado/lib/Parser')
const Model = require('./lib/Model')
const Session = require('kado/lib/Session')
const VersionSwitch = require('./lib/VersionSwitch')
const ViewMustache = require('kado/lib/View').ViewMustache
const app = Application.getInstance()
app.setName('kado-web')
app.setVersion(require('./package').version)
const pkg = require('./package')
app.setName(pkg.name)
app.setVersion(pkg.version)
app.models = new Model.ModelManager()
app.models.push(Session.SessionStoreModel)
app.trustProxy = cfg.main.trustProxy || false
// add our logger
const log = Log.LogEngine.getInstance({ name: pkg.name })
app.log.addEngine('console', log)
// add mysql connection
app.database.addEngine('mysql', new Database.MySQL(cfg.mysql))
// add a command to create the database schema
app.cli.command('db', 'test', {
action: () => { app.log.info('Database OK.') }
})
app.cli.command('db', 'init', {
action: async (opts) => {
const db = app.database.getEngine('mysql').getEngine()
const models = app.models.all()
let created = 0
try {
for (const i in models) {
if (!Object.prototype.hasOwnProperty.call(models, i)) continue
const model = models[i]
if (opts.name && model.name !== opts.name) return
const query = model.createTable()
await db.execute(query.toString(), query.toArray())
created++
}
app.log.info(
`Table creation complete! Created ${created} model(s).`)
} catch (e) {
app.log.error('Database initialization failed: ' + e.message)
app.log.dump(e)
process.exit(11)
}
}
})
// add an http server
const http = new HyperText.HyperTextServer()
http.setHost(cfg.main.host)
......@@ -63,20 +100,24 @@ if (process.env.command !== 'session' && cluster.isMaster()) {
req.query = new URL(`http://localhost${req.url}`).searchParams
})
// add a session handler
app.use((req) => { req.cookie = Parser.cookie('' + req.headers.cookie) })
app.use(Session.getMiddleware({
secret: cfg.main.cookieSecret || 'kado-web',
store: new Session.SessionStoreLocal()
secret: cfg.main.cookieSecret || 'burstcloud',
store: new Session.SessionStoreSQL({
db: app.database.getEngine('mysql'),
model: Session.SessionStoreModel
})
}))
// set the version to session if its in the query string and then redirect
app.use(VersionSwitch.getMiddleware(cfg.main.versionList))
// install routes
Main.register(app)
// initiate communications with the master and startup
Cluster.child(
http.getEngine(),
'kado-web:worker',
() => { return app.start().then(() => { return app.listen() }) },
() => { return app.stop() }
)
if (app.isCommand(process.argv)) { app.start(process.argv) } else {
Cluster.child(
http.getEngine(),
'kado-web:worker',
() => { return app.start().then(() => { return app.listen() }) },
() => { return app.stop() }
)
}
}
......@@ -8,6 +8,13 @@ const cfg = {
cookieSecret: null,
trustProxy: true,
versionList: ['4.x', '3.x']
},
mysql: {
host: 'localhost',
user: 'kado',
password: 'kado',
database: 'kado',
driver: 'mariadb'
}
}
const localConfigFile = fs.path.resolve('./config.local.js')
......
'use strict'
const Assert = require('kado/lib/Assert')
const KadoModel = require('kado/lib/Model')
class Model extends KadoModel {
static async listActionRoute (db, query, data = {}, idField = 'id') {
let rv, action
if (data.action instanceof Array) action = data.action
else action = [data.action]
if (data.delete) {
let deleted = 0
action.forEach(async (id) => {
query.where(idField).value(id)
rv = await db.execute(query.toString(), query.toArray())
Assert.isType('Array', rv)
deleted++
})
return { deleted: deleted }
}
return false
}
}
class ModelManager {
constructor () {
this.models = {}
}
add (model) { return this.push(model) }
push (model) {
let found = 0
for (const i in this.models) {
if (!Object.prototype.hasOwnProperty.call(model, i)) continue
if (model.constructor.name === this.models[i].constructor.name) {
found++
}
}
if (!found) this.models[Object.keys(this.models).length] = model
}
remove (model) {
let removed = 0
for (const i in this.models) {
if (!Object.prototype.hasOwnProperty.call(model, i)) continue
if (model.constructor.name === this.models[i].constructor.name) {
delete this.models[i]
removed++
}
}
return removed
}
all () { return this.models }
}
Model.ModelManager = ModelManager
module.exports = Model
......@@ -4,6 +4,16 @@
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@types/geojson": {
"version": "7946.0.7",
"resolved": "https://registry.npmjs.org/@types/geojson/-/geojson-7946.0.7.tgz",
"integrity": "sha512-wE2v81i4C4Ol09RtsWFAqg3BUitWbHSpSlIo+bNdsCJijO9sjme+zm+73ZMCa/qMC8UEERxzGbvmr1cffo2SiQ=="
},
"@types/node": {
"version": "13.13.15",
"resolved": "https://registry.npmjs.org/@types/node/-/node-13.13.15.tgz",
"integrity": "sha512-kwbcs0jySLxzLsa2nWUAGOd/s21WU1jebrEdtzhsj1D4Yps1EOuyI1Qcu+FD56dL7NRNIJtDDjcqIG22NwkgLw=="
},
"ansi-regex": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz",
......@@ -57,6 +67,11 @@
"integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=",
"dev": true
},
"denque": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/denque/-/denque-1.4.1.tgz",
"integrity": "sha512-OfzPuSZKGcgr96rf1oODnfjqBFmr1DVoc/TrItj3Ohe0Ah1C5WX5Baquw/9U9KovnQ88EqmJbD66rKYUQYN1tQ=="
},
"emoji-regex": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz",
......@@ -84,6 +99,14 @@
"integrity": "sha512-9FG7SSzv9yOY5CGGxfI6NDm7xLYtMOjKtPBxw7Zff3t5UcRcUNTGEeS8lNjhceL34KeetLMoGMFTGoaa83HwyQ==",
"dev": true
},
"iconv-lite": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.5.2.tgz",
"integrity": "sha512-kERHXvpSaB4aU3eANwidg79K8FlrN77m8G9V+0vOR3HYaRifrlwMEpT7ZBJqLSEIHnEgJTHcWK82wwLwwKwtag==",
"requires": {
"safer-buffer": ">= 2.1.2 < 3"
}
},
"is-fullwidth-code-point": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz",
......@@ -91,9 +114,8 @@
"dev": true
},
"kado": {
"version": "4.2.1",
"resolved": "https://registry.npmjs.org/kado/-/kado-4.2.1.tgz",
"integrity": "sha512-LeNXIOTWH+WhKzeyT8f2nY55YU1EqVqLVdziKGIsjJqjn+n9zKTIvjKqD/IyfyTyP2MmeRT4N2fEfs/piRo6dQ=="
"version": "git+https://git.nullivex.com/kado/kado.git#3ae71e17bfab17f002f8cbe9b801dd29cf1e73c9",
"from": "git+https://git.nullivex.com/kado/kado.git"
},
"locate-path": {
"version": "3.0.0",
......@@ -105,6 +127,38 @@
"path-exists": "^3.0.0"
}
},
"long": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz",
"integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA=="
},
"mariadb": {
"version": "2.4.2",
"resolved": "https://registry.npmjs.org/mariadb/-/mariadb-2.4.2.tgz",
"integrity": "sha512-ybK520CO/8Z74MNJ77dEwXjRMhxMbQdpPia+/uJakJWiZoLEmuwSHqs51qWr4FE5WcgHQfL5KCmOiHErR/LpTg==",
"requires": {
"@types/geojson": "^7946.0.7",
"@types/node": "^13.9.8",
"denque": "^1.4.1",
"iconv-lite": "^0.5.1",
"long": "^4.0.0",
"moment-timezone": "^0.5.31",
"please-upgrade-node": "^3.2.0"
}
},
"moment": {
"version": "2.27.0",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.27.0.tgz",
"integrity": "sha512-al0MUK7cpIcglMv3YF13qSgdAIqxHTO7brRtaz3DlSULbqfazqkc5kEjNrLDOM7fsjshoFIihnU8snrP7zUvhQ=="
},
"moment-timezone": {
"version": "0.5.31",
"resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.31.tgz",
"integrity": "sha512-+GgHNg8xRhMXfEbv81iDtrVeTcWt0kWmTEY1XQK14dICTXnWJnT0dxdlPspwqF3keKMVPXwayEsk1DI0AA/jdA==",
"requires": {
"moment": ">= 2.9.0"
}
},
"p-limit": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz",
......@@ -135,6 +189,14 @@
"integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=",
"dev": true
},
"please-upgrade-node": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/please-upgrade-node/-/please-upgrade-node-3.2.0.tgz",
"integrity": "sha512-gQR3WpIgNIKwBMVLkpMUeR3e1/E1y42bqDQZfql+kDeXd8COYfM8PQA4X6y7a8u9Ua9FHmsrrmirW2vHs45hWg==",
"requires": {
"semver-compare": "^1.0.0"
}
},
"require-directory": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
......@@ -147,6 +209,16 @@
"integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==",
"dev": true
},
"safer-buffer": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
},
"semver-compare": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/semver-compare/-/semver-compare-1.0.0.tgz",
"integrity": "sha1-De4hahyUGrN+nvsXiPavxf9VN/w="
},
"set-blocking": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz",
......
......@@ -19,7 +19,8 @@
},
"main": "./app.js",
"dependencies": {
"kado": "^4.x"
"kado": "git+https://git.nullivex.com/kado/kado.git",
"mariadb": "^2.4.2"
},
"devDependencies": {
"highlight.js": "^10.0.3",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment