* init p2p polling app

This commit is contained in:
2026-03-31 19:09:46 +02:00
parent 4275cbd795
commit b5cb0e83e3
16 changed files with 638 additions and 1 deletions

33
server/api/polls/[id].ts Normal file
View File

@@ -0,0 +1,33 @@
// server/api/polls/[id].ts
export default defineEventHandler(async (event) => {
const method = event.node.req.method;
const pollId = getRouterParam(event, 'id');
// We use Nitro's built-in storage.
// 'polls' is the storage namespace.
const storage = useStorage('polls');
if (!pollId) {
throw createError({ statusCode: 400, statusMessage: 'Poll ID required' });
}
// GET: Fetch the saved Yjs document state
if (method === 'GET') {
const data = await storage.getItem(`poll:${pollId}`);
// Return the array of numbers (or null if it doesn't exist yet)
return { update: data || null };
}
// POST: Save a new Yjs document state
if (method === 'POST') {
const body = await readBody(event);
if (body.update && Array.isArray(body.update)) {
// Save the binary update (sent as an array of numbers) to storage
await storage.setItem(`poll:${pollId}`, body.update);
return { success: true };
}
throw createError({ statusCode: 400, statusMessage: 'Invalid update payload' });
}
});

View File

@@ -0,0 +1,15 @@
// server/api/polls/index.get.ts
export default defineEventHandler(async () => {
const storage = useStorage('polls');
// Get all keys in the 'polls' namespace
const allKeys = await storage.getKeys();
// Filter for our specific poll prefix and strip it for the UI
// poll:my-id -> my-id
const polls = allKeys
.filter(key => key.startsWith('poll:'))
.map(key => key.replace('poll:', ''));
return { polls };
});

24
server/middleware/uuid.ts Normal file
View File

@@ -0,0 +1,24 @@
import { v4 as uuidv4 } from 'uuid';
export default defineEventHandler((event) => {
// 1. Check if the cookie already exists
const cookie = getCookie(event, 'user_guid');
// 2. If it doesn't exist, generate and set it
if (!cookie) {
const newUuid = uuidv4();
setCookie(event, 'user_guid', newUuid, {
maxAge: 60 * 60 * 24 * 7, // 1 week
path: '/',
// httpOnly: true, // Set to true if you DON'T need to read it in Vue/JS
sameSite: 'lax',
});
// 3. Inject it into the context so it's available
// to other server routes/plugins during this same request
event.context.userGuid = newUuid;
} else {
event.context.userGuid = cookie;
}
});