1
0
Fork 0

feat: restructure starting with customizable port

This commit is contained in:
xeruf 2024-11-07 01:05:41 +01:00
parent 2a9af9c1d5
commit eacf6111e1
3 changed files with 22 additions and 22 deletions

10
app.js Normal file
View File

@ -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}`);
});

View File

@ -1,9 +1,9 @@
{ {
"name": "calendar-merger", "name": "calendar-merger",
"version": "1.0.0", "version": "1.1.0",
"type": "module", "type": "module",
"scripts": { "scripts": {
"start": "node server.js", "start": "node app.js",
"test": "jest" "test": "jest"
}, },
"dependencies": { "dependencies": {

View File

@ -5,32 +5,31 @@ import axios from 'axios';
import path from 'path'; import path from 'path';
import icalGenerator from 'ical-generator'; import icalGenerator from 'ical-generator';
const app = express(); const server = express();
app.use(express.json()); server.use(express.json());
// Determine the merged calendars directory based on the environment // Determine the merged calendars directory based on the environment
const MERGED_CALENDARS_DIR = 'calendar'; const MERGED_CALENDARS_DIR = 'calendar';
console.log(`Merged calendars directory: ${MERGED_CALENDARS_DIR} under ${process.cwd()}`); console.log(`Merged calendars directory: ${MERGED_CALENDARS_DIR} under ${process.cwd()}`);
// Ensure the merged calendars directory exists // Ensure the merged calendars directory exists
fs.mkdirSync(MERGED_CALENDARS_DIR, { recursive: true }); 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.setHeader('Content-Type', 'application/javascript');
res.sendFile('script.js', { root: '.' }); 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 to sanitize the linkGroupName for use as a filename
function sanitizeFilename(filename) { function sanitizeFilename(filename) {
return filename.replace(/[<>:"/\\|?* ]/g, '_'); // Replace invalid characters with underscores return filename.replace(/[<>:"/\\|?* ]/g, '_'); // Replace invalid characters with underscores
} }
// Merge calendars endpoint // Merge calendars endpoint
app.post('/merge', async (req, res) => { server.post('/merge', async (req, res) => {
const { linkGroupName, calendars } = req.body; const { linkGroupName, calendars } = req.body;
try { try {
@ -131,7 +130,7 @@ app.post('/merge', async (req, res) => {
}); });
// Serve the merged calendar file and refresh if older than an hour // 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 calendarName = req.params.name;
const icsFilePath = path.resolve(MERGED_CALENDARS_DIR, `${calendarName}.ics`); const icsFilePath = path.resolve(MERGED_CALENDARS_DIR, `${calendarName}.ics`);
const jsonFilePath = path.resolve(MERGED_CALENDARS_DIR, `${calendarName}.json`); const jsonFilePath = path.resolve(MERGED_CALENDARS_DIR, `${calendarName}.json`);
@ -238,13 +237,4 @@ function saveCalendarData(calendarId, linkGroupName, calendars) {
} }
} }
// Start the server export default server;
if (process.env.NODE_ENV !== 'test') {
const port = 3000;
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});
}
export default app;