diff --git a/.gitignore b/.gitignore index 5ef6a52..97bc0bc 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,5 @@ yarn-error.log* # typescript *.tsbuildinfo next-env.d.ts + +data \ No newline at end of file diff --git a/src/app/api/file.ts b/src/app/api/file.ts new file mode 100644 index 0000000..a80251b --- /dev/null +++ b/src/app/api/file.ts @@ -0,0 +1,21 @@ +import { promises as fs } from "fs"; +import path from "path"; + +export async function read( + template: T, + fp: string, + ...filePaths: string[] +): Promise { + const filePath = path.join(process.cwd(), fp, ...filePaths); + const dirPath = path.dirname(filePath); + + try { + await fs.access(filePath); + } catch { + await fs.mkdir(dirPath, { recursive: true }); + await fs.writeFile(filePath, JSON.stringify(template, null, 2), "utf-8"); + } + + const content = await fs.readFile(filePath, "utf-8"); + return JSON.parse(content); +} diff --git a/src/app/api/route.ts b/src/app/api/route.ts new file mode 100644 index 0000000..983c27e --- /dev/null +++ b/src/app/api/route.ts @@ -0,0 +1,11 @@ +import { NextRequest, NextResponse } from "next/server"; +import { version } from "../../../package.json"; +import { read } from "./file"; +import { randomUUID } from "crypto"; + +const TEMPLATE = { node_id: randomUUID() }; + +export async function GET(req: NextRequest): Promise { + const { node_id } = await read(TEMPLATE, "data", "node.json"); + return NextResponse.json({ app: "dockship", version, node_id }); +}