@@ -2,9 +2,7 @@ import { Elysia } from "elysia";
22
33import limiter from "../utils/ratelimiter" ;
44import config from "../utils/config" ;
5- import kv from "../utils/kv" ;
6- import log from "../utils/logger" ;
7- import tleFetcher from "../utils/tleFetcher" ;
5+ import groupHandler from "../utils/groupHandler" ;
86
97const tleRoute = new Elysia ( { prefix : "/json" } )
108 . use ( limiter )
@@ -13,61 +11,12 @@ const tleRoute = new Elysia({ prefix: "/json" })
1311 } )
1412 . get ( "/:group" , async ( ctx ) => {
1513 const group = ctx . params . group ;
16- if ( config . allowedGroups . includes ( group ) === false ) {
17- return new Response ( `Group "${ group } " is not allowed.` , { status : 403 } ) ;
18- }
19-
20- let timestamp = await kv . get ( `${ group } _timestamp_json` ) ;
21- const now = Date . now ( ) ;
22- const staleDuration = group === "active" ? config . cacheActiveDuration : config . cacheDuration ;
23- const isStale = timestamp ? now - timestamp > staleDuration : true ;
2414 const lastFetchedHeader = new Date ( ctx . request . headers . get ( "If-Modified-Since" ) || 0 ) . getTime ( ) ;
25-
26- if ( lastFetchedHeader && timestamp && lastFetchedHeader <= timestamp && ! isStale ) {
27- log . debug ( `TLEs for group "${ group } ", format "json" not modified since last fetch. Returning 304.` ) ;
28- return new Response ( null , { status : 304 , headers : { "Last-Modified" : new Date ( timestamp ) . toUTCString ( ) , "Cache-Control" : `max-age=${ group === "active" ? Math . ceil ( ( config . cacheActiveDuration - ( now - timestamp ) ) / 1000 ) : Math . ceil ( ( config . cacheDuration - ( now - timestamp ) ) / 1000 ) } ` } } ) ;
29- }
30-
31- let tle = await kv . get ( `${ group } _json` ) ;
32-
33- if ( ! tle ) {
34- log . debug ( `No cached TLEs for group "${ group } " in format "json". Fetching from Celestrak...` ) ;
35- tle = await tleFetcher ( group , "json" ) ;
36- timestamp = now ;
37- kv . set ( `${ group } _json` , tle ) ;
38- kv . set ( `${ group } _timestamp_json` , timestamp ) ;
39- } else if ( isStale ) {
40- log . debug ( `TLEs for group "${ group } ", format "json" are stale. Fetching fresh TLEs...` ) ;
41- tle = await tleFetcher ( group , "json" ) ;
42- timestamp = now ;
43- kv . set ( `${ group } _json` , tle ) ;
44- kv . set ( `${ group } _timestamp_json` , timestamp ) ;
45- } else {
46- log . debug ( `Serving cached TLEs for group "${ group } " in format "json".` ) ;
47- }
48-
49- return new Response ( tle , {
50- headers : { "Content-Type" : "application/json" , "Last-Modified" : new Date ( timestamp ) . toUTCString ( ) , "Cache-Control" : `max-age=${ group === "active" ? Math . ceil ( ( config . cacheActiveDuration - ( now - timestamp ) ) / 1000 ) : Math . ceil ( ( config . cacheDuration - ( now - timestamp ) ) / 1000 ) } ` } ,
51- } ) ;
15+ return await groupHandler . handleGroupRequest ( group , lastFetchedHeader , "json" ) ;
5216 } )
5317 . get ( "/:group/status" , async ( ctx ) => {
5418 const group = ctx . params . group ;
55- if ( config . allowedGroups . includes ( group ) === false ) {
56- return new Response ( `Group "${ group } " is not allowed.` , { status : 403 } ) ;
57- }
58-
59- const timestamp = await kv . get ( `${ group } _timestamp_json` ) ;
60- if ( ! timestamp ) {
61- return new Response ( `No cached TLEs for group "${ group } ".` , { status : 404 } ) ;
62- }
63-
64- const now = Date . now ( ) ;
65- const age = Math . floor ( ( now - timestamp ) / 1000 ) ;
66- const cacheDuration = group === "active" ? config . cacheActiveDuration : config . cacheDuration ;
67- const isStale = age > cacheDuration / 1000 ;
68- const nextUpdate = new Date ( timestamp + cacheDuration ) . toUTCString ( ) ;
69-
70- return new Response ( `Group: ${ group } \nLast Updated: ${ new Date ( timestamp ) . toUTCString ( ) } \nAge: ${ age } seconds\nStatus: ${ isStale ? "Stale" : "Fresh" } \nNext Update: ${ nextUpdate } ` , { status : 200 , headers : { "Content-Type" : "text/plain" } } ) ;
19+ return await groupHandler . handleGroupStatus ( group , "json" ) ;
7120 } ) ;
7221
7322export default tleRoute ;
0 commit comments