K12 Backend Core (Microservices)
This project is part of the K12 Backend Ecosystem, built with TypeScript, Express.js, and Node.js 22.
It follows a microservices architecture, where each domain is isolated into an independent service.
Currently, the system includes:
- Core Service – user management, reports, investigations
- Auth Service – standalone authentication & authorization microservice (JWT, refresh tokens, sessions)
- Investigations Infrastructure – investigation workflows
Each service communicates via gRPC (synchronous) and Redis Pub/Sub (asynchronous).
Shared contracts (.proto files, DTOs, enums, interfaces) are maintained in a common package.
Requirements
- Node.js v22 or higher
- pnpm for dependency management
- MongoDB for persistence
- Redis for cache and inter-service messaging
- protoc +
ts-protofor gRPC code generation
Core Service Structure
src/
├─ index.ts # entry: starts Express server
├─ app.ts # express app, middlewares, routes
├─ config/
│ ├─ env.ts # environment configuration
│ └─ logger.ts # centralized logger
├─ controllers/ # controllers (request handling)
│ ├─ user.controller.ts
│ ├─ report.controller.ts
│ └─ investigation.controller.ts
├─ routes/ # express routes
│ ├─ user.routes.ts
│ ├─ report.routes.ts
│ └─ investigation.routes.ts
├─ models/ # mongoose models
│ ├─ User.model.ts
│ ├─ Report.model.ts
│ └─ Investigation.model.ts
├─ services/ # business logic
│ ├─ user.service.ts
│ ├─ report.service.ts
│ └─ investigation.service.ts
├─ interfaces/ # service/domain contracts
│ ├─ IUserService.ts
│ ├─ IReportService.ts
│ ├─ IInvestigationService.ts
│ └─ IRepository.ts
├─ middlewares/
│ ├─ errorHandler.ts
│ └─ validateRequest.ts
├─ database/
│ ├─ mongo.connection.ts
│ └─ redis.client.ts
├─ grpc/ # generated gRPC clients
│ ├─ auth.proto
│ ├─ auth_grpc_pb.ts
│ └─ auth_pb.ts
├─ utils/
│ ├─ schema.ts
│ ├─ time.ts
│ └─ apiResponse.ts
├─ types/ # DTOs, enums, global contracts
│ ├─ user.ts # IUser, IUserDTO, UserRole
│ ├─ report.ts # IReport, ReportStatus
│ ├─ investigation.ts # IInvestigation, InvestigationState
│ ├─ enums.ts # ErrorCode, LogLevel
│ └─ index.d.ts # Express global type extensions
├─ tests/
│ ├─ unit/
│ └─ e2e/
Auth Service (separate microservice)
The Auth Service is deployed independently.
It exposes gRPC endpoints for authentication and authorization logic.
gRPC Endpoints
| RPC Method | Maps To Endpoint | Description |
|---|---|---|
GenerateToken(LoginRequest) → TokenResponse |
/auth/login |
Authenticates user and returns JWT token. |
ChangePassword(ChangePasswordRequest) → StatusResponse |
/auth/change-password |
Allows logged-in users to change their password. |
ChangeAvatar(ChangeAvatarRequest) → StatusResponse |
/auth/change-avatar |
Updates user avatar and stores it in GCS. |
CreateUser(CreateUserRequest) → UserResponse |
/admin/create-user |
Admin-only. Creates new user with role and credentials. |
ResetPassword(ResetPasswordRequest) → StatusResponse |
/admin/reset-password |
Admin-only. Resets another user's password. |
DeleteUser(DeleteUserRequest) → StatusResponse |
/admin/delete-user |
Admin-only. Deletes a user (not self). |
GetUserById(UserRequest) → UserResponse |
/me |
Returns info about current authenticated user. |
Inter-Service Communication
- gRPC → synchronous calls (Core ↔ Auth).
- Example: Core calls
ValidateTokenin Auth for protected routes.
- Example: Core calls
- Redis Pub/Sub → asynchronous events (e.g.,
user.created,session.expired).
Development Standards
- TypeScript strict mode
- ESLint + Prettier enforced
- JSDoc required for public methods
- Husky pre-commit hooks: lint, type-check, tests
Commit examples:
feat: add sceneLoop placeholderfix: correct ESM import in index.tsdocs: add PROJECT_STRUCTURE.mdrefactor(core): simplify World.tick ordertest(e2e): add ws happy pathbuild: update tsconfig for declarationsci: run lint and test on PRrevert: revert "feat: add ws"
Installation (Core Service)
- Install Node.js v22
- Install pnpm globally
npm install -g pnpm - Clone repository and install deps
git clone <repository-url> cd k12_BackEnd_Core pnpm install - Setup Husky
pnpm dlx husky install - Install
protoc- macOS (Homebrew):
brew install protobuf
- macOS (Homebrew):
- Generate gRPC code from proto files
pnpm proto:gen - Configure
.envPORT=3000 MONGO_URI=mongodb://localhost:27017/k12 REDIS_URI=localhost REDIS_PORT=6379 AUTH_GRPC_HOST=localhost AUTH_GRPC_PORT=50051
Scripts
pnpm dev– run dev serverpnpm build– compile TypeScriptpnpm start– run compiled buildpnpm lint– lint codepnpm proto:gen– generate gRPC stubs from proto filespnpm test– run unit + e2e testspnpm run docs– generate disposable HTML docs intodocs/(ignored by git)
Documentation
Run pnpm run docs to build the temporary .doc-tmp/ bundle and output HTML documentation into docs/. The docs/ directory is ignored by git—regenerate it locally or in CI whenever you need fresh API docs.
Testing
- Unit tests – services, utils
- E2E tests – gRPC interactions + API routes