mirror of
https://github.com/R0m1k3/noteflow.git
synced 2026-10-22 22:58:15 +02:00
[dyad] wrote 2 file(s)
This commit is contained in:
1 parent
d92c4563ec
commit
204e66dd3a
2 files changed
+84
-10
No files matched your search
+83
-9
@@ -10,6 +10,7 @@ if (!fs.existsSync(dataDir)) {
|
|||||||
fs.mkdirSync(dataDir, { recursive: true });
|
fs.mkdirSync(dataDir, { recursive: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Configure logger
|
||||||
const logger = winston.createLogger({
|
const logger = winston.createLogger({
|
||||||
level: 'info',
|
level: 'info',
|
||||||
format: winston.format.combine(
|
format: winston.format.combine(
|
||||||
@@ -25,14 +26,7 @@ const logger = winston.createLogger({
|
|||||||
const dbPath = path.join(dataDir, 'notes.db');
|
const dbPath = path.join(dataDir, 'notes.db');
|
||||||
logger.info(`Using database at: ${dbPath}`);
|
logger.info(`Using database at: ${dbPath}`);
|
||||||
|
|
||||||
// Ensure database directory has correct permissions
|
// Create database connection
|
||||||
try {
|
|
||||||
fs.accessSync(dataDir, fs.constants.W_OK);
|
|
||||||
} catch (err) {
|
|
||||||
logger.error(`No write access to ${dataDir}`, err);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
const db = new sqlite3.Database(dbPath, (err) => {
|
const db = new sqlite3.Database(dbPath, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error('Database connection error:', err);
|
logger.error('Database connection error:', err);
|
||||||
@@ -42,4 +36,84 @@ const db = new sqlite3.Database(dbPath, (err) => {
|
|||||||
initializeDatabase();
|
initializeDatabase();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Rest of the file remains the same...
|
// Initialize database schema
|
||||||
|
function initializeDatabase() {
|
||||||
|
db.serialize(() => {
|
||||||
|
// Create users table
|
||||||
|
db.run(`
|
||||||
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
username TEXT UNIQUE NOT NULL,
|
||||||
|
password_hash TEXT NOT NULL,
|
||||||
|
is_admin INTEGER DEFAULT 0,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
)
|
||||||
|
`);
|
||||||
|
|
||||||
|
// Create notes table
|
||||||
|
db.run(`
|
||||||
|
CREATE TABLE IF NOT EXISTS notes (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
user_id INTEGER NOT NULL,
|
||||||
|
title TEXT NOT NULL,
|
||||||
|
content TEXT,
|
||||||
|
archived INTEGER DEFAULT 0,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
|
||||||
|
)
|
||||||
|
`);
|
||||||
|
|
||||||
|
// Create todos table
|
||||||
|
db.run(`
|
||||||
|
CREATE TABLE IF NOT EXISTS todos (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
note_id INTEGER NOT NULL,
|
||||||
|
text TEXT NOT NULL,
|
||||||
|
completed INTEGER DEFAULT 0,
|
||||||
|
position INTEGER DEFAULT 0,
|
||||||
|
FOREIGN KEY (note_id) REFERENCES notes (id) ON DELETE CASCADE
|
||||||
|
)
|
||||||
|
`);
|
||||||
|
|
||||||
|
// Create images table
|
||||||
|
db.run(`
|
||||||
|
CREATE TABLE IF NOT EXISTS images (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
note_id INTEGER NOT NULL,
|
||||||
|
filename TEXT NOT NULL,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY (note_id) REFERENCES notes (id) ON DELETE CASCADE
|
||||||
|
)
|
||||||
|
`);
|
||||||
|
|
||||||
|
// Check if admin user exists, create if not
|
||||||
|
db.get('SELECT * FROM users WHERE username = ?', ['admin'], async (err, user) => {
|
||||||
|
if (err) {
|
||||||
|
logger.error('Error checking admin user:', err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
try {
|
||||||
|
const hash = await bcrypt.hash('admin', 10);
|
||||||
|
db.run(
|
||||||
|
'INSERT INTO users (username, password_hash, is_admin) VALUES (?, ?, ?)',
|
||||||
|
['admin', hash, 1],
|
||||||
|
(err) => {
|
||||||
|
if (err) {
|
||||||
|
logger.error('Error creating admin user:', err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
logger.info('Admin user created successfully');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
logger.error('Error hashing admin password:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { db, logger };
|
||||||
@@ -2,7 +2,7 @@ const express = require('express');
|
|||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
const helmet = require('helmet');
|
const helmet = require('helmet');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { logger } = require('./config/database');
|
const { db, logger } = require('./config/database');
|
||||||
|
|
||||||
// Create Express app
|
// Create Express app
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|||||||
Reference in new issue
Block a user