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
1
Layer 2
Template Layer
Code templates that generate your project structure
controllersservicesschemastests
2
Layer 3
Core Layer
Fastify server, middleware, and configuration
serverappmiddlewareplugins
3
Layer 4
Module Layer
Feature modules for specific functionality
authusersemailcachequeue
4
Layer 5
Service Layer
Reusable services used across modules
webhookwebsocketsearchanalytics
5
Design Patterns
Repository Pattern
Data access abstraction through Prisma client
src/modules/users/user.repository.tsService Pattern
Business logic encapsulation in services
src/services/user.service.tsController Pattern
Request handling and response formatting
src/controllers/user.controller.tsMiddleware Pattern
Request/response preprocessing
src/middleware/auth.tsModule 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.tsimport { 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 in2app.post('/api/users', userController.create);34// 2. Middleware chain executes (auth, validation, etc.)5// - Rate limiting6// - Authentication7// - Validation (Zod schema)89// 3. Controller receives validated request10class UserController {11async create(req, res) {12const data = req.body; // Already validated13const user = await userService.create(data);14res.json(user);15}16}1718// 4. Service handles business logic19class UserService {20async create(data) {21const hashedPassword = await hash(data.password);22const user = await prisma.user.create({23data: { ...data, password: hashedPassword }24});25return user;26}27}2829// 5. Response sent back30res.json({ user, message: 'Created successfully' });