From 89d65f4b96b7bd22614b2b8a9fbafb10dc2f1a8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gaute=20Fl=C3=A6gstad?= Date: Mon, 20 Oct 2025 10:49:23 +0200 Subject: [PATCH] feat(#4): :sparkles: Made a small express server for saving annotations --- server.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ vite.config.js | 14 ++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 server.js create mode 100644 vite.config.js diff --git a/server.js b/server.js new file mode 100644 index 0000000..d11befc --- /dev/null +++ b/server.js @@ -0,0 +1,48 @@ +import express from 'express' +import cors from 'cors' +import fs from 'fs' +import path from 'path' +import { fileURLToPath } from 'url' + +const filename = fileURLToPath(import.meta.url) +const dirname = path.dirname(filename) + +const app = express() +const PORT = process.env.PORT || 5174 + +app.use(cors()) +app.use(express.json({ limit: '2mb' })) + +// Directory where annotations are stored alongside Vite's public folder +const annotationsDir = path.join(dirname, 'public', 'annotations') +const annotationsFile = path.join(annotationsDir, 'annotations.json') + +// Ensure directory exists +function ensureDirSync(dir) { + if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true }) +} + +// Save annotations JSON +app.post('/api/annotations', (req, res) => { + try { + const body = req.body + if (!body || typeof body !== 'object') { + return res.status(400).json({ error: 'Invalid JSON body' }) + } + if (!body.folders || typeof body.folders !== 'object') { + return res.status(400).json({ error: 'Missing folders in payload' }) + } + + ensureDirSync(annotationsDir) + const data = JSON.stringify(body, null, 2) + fs.writeFileSync(annotationsFile, data, 'utf8') + return res.json({ saved: true, path: '/annotations/annotations.json' }) + } catch (err) { + console.error('Failed to save annotations:', err) + return res.status(500).json({ error: 'Failed to save annotations' }) + } +}) + +app.listen(PORT, () => { + console.log(`API server listening on http://localhost:${PORT}`) +}) diff --git a/vite.config.js b/vite.config.js new file mode 100644 index 0000000..afeed41 --- /dev/null +++ b/vite.config.js @@ -0,0 +1,14 @@ +import { defineConfig } from 'vite' + +export default defineConfig({ + server: { + port: 5173, + proxy: { + '/api': { + target: 'http://localhost:5174', + changeOrigin: true, + secure: false + } + } + } +})