const express = require('express'); const bodyParser = require('body-parser'); const fs = require('fs'); const path = require('path'); const basicAuth = require('basic-auth'); const app = express(); const port = 3044; app.use(bodyParser.json()); app.use(express.static('public')); // Ensure the /data directory exists const dataDir = path.join('/data'); if (!fs.existsSync(dataDir)) { fs.mkdirSync(dataDir, { recursive: true }); } // Authentication middleware const auth = (req, res, next) => { const user = basicAuth(req); const username = 'fredrik'; // Replace with your desired username const password = 'apa'; // Replace with your desired password if (user && user.name === username && user.pass === password) { return next(); } else { res.set('WWW-Authenticate', 'Basic realm="401"'); return res.status(401).send('Authentication required.'); } }; // Protect the /add-task endpoint with authentication app.post('/add-task', auth, (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'); fs.appendFile(filePath, orgFormattedData, (err) => { if (err) { return res.status(500).send('Error writing to file.'); } res.send({ message: 'Task added successfully!' }); }); }); app.listen(port, () => { console.log(`Server running at http://localhost:${port}`); });