@@ -3,14 +3,25 @@ import { loadConfig } from "./config";
33import { fetchTimetable } from "./webuntis" ;
44import { lessonsToIcs } from "./ics" ;
55import { CacheEntry } from "./types" ;
6+ import { sessionCache , SESSION_TTL_MS } from "./webuntis" ;
7+ import { CacheHandler } from "./cacheHandler" ;
68
79async function main ( ) {
810 const { config, configPath } = await loadConfig ( ) ;
911 console . log ( `Loaded config from ${ configPath } ` ) ;
1012
1113 const app = express ( ) ;
14+ const icsCache = new CacheHandler ( config . cacheDuration ) ;
1215
13- const icsCache = new Map < string , CacheEntry > ( ) ;
16+ function sendIcs ( res : express . Response , filename : string , ics : string ) {
17+ return res
18+ . setHeader ( "Content-Type" , "text/calendar" )
19+ . setHeader (
20+ "Content-Disposition" ,
21+ `attachment; filename=${ filename } .ics`
22+ )
23+ . send ( ics ) ;
24+ }
1425
1526 app . get ( "/timetable/:name" , async ( req , res ) => {
1627 try {
@@ -21,19 +32,9 @@ async function main() {
2132 ) ;
2233 if ( ! user ) return res . status ( 404 ) . send ( "User not found" ) ;
2334
24- const now = Date . now ( ) ;
25- const cache = icsCache . get ( user . username ) ;
26- if (
27- cache &&
28- now - cache . timestamp < config . cacheDuration * 60 * 60 * 24
29- ) {
30- return res
31- . setHeader ( "Content-Type" , "text/calendar" )
32- . setHeader (
33- "Content-Disposition" ,
34- `attachment; filename=${ user . friendlyName } .ics`
35- )
36- . send ( cache . ics ) ;
35+ const cacheEntry = icsCache . get ( user . username ) ;
36+ if ( cacheEntry ) {
37+ return sendIcs ( res , user . friendlyName , cacheEntry . ics ) ;
3738 }
3839
3940 const today = new Date ( ) ;
@@ -48,14 +49,7 @@ async function main() {
4849 config . timezone || "Europe/Berlin"
4950 ) ;
5051
51- icsCache . set ( user . username , { timestamp : now , ics } ) ;
52-
53- res . setHeader ( "Content-Type" , "text/calendar" ) ;
54- res . setHeader (
55- "Content-Disposition" ,
56- `attachment; filename=${ user . friendlyName } .ics`
57- ) ;
58- res . send ( ics ) ;
52+ return sendIcs ( res , user . friendlyName , ics ) ;
5953 } catch ( err ) {
6054 console . error ( err ) ;
6155 res . status ( 500 ) . send ( "Error fetching timetable" ) ;
0 commit comments