v2.0: SQLite statt PostgreSQL (keine externe DB nötig)

This commit is contained in:
Hermes
2026-08-12 08:17:20 +00:00
parent 7088e5e879
commit 4b2a76e9c1
3 changed files with 39 additions and 49 deletions
+30 -46
View File
@@ -1,33 +1,22 @@
const express = require('express');
const { Pool } = require('pg');
const Database = require('better-sqlite3');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// PostgreSQL connection
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: process.env.DB_SSL === 'true' ? { rejectUnauthorized: false } : false,
});
// Initialize DB table
async function initDB() {
const client = await pool.connect();
try {
await client.query(`
CREATE TABLE IF NOT EXISTS todos (
id SERIAL PRIMARY KEY,
text TEXT NOT NULL,
done BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT NOW()
)
`);
console.log('⚡ DATABASE ONLINE ⚡');
} finally {
client.release();
}
}
// SQLite – keine externe DB nötig
const db = new Database('/data/todos.db');
db.pragma('journal_mode = WAL');
db.exec(`
CREATE TABLE IF NOT EXISTS todos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT NOT NULL,
done INTEGER DEFAULT 0,
created_at TEXT DEFAULT (datetime('now'))
)
`);
console.log('⚡ DATABASE ONLINE (SQLite) ⚡');
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
@@ -35,44 +24,39 @@ app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Routes
app.get('/', async (req, res) => {
app.get('/', (req, res) => {
try {
const result = await pool.query('SELECT * FROM todos ORDER BY created_at DESC');
res.render('index', { todos: result.rows, error: null });
const todos = db.prepare('SELECT * FROM todos ORDER BY created_at DESC').all();
res.render('index', { todos, error: null });
} catch (err) {
res.render('index', { todos: [], error: 'DATABASE CONNECTION LOST ⚠' });
res.render('index', { todos: [], error: 'DATABASE ERROR ⚠' });
}
});
app.post('/add', async (req, res) => {
app.post('/add', (req, res) => {
const { text } = req.body;
if (text && text.trim()) {
await pool.query('INSERT INTO todos (text) VALUES ($1)', [text.trim()]);
db.prepare('INSERT INTO todos (text) VALUES (?)').run(text.trim());
}
res.redirect('/');
});
app.post('/toggle/:id', async (req, res) => {
await pool.query('UPDATE todos SET done = NOT done WHERE id = $1', [req.params.id]);
app.post('/toggle/:id', (req, res) => {
db.prepare('UPDATE todos SET done = NOT done WHERE id = ?').run(req.params.id);
res.redirect('/');
});
app.post('/delete/:id', async (req, res) => {
await pool.query('DELETE FROM todos WHERE id = $1', [req.params.id]);
app.post('/delete/:id', (req, res) => {
db.prepare('DELETE FROM todos WHERE id = ?').run(req.params.id);
res.redirect('/');
});
// Health check
app.get('/health', (req, res) => res.json({ status: 'ONLINE', db: 'CONNECTED' }));
app.get('/health', (req, res) => res.json({ status: 'ONLINE', db: 'SQLITE' }));
initDB().then(() => {
app.listen(PORT, () => {
console.log(`╔══════════════════════════════════════╗`);
console.log(`║ ⚡ TECHNO-TODO v1.0 ONLINE ⚡ ║`);
console.log(`║ PORT: ${PORT} ║`);
console.log(`╚══════════════════════════════════════╝`);
});
}).catch(err => {
console.error('Failed to init DB:', err);
process.exit(1);
app.listen(PORT, () => {
console.log(`╔══════════════════════════════════════╗`);
console.log(`║ ⚡ TECHNO-TODO v2.0 ONLINE ⚡ ║`);
console.log(`║ PORT: ${PORT} ║`);
console.log(`║ DB: SQLite (no externals) ║`);
console.log(`╚══════════════════════════════════════╝`);
});