diff options
| author | Alex <zuedev@gmail.com> | 2025-03-23 11:16:14 +0000 |
|---|---|---|
| committer | Alex <zuedev@gmail.com> | 2025-03-23 11:16:14 +0000 |
| commit | 6601749134b506a3e826b335a67d7d18d2b276d5 (patch) | |
| tree | b1f8940982bd36d2ee82f51f9d911af4d735bbdc | |
| parent | 459fabf119117c2d204ea5e8f17b05a713f4514e (diff) | |
| download | zue.dev-6601749134b506a3e826b335a67d7d18d2b276d5.tar zue.dev-6601749134b506a3e826b335a67d7d18d2b276d5.tar.gz zue.dev-6601749134b506a3e826b335a67d7d18d2b276d5.tar.bz2 zue.dev-6601749134b506a3e826b335a67d7d18d2b276d5.tar.xz zue.dev-6601749134b506a3e826b335a67d7d18d2b276d5.zip | |
refactor: implement Router class for improved request handling
| -rw-r--r-- | source/index.js | 141 | ||||
| -rw-r--r-- | source/library/router.js | 67 |
2 files changed, 129 insertions, 79 deletions
diff --git a/source/index.js b/source/index.js index e11aeac..bed631f 100644 --- a/source/index.js +++ b/source/index.js @@ -1,3 +1,5 @@ +import Router from "./library/router.js"; + import talent96 from "./data/talent96.js"; export default { @@ -12,99 +14,80 @@ export default { @returns {Response} a new Response object */ async fetch(request, environment, context) { - const url = new URL(request.url); - - switch (url.pathname) { - // return a simple hello world message - case "/": - return Respond({ - message: "Hello, World! :3", - }); - - // return status of a given service - case "/status": - const service = url.searchParams.get("service"); - - const acceptedServices = [ - "dns", - "load-balancer", - "cdn", - "functions", - "mysql-cluster", - "mongodb-cluster", - "redis-cluster", - "elasticsearch-cluster", - "git-connector", - "job-runners", - "container-registry", - "kubernetes-cluster", - "bare-metal-servers", - "game-server-api", - "anti-ddos-protection", - "anti-cheat-api", - ]; - - if (acceptedServices.includes(service)) - return Respond({ - status: "ok", - }); - - return Respond({ - error: `service not found`, + const router = new Router(request, environment, context); + + router.add("/", () => { + return router.respond({ + message: "Hello, World! :3", + }); + }); + + router.add("/status", (request) => { + const url = new URL(request.url); + const service = url.searchParams.get("service"); + + const acceptedServices = [ + "dns", + "load-balancer", + "cdn", + "functions", + "mysql-cluster", + "mongodb-cluster", + "redis-cluster", + "elasticsearch-cluster", + "git-connector", + "job-runners", + "container-registry", + "kubernetes-cluster", + "bare-metal-servers", + "game-server-api", + "anti-ddos-protection", + "anti-cheat-api", + ]; + + if (acceptedServices.includes(service)) + return router.respond({ + status: "ok", }); - // check if twitch user is streaming from a whitelist - case "/96/twitch/streaming": - const channel = url.searchParams.get("channel"); + return router.respond({ + error: `service not found`, + }); + }); - if (!channel) - return Respond({ - error: `channel not provided`, - }); + router.add("/96/twitch/streaming", async (request) => { + const url = new URL(request.url); + const channel = url.searchParams.get("channel"); - const whitelist = ["zuedev", ...talent96]; + if (!channel) + return router.respond({ + error: `channel not provided`, + }); - if (!whitelist.includes(channel)) - return Respond({ - error: `channel not whitelisted`, - }); + const whitelist = ["zuedev", ...talent96]; - const channelLive = await isTwitchChannelLive(channel); + if (!whitelist.includes(channel)) + return router.respond({ + error: `channel not whitelisted`, + }); - if (channelLive) - return Respond({ - status: "live", - }); + const channelLive = await isTwitchChannelLive(channel); - return Respond({ - status: "offline", + if (channelLive) + return router.respond({ + status: "live", }); - // default case - default: - return Respond({ - error: "not found", - }); - } + return router.respond({ + status: "offline", + }); + }); + + return router.route(); }, }; /* - Helper function to respond with a JSON object - - @param {object} body - the JSON object to respond with - @returns {Response} a new Response object -*/ -function Respond(body) { - return new Response(JSON.stringify(body), { - headers: { - "Access-Control-Allow-Origin": "*", - "Content-Type": "application/json", - }, - }); -} - -/* Checks if a twitch channel is live by fetching the "live" preview image of the channel, if the image is fetched successfully, then the channel is live, otherwise it's offline. diff --git a/source/library/router.js b/source/library/router.js new file mode 100644 index 0000000..7545476 --- /dev/null +++ b/source/library/router.js @@ -0,0 +1,67 @@ +/* + Router class to handle routing of requests based on the request path. + + @param {Request} request - the incoming request object + @param {Environment} environment - the environment object + @param {Context} context - the context object + + @returns {Response} a new Response object +*/ +export default class Router { + constructor(request, environment, context) { + this.request = request; + this.environment = environment; + this.context = context; + this.routes = []; + } + + /* + Add a new route to the router. + + @param {string} path - the path to match + @param {function} handler - the handler function to call if the path matches + + @returns {Router} the router object + */ + add(path, handler) { + return this.routes.push({ + path, + handler, + }); + } + + /* + Helper function to respond with a JSON object + + @param {object} body - the JSON object to respond with + + @returns {Response} a new Response object + */ + respond(body) { + return new Response(JSON.stringify(body), { + headers: { + "Access-Control-Allow-Origin": "*", + "Content-Type": "application/json", + }, + }); + } + + /* + Route the request to the appropriate handler based on the request path. + + @returns {Response} a new Response object + */ + route() { + for (const route of this.routes) { + const url = new URL(this.request.url); + + if (url.pathname === route.path) { + return route.handler(this.request, this.environment, this.context); + } + } + + return this.respond({ + error: `route not found`, + }); + } +} |
