Files
jam-session-web/src/app/api/config/route.ts
T
2025-01-13 12:37:08 +01:00

44 lines
1.1 KiB
TypeScript

import {NextResponse} from "next/server";
import fs from "fs";
import path from "path";
let writeLock: boolean = false;
export async function GET() {
const configPath = process.env.CONFIG_PATH || "./config.json";
const absolutePath = path.resolve(configPath);
if (!fs.existsSync(absolutePath)) {
return NextResponse.json({error: "Config file not found"}, {status: 404});
}
const fileContent = fs.readFileSync(absolutePath, "utf-8");
const config = JSON.parse(fileContent);
return NextResponse.json(config);
}
// post function for updating the config
export async function POST({body}) {
if (writeLock) {
return NextResponse.json({error: "Write lock is active"}, {status: 400});
}
writeLock = true;
try {
const configPath = process.env.CONFIG_PATH || "./config.json";
const absolutePath = path.resolve(configPath);
fs.writeFileSync(absolutePath, JSON.stringify(body, null, 2));
return NextResponse.json({success: true});
}
catch (e) {
return NextResponse.json({error: e.message}, {status: 500});
}
finally {
writeLock = false;
}
}