Node ID #4
This commit is contained in:
@@ -39,3 +39,5 @@ yarn-error.log*
|
|||||||
# typescript
|
# typescript
|
||||||
*.tsbuildinfo
|
*.tsbuildinfo
|
||||||
next-env.d.ts
|
next-env.d.ts
|
||||||
|
|
||||||
|
data
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import { promises as fs } from "fs";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
|
export async function read<T extends object>(
|
||||||
|
template: T,
|
||||||
|
fp: string,
|
||||||
|
...filePaths: string[]
|
||||||
|
): Promise<T> {
|
||||||
|
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);
|
||||||
|
}
|
||||||
@@ -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<NextResponse> {
|
||||||
|
const { node_id } = await read(TEMPLATE, "data", "node.json");
|
||||||
|
return NextResponse.json({ app: "dockship", version, node_id });
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user