|
| 1 | +import { Elysia } from "elysia"; |
| 2 | + |
| 3 | +import limiter from "../utils/ratelimiter"; |
| 4 | +import config from "../utils/config"; |
| 5 | +import kv from "../utils/kv"; |
| 6 | +import log from "../utils/logger"; |
| 7 | +import tleFetcher from "../utils/tleFetcher"; |
| 8 | + |
| 9 | +const tleRoute = new Elysia({ prefix: "/csv" }) |
| 10 | + .use(limiter) |
| 11 | + .get("/", () => { |
| 12 | + return new Response(`Allowed groups: ${config.allowedGroups.join(", ")}`, { status: 200, headers: { "Content-Type": "text/plain" } }); |
| 13 | + }) |
| 14 | + .get("/:group", async (ctx) => { |
| 15 | + 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_csv`); |
| 21 | + const now = Date.now(); |
| 22 | + const staleDuration = group === "active" ? config.cacheActiveDuration : config.cacheDuration; |
| 23 | + const isStale = timestamp ? now - timestamp > staleDuration : true; |
| 24 | + 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 "csv" 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}_csv`); |
| 32 | + |
| 33 | + if (!tle) { |
| 34 | + log.debug(`No cached TLEs for group "${group}" in format "csv". Fetching from Celestrak...`); |
| 35 | + tle = await tleFetcher(group, "csv"); |
| 36 | + timestamp = now; |
| 37 | + kv.set(`${group}_csv`, tle); |
| 38 | + kv.set(`${group}_timestamp_csv`, timestamp); |
| 39 | + } else if (isStale) { |
| 40 | + log.debug(`TLEs for group "${group}", format "csv" are stale. Fetching fresh TLEs...`); |
| 41 | + tle = await tleFetcher(group, "csv"); |
| 42 | + timestamp = now; |
| 43 | + kv.set(`${group}_csv`, tle); |
| 44 | + kv.set(`${group}_timestamp_csv`, timestamp); |
| 45 | + } else { |
| 46 | + log.debug(`Serving cached TLEs for group "${group}" in format "csv".`); |
| 47 | + } |
| 48 | + |
| 49 | + return new Response(tle, { |
| 50 | + headers: { "Content-Type": "text/plain", "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 | + }); |
| 52 | + }) |
| 53 | + .get("/:group/status", async (ctx) => { |
| 54 | + 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_csv`); |
| 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" } }); |
| 71 | + }); |
| 72 | + |
| 73 | +export default tleRoute; |
0 commit comments