boredgame/src/lib/server/api/index.ts

45 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-07-21 19:05:48 +00:00
import { Hono } from 'hono';
import { hc } from 'hono/client';
import { cors } from 'hono/cors';
import { logger } from 'hono/logger';
2024-07-21 19:05:48 +00:00
import { validateAuthSession, verifyOrigin } from './middleware/auth.middleware';
import users from './controllers/user.controller';
2024-07-21 19:05:48 +00:00
import { config } from './common/config';
/* ----------------------------------- Api ---------------------------------- */
const app = new Hono().basePath('/api');
/* --------------------------- Global Middlewares --------------------------- */
app.use(verifyOrigin).use(validateAuthSession);
app.use(logger());
app.use(
'/*',
cors({
origin: [
'http://localhost:5173',
'http://localhost:80',
'http://host.docker.internal:80',
'http://host.docker.internal:5173'
], // Replace with your allowed domains
allowMethods: ['POST'],
allowHeaders: ['Content-Type']
// credentials: true, // If you need to send cookies or HTTP authentication
})
);
2024-07-21 19:05:48 +00:00
/* --------------------------------- Routes --------------------------------- */
const routes = app
.route('/user', users)
.get('/', (c) => c.json({ message: 'Server is healthy' }));
2024-07-21 19:05:48 +00:00
/* -------------------------------------------------------------------------- */
/* Exports */
/* -------------------------------------------------------------------------- */
export type AppType = typeof routes;
export const rpc = hc<AppType>(config.ORIGIN);
2024-07-21 19:05:48 +00:00
export type ApiClient = typeof rpc;
export type ApiRoutes = typeof routes;
export { app };