Compare commits
1 Commits
Author | SHA1 | Date |
---|---|---|
xeruf | eacf6111e1 |
|
@ -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}`);
|
||||
});
|
|
@ -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": {
|
||||
|
|
30
server.js
30
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;
|
||||
|
|
Loading…
Reference in New Issue