Add backend API for personal finance management application
- Introduced a comprehensive backend API using TypeScript, Fastify, and PostgreSQL. - Added essential files including architecture documentation, environment configuration, and Docker setup. - Implemented RESTful routes for managing assets, liabilities, clients, invoices, and cashflow. - Established a robust database schema with Prisma for data management. - Integrated middleware for authentication and error handling. - Created service and repository layers to adhere to SOLID principles and clean architecture. - Included example environment variables for development, staging, and production setups.
This commit is contained in:
98
backend-api/src/server.ts
Normal file
98
backend-api/src/server.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
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() {
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user