Implement tag management with SQLite; update save and retrieve endpoints
This commit is contained in:
48
server.js
48
server.js
@@ -4,6 +4,7 @@ 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');
|
||||
@@ -12,6 +13,21 @@ 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'));
|
||||
@@ -27,8 +43,7 @@ app.use(session({
|
||||
ttl: 30 * 24 * 60 * 60 // 1 month
|
||||
}),
|
||||
cookie: {
|
||||
//secure: process.env.NODE_ENV === 'production', // Ensure cookies are only sent over HTTPS in production
|
||||
secure: false,
|
||||
secure: process.env.NODE_ENV === 'production', // Ensure cookies are only sent over HTTPS in production
|
||||
maxAge: 30 * 24 * 60 * 60 * 1000 // 1 month
|
||||
}
|
||||
}));
|
||||
@@ -36,6 +51,35 @@ app.use(session({
|
||||
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}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user