69 lines
2.3 KiB
TypeScript
69 lines
2.3 KiB
TypeScript
import fastify from "fastify";
|
|
import { log, logger } from "@/utils/log";
|
|
import { serializerCompiler, validatorCompiler, ZodTypeProvider } from "fastify-type-provider-zod";
|
|
import { onShutdown } from "@/utils/shutdown";
|
|
import { EventRouter } from "@/modules/eventRouter";
|
|
import { Fastify } from "./types";
|
|
import { authRoutes } from "./routes/authRoutes";
|
|
import { pushRoutes } from "./routes/pushRoutes";
|
|
import { sessionRoutes } from "./routes/sessionRoutes";
|
|
import { connectRoutes } from "./routes/connectRoutes";
|
|
import { accountRoutes } from "./routes/accountRoutes";
|
|
import { startSocket } from "./socket";
|
|
import { machinesRoutes } from "./routes/machinesRoutes";
|
|
import { devRoutes } from "./routes/devRoutes";
|
|
import { enableMonitoring } from "./utils/enableMonitoring";
|
|
import { enableErrorHandlers } from "./utils/enableErrorHandlers";
|
|
import { enableAuthentication } from "./utils/enableAuthentication";
|
|
|
|
export async function startApi(eventRouter: EventRouter) {
|
|
|
|
// Configure
|
|
log('Starting API...');
|
|
|
|
// Start API
|
|
const app = fastify({
|
|
loggerInstance: logger,
|
|
bodyLimit: 1024 * 1024 * 100, // 100MB
|
|
});
|
|
app.register(import('@fastify/cors'), {
|
|
origin: '*',
|
|
allowedHeaders: '*',
|
|
methods: ['GET', 'POST', 'DELETE']
|
|
});
|
|
app.get('/', function (request, reply) {
|
|
reply.send('Welcome to Happy Server!');
|
|
});
|
|
|
|
// Create typed provider
|
|
app.setValidatorCompiler(validatorCompiler);
|
|
app.setSerializerCompiler(serializerCompiler);
|
|
const typed = app.withTypeProvider<ZodTypeProvider>() as unknown as Fastify;
|
|
|
|
// Enable features
|
|
enableMonitoring(typed);
|
|
enableErrorHandlers(typed);
|
|
enableAuthentication(typed);
|
|
|
|
// Routes
|
|
authRoutes(typed);
|
|
pushRoutes(typed);
|
|
sessionRoutes(typed, eventRouter);
|
|
accountRoutes(typed, eventRouter);
|
|
connectRoutes(typed, eventRouter);
|
|
machinesRoutes(typed, eventRouter);
|
|
devRoutes(typed);
|
|
|
|
// Start HTTP
|
|
const port = process.env.PORT ? parseInt(process.env.PORT, 10) : 3005;
|
|
await app.listen({ port, host: '0.0.0.0' });
|
|
onShutdown('api', async () => {
|
|
await app.close();
|
|
});
|
|
|
|
// Start Socket
|
|
startSocket(typed, eventRouter);
|
|
|
|
// End
|
|
log('API ready on port http://localhost:' + port);
|
|
} |