86 lines
2.6 KiB
JavaScript
86 lines
2.6 KiB
JavaScript
require('dotenv').config();
|
|
const express = require('express');
|
|
const bodyParser = require('body-parser');
|
|
const session = require('express-session');
|
|
const cookieParser = require('cookie-parser');
|
|
const SQLiteStore = require('connect-sqlite3')(session);
|
|
const sqlite3 = require('sqlite3').verbose();
|
|
const tasksRouter = require('./routes/tasks');
|
|
const authRouter = require('./routes/auth');
|
|
const authMiddleware = require('./middleware/auth');
|
|
const logger = require('./logger');
|
|
|
|
const app = express();
|
|
const port = 3044;
|
|
|
|
const db = new sqlite3.Database('/data/sessions.sqlite', (err) => {
|
|
if (err) {
|
|
console.error('Error opening database:', err);
|
|
} else {
|
|
db.run(`CREATE TABLE IF NOT EXISTS tags (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
tag TEXT UNIQUE
|
|
)`, (err) => {
|
|
if (err) {
|
|
console.error('Error creating tags table:', err);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
app.use(bodyParser.json());
|
|
app.use(cookieParser());
|
|
app.use(express.static('public'));
|
|
|
|
// Configure session middleware with SQLite store
|
|
app.use(session({
|
|
secret: process.env.SESSION_SECRET || 'default_secret', // Use a strong secret in production
|
|
resave: false,
|
|
saveUninitialized: false,
|
|
store: new SQLiteStore({
|
|
db: 'sessions.sqlite',
|
|
dir: '/data',
|
|
ttl: 30 * 24 * 60 * 60 // 1 month
|
|
}),
|
|
cookie: {
|
|
secure: process.env.NODE_ENV === 'production', // Ensure cookies are only sent over HTTPS in production
|
|
maxAge: 30 * 24 * 60 * 60 * 1000 // 1 month
|
|
}
|
|
}));
|
|
|
|
app.use('/', authRouter);
|
|
app.use('/', authMiddleware, tasksRouter);
|
|
|
|
// Add routes for handling tags
|
|
app.post('/save-tags', authMiddleware, (req, res) => {
|
|
const { tags } = req.body;
|
|
const placeholders = tags.map(() => '(?)').join(',');
|
|
const sql = `INSERT OR IGNORE INTO tags (tag) VALUES ${placeholders}`;
|
|
|
|
db.run(sql, tags, function(err) {
|
|
if (err) {
|
|
logger.error('Error saving tags:', err);
|
|
res.status(500).send('Error saving tags.');
|
|
} else {
|
|
res.send({ message: 'Tags saved successfully!' });
|
|
logger.info(`New tags saved: ${tags}`);
|
|
}
|
|
});
|
|
});
|
|
|
|
app.get('/get-tags', authMiddleware, (req, res) => {
|
|
db.all('SELECT tag FROM tags', [], (err, rows) => {
|
|
if (err) {
|
|
logger.error('Error retrieving tags:', err);
|
|
res.status(500).json({ error: 'Error retrieving tags' });
|
|
} else {
|
|
const tags = rows.map(row => row.tag);
|
|
res.json(tags);
|
|
}
|
|
});
|
|
});
|
|
|
|
app.listen(port, () => {
|
|
logger.info(`Server running at http://localhost:${port}`);
|
|
});
|