Initial program flow page
This commit is contained in:
90
vite.config.js
Normal file
90
vite.config.js
Normal file
@@ -0,0 +1,90 @@
|
||||
import fs from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { defineConfig } from 'vite';
|
||||
import react from '@vitejs/plugin-react';
|
||||
|
||||
const databasePath = path.resolve(process.cwd(), 'data', 'flow-db.json');
|
||||
|
||||
async function readDatabase() {
|
||||
try {
|
||||
const raw = await fs.readFile(databasePath, 'utf8');
|
||||
return JSON.parse(raw);
|
||||
} catch (error) {
|
||||
if (error.code === 'ENOENT') return null;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function writeDatabase(data) {
|
||||
await fs.mkdir(path.dirname(databasePath), { recursive: true });
|
||||
await fs.writeFile(databasePath, JSON.stringify({
|
||||
...data,
|
||||
updatedAt: new Date().toISOString()
|
||||
}, null, 2));
|
||||
}
|
||||
|
||||
function readRequestBody(request) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let body = '';
|
||||
|
||||
request.on('data', (chunk) => {
|
||||
body += chunk;
|
||||
if (body.length > 5_000_000) {
|
||||
reject(new Error('Request body is too large'));
|
||||
request.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
request.on('end', () => resolve(body));
|
||||
request.on('error', reject);
|
||||
});
|
||||
}
|
||||
|
||||
function sendJson(response, statusCode, payload) {
|
||||
response.statusCode = statusCode;
|
||||
response.setHeader('Content-Type', 'application/json; charset=utf-8');
|
||||
response.end(JSON.stringify(payload));
|
||||
}
|
||||
|
||||
function serverDatabasePlugin() {
|
||||
return {
|
||||
name: 'flow-server-database',
|
||||
configureServer(server) {
|
||||
server.middlewares.use('/api/state', async (request, response) => {
|
||||
try {
|
||||
if (request.method === 'GET') {
|
||||
const data = await readDatabase();
|
||||
if (!data) {
|
||||
sendJson(response, 404, { error: 'Database is empty' });
|
||||
return;
|
||||
}
|
||||
sendJson(response, 200, data);
|
||||
return;
|
||||
}
|
||||
|
||||
if (request.method === 'PUT' || request.method === 'PATCH') {
|
||||
const current = request.method === 'PATCH' ? (await readDatabase()) ?? {} : {};
|
||||
const body = await readRequestBody(request);
|
||||
const payload = body ? JSON.parse(body) : {};
|
||||
const nextData = {
|
||||
...current,
|
||||
...payload
|
||||
};
|
||||
await writeDatabase(nextData);
|
||||
sendJson(response, 200, nextData);
|
||||
return;
|
||||
}
|
||||
|
||||
response.statusCode = 405;
|
||||
response.end();
|
||||
} catch (error) {
|
||||
sendJson(response, 500, { error: error.message });
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [react(), serverDatabasePlugin()]
|
||||
});
|
||||
Reference in New Issue
Block a user