From ee8e4b9fb9e4f1633756ca7ce4fdfc4581ba1d60 Mon Sep 17 00:00:00 2001 From: Daniel Kluge Date: Thu, 13 Jan 2022 18:15:32 +0100 Subject: [PATCH] Add project api route --- pages/api/get_project.ts | 33 +++++++++++++++++++++++++++++++++ public/projects/.gitkeep | 0 2 files changed, 33 insertions(+) create mode 100644 pages/api/get_project.ts create mode 100644 public/projects/.gitkeep diff --git a/pages/api/get_project.ts b/pages/api/get_project.ts new file mode 100644 index 0000000..0e33ad7 --- /dev/null +++ b/pages/api/get_project.ts @@ -0,0 +1,33 @@ +import { readFileSync } from "fs"; +import { resolve } from "path"; +import type { NextApiRequest, NextApiResponse } from "next"; + +interface IFileError { + message: string; + name: string; + stack?: string; + code: string; + errno: number; + syscall: string; + path: string; +} + +export default function handler(req: NextApiRequest, res: NextApiResponse) { + if (req.method !== "GET") return res.status(405).end(); + if (!req.query.projectName) return res.status(400).end(); + + const project = req.query.projectName; + + try { + const path = resolve("./public", "projects", `${project}.md`) + const data = readFileSync(path).toString(); + console.debug(`[API/get_project]\tRequest for ${project}`); + res.status(200).send(data); + } catch (err) { + console.error(`[API/get_project]\tError in request for ${project}! Code: ${(err as IFileError).code}`); + if ((err as IFileError).code === "ENOENT") res.status(404); + else res.status(500); + } finally { + res.end(); + } +} diff --git a/public/projects/.gitkeep b/public/projects/.gitkeep new file mode 100644 index 0000000..e69de29