INIT
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import { Hono } from 'hono';
|
||||
import { serveStatic } from 'hono/bun';
|
||||
import { cors } from 'hono/cors';
|
||||
import PocketBase from 'pocketbase';
|
||||
import { config } from 'dotenv';
|
||||
|
||||
config();
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
// Enable CORS
|
||||
app.use('/*', cors());
|
||||
|
||||
// Serve static files from frontend directory
|
||||
app.use('/*', serveStatic({ root: './frontend' }));
|
||||
|
||||
// PocketBase client
|
||||
const pb = new PocketBase(process.env.PB_DB || 'https://pocketbase.ccllc.pro');
|
||||
|
||||
// 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(process.env.PB_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,
|
||||
};
|
||||
Reference in New Issue
Block a user