Skip to content

Commit 06ec2b5

Browse files
committed
Add initial backend services for AI OCR
Introduces a NestJS-based backend for document upload and OCR management, including modules for document handling, database stubs, queue stubs, and OCR stubs. Adds REST API endpoints, DTO validation, file storage logic, and test scripts for upload functionality. Includes configuration, documentation, and testing instructions.
1 parent 961c9f6 commit 06ec2b5

26 files changed

Lines changed: 20748 additions & 0 deletions

apps/README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# AI OCR Dashboard
2+
3+
Backend services for government document processing and OCR management.
4+
5+
## Project Structure
6+
7+
This is a monorepo containing:
8+
- **Backend Services** (`apps/backend-services`) - NestJS API services for document processing
9+
10+
## Features
11+
12+
- 📄 Document Upload & Processing
13+
- 🔍 **OCR (Optical Character Recognition)** - Real-time text extraction from images and PDFs
14+
- Image OCR processing with word-level bounding boxes
15+
- Multi-page PDF OCR with per-page analysis
16+
- Confidence scoring for extracted text
17+
18+
19+
## Tech Stack
20+
21+
### Backend Services
22+
- **NestJS** - Node.js framework
23+
- **Fastify** - HTTP server
24+
- **TypeScript** - Type Safety
25+
26+
## Getting Started
27+
28+
### Prerequisites
29+
30+
- Node.js 22+ (recommended)
31+
- npm 9+
32+
33+
### Installation
34+
35+
1. Install root dependencies:
36+
```bash
37+
npm install
38+
```
39+
40+
2. Install backend services dependencies:
41+
```bash
42+
cd apps/backend-services && npm install
43+
```
44+
45+
### Development
46+
47+
#### Start Backend Services
48+
```bash
49+
npm run dev
50+
# or
51+
cd apps/backend-services && npm run start:dev
52+
```
53+
54+
### Available Scripts
55+
56+
**Root Level:**
57+
- `npm run dev` - Start backend services
58+
- `npm run build` - Build backend services
59+
60+
**Backend Services:**
61+
- `cd apps/backend-services && npm run start:dev` - Start development server
62+
- `cd apps/backend-services && npm run build` - Build for production
63+
- `cd apps/backend-services && npm run start:prod` - Start production server
64+
65+
## Project Structure
66+
67+
```
68+
ai-ocr/
69+
├── apps/
70+
│ └── backend-services/ # NestJS backend services
71+
│ ├── src/
72+
│ │ ├── modules/ # Feature modules
73+
│ │ ├── common/ # Shared utilities
74+
│ │ └── main.ts # Application entry point
75+
│ └── package.json # Backend services dependencies
76+
└── package.json # Root workspace configuration
77+
```
78+
79+
## Development Notes
80+
81+
- Backend services handle document uploads via REST API
82+
- Files are stored to local filesystem with UUID-based naming
83+
- Database API and message queue integrations are stubbed and ready for implementation
84+
- See `apps/backend-services/README.md` for detailed documentation
85+
86+
## License
87+
88+
MIT
89+

apps/backend-services/.gitignore

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# compiled output
2+
/dist
3+
/node_modules
4+
5+
# Logs
6+
logs
7+
*.log
8+
npm-debug.log*
9+
pnpm-debug.log*
10+
yarn-debug.log*
11+
yarn-error.log*
12+
lerna-debug.log*
13+
14+
# OS
15+
.DS_Store
16+
17+
# Tests
18+
/coverage
19+
/.nyc_output
20+
21+
# IDEs and editors
22+
/.idea
23+
.project
24+
.classpath
25+
.c9/
26+
*.launch
27+
.settings/
28+
*.sublime-workspace
29+
30+
# IDE - VSCode
31+
.vscode/*
32+
!.vscode/settings.json
33+
!.vscode/tasks.json
34+
!.vscode/launch.json
35+
!.vscode/extensions.json
36+
37+
# Environment
38+
.env
39+
.env.local
40+
.env.*.local
41+
42+
# Storage
43+
/storage
44+
45+
# Build info
46+
*.tsbuildinfo
47+

apps/backend-services/README.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# Backend Services
2+
3+
NestJS backend services for the AI OCR pipeline. Handles document uploads via REST API, stores files to local filesystem, and integrates with database API and message queue (stubbed).
4+
5+
## Features
6+
7+
- REST API endpoint for document uploads (base64-encoded files)
8+
- Local filesystem storage with UUID-based naming
9+
- Stubbed database API integration
10+
- Stubbed RabbitMQ message queue integration
11+
- File type validation
12+
- Comprehensive error handling and logging
13+
14+
## Prerequisites
15+
16+
- Node.js 22+
17+
- npm 9+
18+
19+
## Setup
20+
21+
### 1. Install Dependencies
22+
23+
```bash
24+
npm install
25+
```
26+
27+
### 2. Environment Configuration
28+
29+
Create a `.env` file in the backend services directory:
30+
31+
```env
32+
PORT=3002
33+
NODE_ENV=development
34+
FRONTEND_URL=http://localhost:5173
35+
DATABASE_API_URL=http://localhost:3001/api/documents
36+
STORAGE_PATH=./storage/documents
37+
RABBITMQ_URL=amqp://localhost:5672
38+
RABBITMQ_EXCHANGE=document_upload
39+
RABBITMQ_ROUTING_KEY=document.uploaded
40+
```
41+
42+
### 3. Run the Service
43+
44+
```bash
45+
# Development mode
46+
npm run start:dev
47+
48+
# Production mode
49+
npm run build
50+
npm run start:prod
51+
```
52+
53+
## API Endpoints
54+
55+
### POST /api/upload
56+
57+
Upload a document with base64-encoded file data.
58+
59+
**Request Body:**
60+
```json
61+
{
62+
"title": "Document Title",
63+
"file": "base64-encoded-file-data",
64+
"file_type": "pdf|image|scan",
65+
"original_filename": "document.pdf",
66+
"metadata": {
67+
"key": "value"
68+
}
69+
}
70+
```
71+
72+
**Response:**
73+
```json
74+
{
75+
"success": true,
76+
"document": {
77+
"id": "doc_1234567890_abc123",
78+
"title": "Document Title",
79+
"original_filename": "document.pdf",
80+
"file_type": "pdf",
81+
"file_size": 1024,
82+
"status": "pending",
83+
"created_at": "2024-01-01T00:00:00.000Z"
84+
}
85+
}
86+
```
87+
88+
## Architecture
89+
90+
- **Framework**: NestJS with Fastify
91+
- **Database**: Stubbed API client (ready for HTTP client integration)
92+
- **Message Queue**: Stubbed RabbitMQ interface (ready for amqplib integration)
93+
- **File Storage**: Local filesystem (can be upgraded to S3/object storage)
94+
95+
## Testing
96+
97+
See [TESTING.md](./TESTING.md) for comprehensive testing instructions.
98+
99+
### Quick Test
100+
101+
1. **Start the service:**
102+
```bash
103+
npm run start:dev
104+
```
105+
106+
2. **Use the test script:**
107+
```bash
108+
# Using bash script
109+
./test-upload.sh path/to/your/file.pdf
110+
111+
# Using Node.js script
112+
node test-upload.js path/to/your/file.pdf
113+
```
114+
115+
3. **Or use cURL:**
116+
```bash
117+
FILE_BASE64=$(base64 -i yourfile.pdf)
118+
curl -X POST http://localhost:3002/api/upload \
119+
-H "Content-Type: application/json" \
120+
-d "{
121+
\"title\": \"Test Document\",
122+
\"file\": \"$FILE_BASE64\",
123+
\"file_type\": \"pdf\",
124+
\"original_filename\": \"yourfile.pdf\"
125+
}"
126+
```
127+
128+
## Development
129+
130+
The service uses stubbed implementations for:
131+
- Database operations (API calls logged, ready for HTTP client)
132+
- RabbitMQ message publishing (logged, ready for amqplib)
133+
134+
Replace the stubbed implementations when ready to integrate with actual services.
135+

0 commit comments

Comments
 (0)