mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Authetication, logger middleware, db create tool
This commit is contained in:
parent
5f0b17a0db
commit
ebd27f93c1
11 changed files with 164 additions and 94 deletions
39
utils/dbSetup.js
Normal file
39
utils/dbSetup.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
|
||||
const utils = require('../utils/utils.js')
|
||||
const dbtools = require('../utils/dbtools.js')
|
||||
const dbStructPath = '../modules/api/apiDBStruct.json'
|
||||
const usersDBPath = '../data/dbs/users.db'
|
||||
|
||||
let authDB
|
||||
|
||||
console.clear()
|
||||
|
||||
function CreateDB () {
|
||||
const dbStruct = utils.ReadJSON(dbStructPath)
|
||||
authDB = dbtools.GetDB(usersDBPath)
|
||||
|
||||
Object.keys(dbStruct).forEach((tableName) => {
|
||||
const tableData = dbStruct[tableName]
|
||||
dbtools.CreateTable(authDB, tableName, tableData.tableStruct, tableData.foreignKey)
|
||||
})
|
||||
|
||||
try {
|
||||
// TODO: fill with data
|
||||
dbtools.Insert(authDB, 'users', {
|
||||
pw: 2,
|
||||
notes: 'hemnlo'
|
||||
})
|
||||
dbtools.Insert(authDB, 'users', {
|
||||
pw: 1,
|
||||
notes: 'hemnlo'
|
||||
})
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
|
||||
// Object.keys(dbStruct).forEach((key) => {
|
||||
// console.log(key)
|
||||
// console.log(dbtools.TableInfo(authDB, key))
|
||||
// })
|
||||
}
|
||||
CreateDB()
|
|
@ -109,19 +109,41 @@ function Delete (db, table, conditions) {
|
|||
}
|
||||
}
|
||||
|
||||
function CreateTable (db, name, columns) {
|
||||
function CreateTable (db, name, columns, foreignKeys) {
|
||||
// CREATE TABLE users(pw text PRIMARY KEY NOT NULL, id number, lastIP text, notes text, loginCount
|
||||
// number, lastLogin text, lastAccess text
|
||||
//
|
||||
// FOREIGN KEY(songartist, songalbum) REFERENCES album(albumartist, albumname) )
|
||||
|
||||
try {
|
||||
const cols = Object.keys(columns).reduce((acc, key) => {
|
||||
const item = columns[key]
|
||||
const isPrimary = item.primary ? ' PRIMARY KEY' : ''
|
||||
const isNotNull = item.notNull ? ' NOT NULL' : ''
|
||||
// FIXME: array, and push stuff, then join()
|
||||
const flags = []
|
||||
const toCheck = {
|
||||
primary: 'PRIMARY KEY',
|
||||
notNull: 'NOT NULL',
|
||||
unique: 'UNIQUE',
|
||||
autoIncrement: 'AUTOINCREMENT'
|
||||
}
|
||||
Object.keys(toCheck).forEach((key) => {
|
||||
if (item[key]) {
|
||||
flags.push(toCheck[key])
|
||||
}
|
||||
})
|
||||
|
||||
acc.push(`${key} ${item.type}${isPrimary}${isNotNull}`)
|
||||
acc.push(`${key} ${item.type} ${flags.join(' ')}`)
|
||||
return acc
|
||||
}, []).join(', ')
|
||||
|
||||
// IF NOT EXISTS // TODO
|
||||
const s = `CREATE TABLE ${name}(${cols})`
|
||||
let fKeys = ''
|
||||
if (foreignKeys) {
|
||||
const { keysFrom, table, keysTo } = foreignKeys
|
||||
fKeys = `, FOREIGN KEY(${keysFrom.join(', ')}) REFERENCES ${table}(${keysTo.join(', ')})`
|
||||
}
|
||||
|
||||
// IF NOT EXISTS
|
||||
const s = `CREATE TABLE ${name}(${cols}${fKeys})`
|
||||
DebugLog(s)
|
||||
|
||||
const stmt = db.prepare(s)
|
||||
|
|
|
@ -125,7 +125,14 @@ function LogReq (req, toFile, sc) {
|
|||
hostname = 'NOHOST'
|
||||
Log('req.hostname is undefined! req.hostname: ' + req.hostname, GetColor('redbg'))
|
||||
}
|
||||
logEntry += dl + hostname + dl + req.headers['user-agent'] + dl + req.method + dl
|
||||
logEntry += dl +
|
||||
hostname + dl +
|
||||
req.headers['user-agent'] + dl +
|
||||
req.method + dl
|
||||
|
||||
if (req.session && req.session.user) {
|
||||
logEntry += C('cyan') + req.session.user.id + C() + dl
|
||||
}
|
||||
|
||||
logEntry += GetRandomColor(req.url.split('?')[0]) + req.url
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue