const express = require('express'); const http = require('http'); const fs = require('fs'); const fetch = require('isomorphic-fetch'); const path = require('path'); const cors = require('cors'); require('dotenv').config(); const msGraphHelper = require('./msGraphHelper'); const settings = require('./settings'); const HTTP_PORT = process.env.HTTP_PORT || 8080; const app = express(); const slideshowDir = './slideshow'; app.use(express.static(path.join(__dirname, 'public'))); app.use(cors({ origin: 'http://localhost:5173' })); app.use(express.json()); app.use(express.static('public')); // GRAPH API SECTION - USER AUTH // // msGraphHelper.initializeGraphForUserAuth(settings, async (deviceCodeResponse) => { // if (!deviceCodeResponse) { // throw new Error("Device code response is undefined."); // } // console.log(deviceCodeResponse.message); // }); // // GRAPH API SECTION - APP ONLY AUTH // msGraphHelper.initializeGraphForAppOnlyAuth(settings); const data = fs.readFileSync('./resourceUPNs.json', 'utf8'); const resourceUPNs = JSON.parse(data); app.get('/api/events', async (req, res) => { try { let now = new Date(); let startDateTime = new Date(now); // startDateTime.setHours(8, 0, 0, 0); let endDateTime = new Date(now); endDateTime.setHours(20, 0, 0, 0); if (startDateTime > endDateTime) { // Exit early since there are no relevant events to display return res.json({}); } let events = {}; for (const resource of resourceUPNs) { try { const data = await msGraphHelper.getCalendarView(resource, startDateTime.toISOString(), endDateTime.toISOString()); // fs.readFileSync(`./events/${resource}.json`, 'utf8'); const resourceEvents = data; events[resource] = resourceEvents; // console.log(`${new Date()}: Grabbed events for ${resource}`); } catch (error) { console.error(error); } } res.json(events); } catch (err) { res.status(500).json({ message: err.message }); } }); app.get('/api/weather', async (req, res) => { try { const lat = 45.49; const lon = -73.58; const appId = process.env.OPENWEATHER_APPID; const url = `https://api.openweathermap.org/data/2.5/weather?units=metric&lat=${lat}&lon=${lon}&appid=${appId}`; const response = await fetch(url); if (response.ok) { const json = await response.json(); res.json(json); } else { res.status(500).json({ message: 'Error in /weather' }); } } catch (err) { res.status(500).json({ message: err.message }); } }); app.get('/api/quotes', async (req, res) => { try { const url = `https://api.quotable.io/quotes?limit=150&tags=urban|city|technology|future` const response = await fetch(url); if (response.ok) { const json = await response.json(); res.json(json); } else { res.status(500).json({ message: 'Error in /quotes' }); } } catch (err) { res.status(500).json({ message: err.message }); } }); app.use('/slideshow', express.static(slideshowDir)); app.get('/api/slideshow', (req, res) => { fs.readdir(slideshowDir, (err, files) => { if (err) { console.error('Failed to list slideshow images: ', err.message); res.status(500).json({ message: err.message }); return; } else { const imagePaths = files .filter(file => /\.(jpg|jpeg|png|gif)$/i.test(file)) .map(file => `/slideshow/${file}`); res.json(imagePaths); } }); }); const httpServer = http.createServer(app); httpServer.listen(HTTP_PORT, () => console.log(`HTTP Server listening on http://localhost:${HTTP_PORT}`));