Files
personal-finance/backend-api/src/server.ts
Alexander Zinn 40210c454e Add lock files for package management and update architecture documentation
- Introduced bun.lock and package-lock.json to manage dependencies for the project.
- Enhanced backend API architecture documentation with additional security and documentation guidelines.
- Made minor formatting adjustments across various files for consistency and clarity.
2025-12-11 02:11:43 -05:00

103 lines
3.0 KiB
TypeScript

import Fastify from 'fastify';
import cors from '@fastify/cors';
import jwt from '@fastify/jwt';
import swagger from '@fastify/swagger';
import swaggerUi from '@fastify/swagger-ui';
import {env} from './config/env';
import {errorHandler} from './middleware/errorHandler';
import {authRoutes} from './routes/auth';
import {assetRoutes} from './routes/assets';
import {liabilityRoutes} from './routes/liability.routes';
import {clientRoutes} from './routes/client.routes';
import {invoiceRoutes} from './routes/invoice.routes';
import {netWorthRoutes} from './routes/networth.routes';
import {debtRoutes} from './routes/debt.routes';
import {cashflowRoutes} from './routes/cashflow.routes';
import {dashboardRoutes} from './routes/dashboard.routes';
/**
* Create and configure Fastify server
* Implements Single Responsibility: Server configuration
*/
export async function buildServer() {
if (env.NODE_ENV !== 'production') {
console.log('Development mode enabled. Environment variables [%o]', env);
}
const fastify = Fastify({
logger: {
level: env.NODE_ENV === 'development' ? 'info' : 'error',
transport: env.NODE_ENV === 'development' ? {target: 'pino-pretty'} : undefined
}
});
// Register plugins
await fastify.register(cors, {
origin: env.CORS_ORIGIN,
credentials: true
});
await fastify.register(jwt, {
secret: env.JWT_SECRET,
sign: {
expiresIn: env.JWT_EXPIRES_IN
}
});
// Register Swagger for API documentation
await fastify.register(swagger, {
openapi: {
info: {
title: 'Personal Finances API',
description: 'API for managing personal finances including assets, liabilities, invoices, and more',
version: '1.0.0'
},
servers: [
{
url: `http://localhost:${env.PORT}`,
description: 'Development server'
}
],
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT'
}
}
}
}
});
await fastify.register(swaggerUi, {
routePrefix: '/docs',
uiConfig: {
docExpansion: 'list',
deepLinking: false
}
});
// Register error handler
fastify.setErrorHandler(errorHandler);
// Health check
fastify.get('/health', async () => ({
status: 'ok',
timestamp: new Date().toISOString()
}));
// Register routes
await fastify.register(authRoutes, {prefix: '/api/auth'});
await fastify.register(assetRoutes, {prefix: '/api/assets'});
await fastify.register(liabilityRoutes, {prefix: '/api/liabilities'});
await fastify.register(clientRoutes, {prefix: '/api/clients'});
await fastify.register(invoiceRoutes, {prefix: '/api/invoices'});
await fastify.register(netWorthRoutes, {prefix: '/api/net-worth'});
await fastify.register(debtRoutes, {prefix: '/api/debts'});
await fastify.register(cashflowRoutes, {prefix: '/api/cashflow'});
await fastify.register(dashboardRoutes, {prefix: '/api/dashboard'});
return fastify;
}