diff --git a/Webserver/src/routes/api/reddit/+server.ts b/Webserver/src/routes/api/reddit/+server.ts new file mode 100644 index 0000000..6e58e77 --- /dev/null +++ b/Webserver/src/routes/api/reddit/+server.ts @@ -0,0 +1,22 @@ +import type { APIResponse } from "$lib/interfaces"; +import { json, type RequestHandler } from "@sveltejs/kit"; + +export const GET: RequestHandler = async ({ url }) => { + // Get endpoint on main API from searchparams + const params = url.searchParams; + const subreddit = params.get('subreddit'); + // Return if param not found + if (subreddit === null) return json({ success: false } as APIResponse); + + // Get subreddit page data + const response = await fetch(`https://www.reddit.com/r/${subreddit}/`, { + headers: new Headers({ + "User-Agent": "MatrixRedditMemes/0.0.1" + }) + }); + // Read data as string + const text = await response.text(); + + // Return source data + return json({ success: true, results: text.split('\n') } as APIResponse); +};