Back to Documentation

Architecture

Learn about the modular architecture and design patterns

Architecture Layers

ServCraft follows a layered architecture for separation of concerns

Layer 1

CLI Layer

ServCraft CLI for project generation and management

initaddgeneratescaffold
Layer 2

Template Layer

Code templates that generate your project structure

controllersservicesschemastests
Layer 3

Core Layer

Fastify server, middleware, and configuration

serverappmiddlewareplugins
Layer 4

Module Layer

Feature modules for specific functionality

authusersemailcachequeue
Layer 5

Service Layer

Reusable services used across modules

webhookwebsocketsearchanalytics

Design Patterns

Repository Pattern

Data access abstraction through Prisma client

src/modules/users/user.repository.ts

Service Pattern

Business logic encapsulation in services

src/services/user.service.ts

Controller Pattern

Request handling and response formatting

src/controllers/user.controller.ts

Middleware Pattern

Request/response preprocessing

src/middleware/auth.ts

Module Structure

Standard structure for all feature modules

module.ts- Module configuration and registration
controller.ts- HTTP request handlers
service.ts- Business logic
schema.ts- Validation schemas (Zod/Joi/Yup)
routes.ts- Route definitions
index.ts- Public exports
Module Index Export
// Example: src/modules/auth/index.ts
import { AuthController } from './controller';
import { AuthService } from './service';
import { authSchema } from './schema';
export const authModule = {
controller: AuthController,
service: AuthService,
schema: authSchema,
};
export { AuthController } from './controller';
export { AuthService } from './service';
export { default as authMiddleware } from './middleware';

Request Flow

Request Processing Flow
1// 1. Request comes in
2app.post('/api/users', userController.create);
3
4// 2. Middleware chain executes (auth, validation, etc.)
5// - Rate limiting
6// - Authentication
7// - Validation (Zod schema)
8
9// 3. Controller receives validated request
10class UserController {
11 async create(req, res) {
12 const data = req.body; // Already validated
13 const user = await userService.create(data);
14 res.json(user);
15 }
16}
17
18// 4. Service handles business logic
19class UserService {
20 async create(data) {
21 const hashedPassword = await hash(data.password);
22 const user = await prisma.user.create({
23 data: { ...data, password: hashedPassword }
24 });
25 return user;
26 }
27}
28
29// 5. Response sent back
30res.json({ user, message: 'Created successfully' });