48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import { Hono } from 'hono';
|
|
import { cors } from 'hono/cors';
|
|
import { AppContext } from './utils';
|
|
import publicRoutes from './routes/public';
|
|
import adminRoutes from './routes/admin';
|
|
|
|
const app = new Hono<AppContext>();
|
|
|
|
// CORS configuration
|
|
app.use(
|
|
'*',
|
|
cors({
|
|
origin: '*', // In a real app, restrict this to your frontend's origin
|
|
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
|
allowHeaders: ['Content-Type', 'Authorization', 'X-Admin-API-Key', 'x-mworld-origin'],
|
|
exposeHeaders: ['Content-Length'],
|
|
maxAge: 600,
|
|
})
|
|
);
|
|
|
|
// Optional: Handle pre-flight requests
|
|
app.options('*', (c) => {
|
|
return c.text('', 204);
|
|
});
|
|
|
|
// Mount the routes
|
|
app.route('/api', publicRoutes);
|
|
app.route('/api/admin', adminRoutes);
|
|
|
|
app.get('/', (c) => {
|
|
return c.json({
|
|
message: 'Welcome to the Cloudflare Social API!',
|
|
siteId: 'd9wl4z'
|
|
});
|
|
});
|
|
|
|
app.notFound((c) => {
|
|
return c.json({ success: false, msg: 'Not Found' }, 404);
|
|
});
|
|
|
|
app.onError((err, c) => {
|
|
console.error(`Unhandled error: ${err}`, err);
|
|
// In production, you might not want to expose the error message
|
|
const message = err instanceof Error ? err.message : 'Internal Server Error';
|
|
return c.json({ success: false, msg: message }, 500);
|
|
});
|
|
|
|
export default app; |