New routes for projects
This commit is contained in:
parent
5846e5241d
commit
0539bceeaa
29
pages/api/projects.ts
Normal file
29
pages/api/projects.ts
Normal file
@ -0,0 +1,29 @@
|
||||
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<string>) {
|
||||
if (req.method !== "GET") return res.status(405).end();
|
||||
|
||||
try {
|
||||
const path = resolve("./public", "projects", "list.json");
|
||||
const data = readFileSync(path).toString();
|
||||
console.debug("[API/get_project]\tRequest for project list");
|
||||
res.status(200).send(data);
|
||||
} catch (err) {
|
||||
console.error(`[API/get_project]\tError in request for project list! Code: ${(err as IFileError).code}`);
|
||||
res.status(500);
|
||||
} finally {
|
||||
res.end();
|
||||
}
|
||||
}
|
33
pages/api/projects/[name].ts
Normal file
33
pages/api/projects/[name].ts
Normal file
@ -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<string>) {
|
||||
if (req.method !== "GET") return res.status(405).end();
|
||||
if (!req.query.name) return res.status(400).end();
|
||||
|
||||
const project = req.query.name;
|
||||
|
||||
try {
|
||||
const path = resolve("./public", "projects", `${project}.adoc`);
|
||||
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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user