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

41 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-05-25 06:02:26 +00:00
import 'reflect-metadata';
import { Hono } from 'hono';
import { hc } from 'hono/client';
import { container } from 'tsyringe';
import { IamController } from './controllers/iam.controller';
2024-08-07 15:13:36 +00:00
import { env } from './configs/envs.config';
import { validateAuthSession, verifyOrigin } from './middlewares/auth.middleware';
// import { TestJob } from './jobs/test.job';
import { glob, globSync } from 'glob';
import path from 'path';
2024-08-07 15:13:36 +00:00
console.log('API SERVER STARTED');
/* ----------------------------------- Api ---------------------------------- */
2024-05-25 06:02:26 +00:00
const app = new Hono().basePath('/api');
/* --------------------------- Global Middlewares --------------------------- */
2024-06-25 02:45:00 +00:00
app.use(verifyOrigin).use(validateAuthSession);
/* --------------------------------- Routes --------------------------------- */
2024-05-25 06:02:26 +00:00
const routes = app
.route('/iam', container.resolve(IamController).routes())
2024-06-25 02:45:00 +00:00
2024-05-25 06:02:26 +00:00
/* -------------------------------------------------------------------------- */
/* Exports */
/* -------------------------------------------------------------------------- */
2024-08-07 15:13:36 +00:00
export const rpc = hc<typeof routes>(env.ORIGIN);
2024-05-25 06:02:26 +00:00
export type ApiClient = typeof rpc;
export type ApiRoutes = typeof routes;
export { app };
2024-08-07 15:13:36 +00:00
async function resolveJobs() {
const jobFiles = globSync('**/*.job.*');
for (const file of jobFiles) {
const module = await import(path.resolve(file));
container.resolve(module.default)
}
}
resolveJobs();