63 lines
2.0 KiB
JavaScript
63 lines
2.0 KiB
JavaScript
const express = require('express');
|
||
const Database = require('better-sqlite3');
|
||
const path = require('path');
|
||
|
||
const app = express();
|
||
const PORT = process.env.PORT || 3000;
|
||
|
||
// 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')));
|
||
app.set('view engine', 'ejs');
|
||
app.set('views', path.join(__dirname, 'views'));
|
||
|
||
// Routes
|
||
app.get('/', (req, res) => {
|
||
try {
|
||
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 ERROR ⚠' });
|
||
}
|
||
});
|
||
|
||
app.post('/add', (req, res) => {
|
||
const { text } = req.body;
|
||
if (text && text.trim()) {
|
||
db.prepare('INSERT INTO todos (text) VALUES (?)').run(text.trim());
|
||
}
|
||
res.redirect('/');
|
||
});
|
||
|
||
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', (req, res) => {
|
||
db.prepare('DELETE FROM todos WHERE id = ?').run(req.params.id);
|
||
res.redirect('/');
|
||
});
|
||
|
||
app.get('/health', (req, res) => res.json({ status: 'ONLINE', db: 'SQLITE' }));
|
||
|
||
app.listen(PORT, () => {
|
||
console.log(`╔══════════════════════════════════════╗`);
|
||
console.log(`║ ⚡ TECHNO-TODO v2.0 ONLINE ⚡ ║`);
|
||
console.log(`║ PORT: ${PORT} ║`);
|
||
console.log(`║ DB: SQLite (no externals) ║`);
|
||
console.log(`╚══════════════════════════════════════╝`);
|
||
});
|