Skip to content

Commit 69d6aff

Browse files
committed
Consolidate different formats into single handler
1 parent 9c56dba commit 69d6aff

4 files changed

Lines changed: 74 additions & 162 deletions

File tree

src/routes/csv.ts

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import { Elysia } from "elysia";
22

33
import limiter from "../utils/ratelimiter";
44
import 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

97
const tleRoute = new Elysia({ prefix: "/csv" })
108
.use(limiter)
@@ -13,61 +11,12 @@ const tleRoute = new Elysia({ prefix: "/csv" })
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_csv`);
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 "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-
});
15+
return await groupHandler.handleGroupRequest(group, lastFetchedHeader, "tle");
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_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" } });
19+
return await groupHandler.handleGroupStatus(group, "tle");
7120
});
7221

7322
export default tleRoute;

src/routes/json.ts

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import { Elysia } from "elysia";
22

33
import limiter from "../utils/ratelimiter";
44
import 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

97
const 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

7322
export default tleRoute;

src/routes/tle.ts

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import { Elysia } from "elysia";
22

33
import limiter from "../utils/ratelimiter";
44
import 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

97
const tleRoute = new Elysia({ prefix: "/tle" })
108
.use(limiter)
@@ -13,61 +11,12 @@ const tleRoute = new Elysia({ prefix: "/tle" })
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_tle`);
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}" 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}_tle`);
32-
33-
if (!tle) {
34-
log.debug(`No cached TLEs for group "${group}". Fetching from Celestrak...`);
35-
tle = await tleFetcher(group);
36-
timestamp = now;
37-
kv.set(`${group}_tle`, tle);
38-
kv.set(`${group}_timestamp_tle`, timestamp);
39-
} else if (isStale) {
40-
log.debug(`TLEs for group "${group}" are stale. Fetching fresh TLEs...`);
41-
tle = await tleFetcher(group);
42-
timestamp = now;
43-
kv.set(`${group}_tle`, tle);
44-
kv.set(`${group}_timestamp_tle`, timestamp);
45-
} else {
46-
log.debug(`Serving cached TLEs for group "${group}".`);
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-
});
15+
return await groupHandler.handleGroupRequest(group, lastFetchedHeader, "tle");
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_tle`);
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, "tle");
7120
});
7221

7322
export default tleRoute;

src/utils/groupHandler.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import kv from "./kv";
2+
import config from "./config";
3+
import log from "./logger";
4+
import tleFetcher from "./tleFetcher";
5+
6+
async function handleGroupRequest(group: string, lastFetchedHeader: number, format: "tle" | "json" | "csv") {
7+
if (config.allowedGroups.includes(group) === false) {
8+
return new Response(`Group "${group}" is not allowed.`, { status: 403 });
9+
}
10+
11+
let timestamp = await kv.get(`${group}_timestamp_${format}`);
12+
const now = Date.now();
13+
const staleDuration = group === "active" ? config.cacheActiveDuration : config.cacheDuration;
14+
const isStale = timestamp ? now - timestamp > staleDuration : true;
15+
16+
if (lastFetchedHeader && timestamp && lastFetchedHeader <= timestamp && !isStale) {
17+
log.debug(`GP data for group "${group}", format "${format}" not modified since last fetch. Returning 304.`);
18+
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)}` } });
19+
}
20+
21+
let tle = await kv.get(`${group}_${format}`);
22+
23+
if (!tle) {
24+
log.debug(`No cached GP data for group "${group}", format "${format}". Fetching from Celestrak...`);
25+
tle = await tleFetcher(group);
26+
timestamp = now;
27+
kv.set(`${group}_${format}`, tle);
28+
kv.set(`${group}_timestamp_${format}`, timestamp);
29+
} else if (isStale) {
30+
log.debug(`GP data for group "${group}", format "${format}" are stale. Fetching fresh TLEs...`);
31+
tle = await tleFetcher(group, format);
32+
timestamp = now;
33+
kv.set(`${group}_${format}`, tle);
34+
kv.set(`${group}_timestamp_${format}`, timestamp);
35+
} else {
36+
log.debug(`Serving cached GP data for group "${group}", format "${format}".`);
37+
}
38+
39+
const contentType = format === "json" ? "application/json" : "text/plain";
40+
41+
return new Response(tle, {
42+
headers: { "Content-Type": contentType, "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)}` },
43+
});
44+
}
45+
46+
async function handleGroupStatus(group: string, format: "tle" | "json" | "csv") {
47+
if (config.allowedGroups.includes(group) === false) {
48+
return new Response(`Group "${group}" is not allowed.`, { status: 403 });
49+
}
50+
51+
const timestamp = await kv.get(`${group}_timestamp_${format}`);
52+
if (!timestamp) {
53+
return new Response(`No cached TLEs for group "${group}".`, { status: 404 });
54+
}
55+
56+
const now = Date.now();
57+
const age = Math.floor((now - timestamp) / 1000);
58+
const cacheDuration = group === "active" ? config.cacheActiveDuration : config.cacheDuration;
59+
const isStale = age > cacheDuration / 1000;
60+
const nextUpdate = new Date(timestamp + cacheDuration).toUTCString();
61+
62+
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" } });
63+
}
64+
65+
export default { handleGroupRequest, handleGroupStatus };

0 commit comments

Comments
 (0)