Lägg till autentisering och loggning, refaktorisera uppgifter till router
This commit is contained in:
98
logger.js
Normal file
98
logger.js
Normal file
@@ -0,0 +1,98 @@
|
||||
const { createLogger, format, transports } = require('winston');
|
||||
|
||||
const logger = createLogger({
|
||||
level: 'info',
|
||||
format: format.combine(
|
||||
format.timestamp(),
|
||||
format.json()
|
||||
),
|
||||
transports: [
|
||||
new transports.Console(),
|
||||
new transports.File({ filename: 'app.log' })
|
||||
]
|
||||
});
|
||||
|
||||
module.exports = logger;
|
||||
|
||||
// filepath: /home/fredrik/dev/org-todo-pwa/routes/tasks.js
|
||||
const express = require('express');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const logger = require('../logger');
|
||||
const auth = require('../middleware/auth');
|
||||
|
||||
const router = express.Router();
|
||||
const dataDir = '/data';
|
||||
|
||||
// Ensure the /data directory exists
|
||||
if (!fs.existsSync(dataDir)) {
|
||||
fs.mkdirSync(dataDir, { recursive: true });
|
||||
}
|
||||
|
||||
// Ensure the tags.json file exists
|
||||
const tagsFilePath = path.join(dataDir, 'tags.json');
|
||||
if (!fs.existsSync(tagsFilePath)) {
|
||||
fs.writeFileSync(tagsFilePath, JSON.stringify([]));
|
||||
}
|
||||
|
||||
// Protect the /add-task endpoint with authentication
|
||||
router.post('/add-task', auth, async (req, res) => {
|
||||
const { subject, description, scheduled } = req.body;
|
||||
const currentDateTime = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
|
||||
|
||||
let orgFormattedData = `
|
||||
* TODO ${subject}
|
||||
SCHEDULED: <${scheduled}>
|
||||
:LOGBOOK:
|
||||
- State "TODO" from "TODO" [${currentDateTime}]
|
||||
:END:
|
||||
`;
|
||||
|
||||
if (description) {
|
||||
orgFormattedData = `
|
||||
* TODO ${subject}
|
||||
${description}
|
||||
SCHEDULED: <${scheduled}>
|
||||
:LOGBOOK:
|
||||
- State "TODO" from "TODO" [${currentDateTime}]
|
||||
:END:
|
||||
`;
|
||||
}
|
||||
|
||||
const filePath = path.join(dataDir, 'tasks.org');
|
||||
try {
|
||||
await fs.promises.appendFile(filePath, orgFormattedData);
|
||||
res.send({ message: 'Task added successfully!' });
|
||||
} catch (err) {
|
||||
logger.error('Error writing to file:', err);
|
||||
res.status(500).send('Error writing to file.');
|
||||
}
|
||||
});
|
||||
|
||||
// Endpoint to save tags
|
||||
router.post('/save-tags', auth, async (req, res) => {
|
||||
const { tags } = req.body;
|
||||
const filePath = path.join(dataDir, 'tags.json');
|
||||
try {
|
||||
await fs.promises.writeFile(filePath, JSON.stringify(tags));
|
||||
res.send({ message: 'Tags saved successfully!' });
|
||||
} catch (err) {
|
||||
logger.error('Error saving tags:', err);
|
||||
res.status(500).send('Error saving tags.');
|
||||
}
|
||||
});
|
||||
|
||||
// Endpoint to retrieve tags
|
||||
router.get('/get-tags', auth, async (req, res) => {
|
||||
const filePath = path.join(dataDir, 'tags.json');
|
||||
try {
|
||||
const data = await fs.promises.readFile(filePath);
|
||||
const tags = JSON.parse(data);
|
||||
res.send(tags);
|
||||
} catch (err) {
|
||||
logger.error('Error retrieving tags:', err);
|
||||
res.status(500).send('Error retrieving tags.');
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user