78 lines
2.1 KiB
TypeScript
78 lines
2.1 KiB
TypeScript
import { Hono } from 'hono';
|
|
import { serveStatic } from 'hono/bun';
|
|
import { cors } from 'hono/cors';
|
|
import PocketBase from 'pocketbase';
|
|
|
|
const app = new Hono();
|
|
|
|
// Enable CORS
|
|
app.use('/*', cors());
|
|
|
|
// Serve static files from frontend directory
|
|
app.use('/*', serveStatic({ root: './frontend' }));
|
|
|
|
// PocketBase client - PB_DB from service environment
|
|
const pb = new PocketBase(process.env.PB_DB || 'https://pocketbase.ccllc.pro');
|
|
|
|
// Collection name from service env (fallback to Ideas_Feedback)
|
|
const PB_IDEA_COLLECTION = process.env.PBIdea_Collection || 'Ideas_Feedback';
|
|
|
|
// Use user's PocketBase token (passed from authenticated frontend)
|
|
function setUserPocketBaseAuth(token: string) {
|
|
pb.authStore.save(token);
|
|
}
|
|
// API endpoint to submit feedback
|
|
app.post('/api/submit', async (c) => {
|
|
try {
|
|
const data = await c.req.json();
|
|
const userToken = data.pbToken;
|
|
|
|
if (!userToken) {
|
|
return c.json({
|
|
success: false,
|
|
message: 'User authentication required. Please log in first.'
|
|
}, 401);
|
|
}
|
|
|
|
// Use user's PocketBase token
|
|
setUserPocketBaseAuth(userToken);
|
|
|
|
// Remove pbToken from data before creating record
|
|
delete data.pbToken;
|
|
|
|
// Create the record in PocketBase as the authenticated user
|
|
const record = await pb.collection(PB_IDEA_COLLECTION).create(data);
|
|
console.log(`✓ Created PocketBase record with ID: ${record.id}`);
|
|
|
|
return c.json({
|
|
success: true,
|
|
message: 'Thank you! Your feedback has been submitted successfully.',
|
|
recordId: record.id
|
|
});
|
|
} catch (error: any) {
|
|
console.error('✗ Error submitting feedback:', error);
|
|
return c.json({
|
|
success: false,
|
|
message: error.message || 'Failed to submit feedback'
|
|
}, 400);
|
|
}
|
|
});
|
|
|
|
// Health check endpoint
|
|
app.get('/api/health', (c) => {
|
|
return c.json({
|
|
status: 'ok',
|
|
service: 'idea-feedback-form',
|
|
timestamp: new Date().toISOString()
|
|
});
|
|
});
|
|
|
|
const PORT = Number(process.env.PORT || 7000);
|
|
|
|
console.log(`Idea & Feedback Form server running at http://localhost:${PORT}`);
|
|
|
|
export default {
|
|
port: PORT,
|
|
fetch: app.fetch,
|
|
};
|