diff --git a/app.js b/app.js new file mode 100644 index 0000000..9206c4f --- /dev/null +++ b/app.js @@ -0,0 +1,10 @@ +import request from 'supertest'; +import express from 'express'; +import fs from 'fs'; +import path from 'path'; +import server from './server.js'; + +const port = process.env.NODE_PORT || 3000; +server.listen(port, () => { + console.log(`Server started on port ${port}`); +}); diff --git a/package.json b/package.json index 33fa084..053fce1 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "name": "calendar-merger", - "version": "1.0.0", + "version": "1.1.0", "type": "module", "scripts": { - "start": "node server.js", + "start": "node app.js", "test": "jest" }, "dependencies": { diff --git a/server.js b/server.js index 429ade8..e0b9019 100644 --- a/server.js +++ b/server.js @@ -5,32 +5,31 @@ import axios from 'axios'; import path from 'path'; import icalGenerator from 'ical-generator'; -const app = express(); -app.use(express.json()); +const server = express(); +server.use(express.json()); // Determine the merged calendars directory based on the environment const MERGED_CALENDARS_DIR = 'calendar'; console.log(`Merged calendars directory: ${MERGED_CALENDARS_DIR} under ${process.cwd()}`); - // Ensure the merged calendars directory exists fs.mkdirSync(MERGED_CALENDARS_DIR, { recursive: true }); -app.get('/script.js', (req, res) => { +// FRONTEND +server.get('/', (req, res) => { + res.sendFile('index.html', { root: '.' }); +}); +server.get('/script.js', (req, res) => { res.setHeader('Content-Type', 'application/javascript'); res.sendFile('script.js', { root: '.' }); }); -app.get('/', (req, res) => { - res.sendFile('index.html', { root: '.' }); -}); - // Function to sanitize the linkGroupName for use as a filename function sanitizeFilename(filename) { return filename.replace(/[<>:"/\\|?* ]/g, '_'); // Replace invalid characters with underscores } // Merge calendars endpoint -app.post('/merge', async (req, res) => { +server.post('/merge', async (req, res) => { const { linkGroupName, calendars } = req.body; try { @@ -131,7 +130,7 @@ app.post('/merge', async (req, res) => { }); // Serve the merged calendar file and refresh if older than an hour -app.get('/calendar/:name', async (req, res) => { +server.get('/calendar/:name', async (req, res) => { const calendarName = req.params.name; const icsFilePath = path.resolve(MERGED_CALENDARS_DIR, `${calendarName}.ics`); const jsonFilePath = path.resolve(MERGED_CALENDARS_DIR, `${calendarName}.json`); @@ -238,13 +237,4 @@ function saveCalendarData(calendarId, linkGroupName, calendars) { } } -// Start the server -if (process.env.NODE_ENV !== 'test') { - const port = 3000; - app.listen(port, () => { - console.log(`Server started on port ${port}`); - }); -} - - -export default app; +export default server;