A Spring Boot microservice designed to store and manage chat histories generated by RAG (Retrieval-Augmented Generation) based chatbot systems. This service provides RESTful APIs for managing chat sessions and messages with features like pagination, search, favorites, and comprehensive statistics.
- Chat Session Management: Create, read, update, and delete chat sessions
- Message Management: Add, retrieve, and delete messages within sessions
- User Isolation: All data is scoped to authenticated users
- Search & Filtering: Search sessions by name and filter messages by sender type
- Pagination: Efficient pagination for large datasets
- Favorites: Mark sessions as favorites for quick access
- Statistics: Get comprehensive statistics about user sessions
- Rate Limiting: Built-in rate limiting to prevent abuse
- Security: OAuth2 JWT-based authentication
- API Documentation: Interactive Swagger UI documentation
- Health Checks: Built-in health monitoring endpoints
- Docker Support: Fully containerized with Docker Compose
- Java 17
- Spring Boot 3.3.5
- Spring Data JPA
- PostgreSQL 15
- Spring Security with OAuth2
- OpenAPI 3 (Swagger)
- Maven
- Docker & Docker Compose
- Docker and Docker Compose installed on your system
- Git (to clone the repository)
git clone <repository-url>
cd rag-chat-storage# Build and start all services
docker-compose up --build
# Or run in detached mode
docker-compose up --build -dThis will start:
- Application: http://localhost:8080
- Database: PostgreSQL on port 5432
- PgAdmin: http://localhost:8081 (admin@example.com / admin123)
- API Base URL: http://localhost:8080/v3/api-docs
- Swagger UI: http://localhost:8080/swagger-ui/index.html
- Health Check: http://localhost:8080/actuator/health
- PgAdmin: http://localhost:8081
- name: postgres-docker
docker-compose down- Java 17 or higher
- Maven 3.6 or higher
- PostgreSQL 15 or higher
- Git
# Create PostgreSQL database
createdb ragdb
# Create user (optional, uses default credentials)
psql ragdb
CREATE USER raguser WITH PASSWORD 'ragpass';
GRANT ALL PRIVILEGES ON DATABASE ragdb TO raguser;The application uses the following default configuration (in application.yaml):
server:
port: 8080
spring:
datasource:
url: jdbc:postgresql://localhost:5432/ragdb
username: raguser
password: ragpass
jpa:
hibernate:
ddl-auto: update# Clone the repository
git clone <repository-url>
cd rag-chat-storage
# Build the application
mvn clean package
# Run the application
mvn spring-boot:run
# Or run the JAR directly
java -jar target/rag-chat-storage-0.0.1-SNAPSHOT.jar| Method | Endpoint | Description |
|---|---|---|
| POST | /api/v1/sessions |
Create a new chat session |
| GET | /api/v1/sessions |
Get all chat sessions |
| GET | /api/v1/sessions/paginated |
Get sessions with pagination |
| GET | /api/v1/sessions/{id} |
Get a specific session |
| PUT | /api/v1/sessions/{id} |
Update a session |
| DELETE | /api/v1/sessions/{id} |
Delete a session |
| PATCH | /api/v1/sessions/{id}/favorite |
Toggle favorite status |
| GET | /api/v1/sessions/favorites |
Get favorite sessions |
| GET | /api/v1/sessions/search?q={term} |
Search sessions |
| GET | /api/v1/sessions/stats |
Get session statistics |
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/v1/sessions/{id}/messages |
Add a message to session |
| GET | /api/v1/sessions/{id}/messages |
Get all messages in session |
| GET | /api/v1/sessions/{id}/messages/paginated |
Get messages with pagination |
| GET | /api/v1/sessions/{id}/messages/{msgId} |
Get a specific message |
| DELETE | /api/v1/sessions/{id}/messages/{msgId} |
Delete a message |
| GET | /api/v1/sessions/{id}/messages/latest?limit={n} |
Get latest N messages |
| GET | /api/v1/sessions/{id}/messages/by-sender/{type} |
Get messages by sender type |
| GET | /api/v1/sessions/{id}/messages/count |
Get message count |
The application uses OAuth2 JWT-based authentication. All API requests require:
- Authorization Header:
Bearer <jwt-token> - User ID Header:
X-User-ID: <user-id>
curl -X GET "http://localhost:8080/api/v1/sessions" \
-H "Authorization: Bearer your-jwt-token" \
-H "X-User-ID: user123"{
"id": 1,
"userId": "user123",
"name": "My Chat Session",
"isFavorite": false,
"createdAt": "2024-01-15T10:30:00Z",
"updatedAt": "2024-01-15T10:30:00Z"
}{
"id": 1,
"senderType": "USER",
"content": "Hello, how are you?",
"context": "Additional context information",
"createdAt": "2024-01-15T10:30:00Z"
}USER: Messages sent by the userASSISTANT: Messages sent by the AI assistant
You can customize the configuration using environment variables:
# Database Configuration
export POSTGRES_DB=ragdb
export POSTGRES_USER=raguser
export POSTGRES_PASSWORD=ragpass
# PgAdmin Configuration
export PGADMIN_EMAIL=admin@example.com
export PGADMIN_PASSWORD=admin123
# Application Configuration
export PORT=8080The application supports the following environment variables:
SPRING_DATASOURCE_URL: Database connection URLSPRING_DATASOURCE_USERNAME: Database usernameSPRING_DATASOURCE_PASSWORD: Database passwordPORT: Application port (default: 8080)SPRING_PROFILES_ACTIVE: Active Spring profile
src/
├── main/
│ ├── java/
│ │ └── bytecode/rag_chat_storage/
│ │ ├── config/ # Configuration classes
│ │ ├── controller/ # REST controllers
│ │ ├── dto/ # Data Transfer Objects
│ │ ├── entity/ # JPA entities
│ │ ├── exception/ # Exception handling
│ │ ├── interceptor/ # Request interceptors
│ │ ├── repository/ # Data repositories
│ │ └── service/ # Business logic
│ └── resources/
│ └── application.yaml # Application configuration
└── test/ # Test classes
# Run all tests
mvn test
# Run tests with coverage
mvn test jacoco:report# Build with production profile
mvn clean package -Pprod
# Or build with Docker
docker build -t rag-chat-storage .The application includes Spring Boot Actuator for monitoring:
- Health Check:
/actuator/health - Application Info:
/actuator/info - Metrics:
/actuator/metrics
Both the database and application containers include health checks:
- Database: Checks PostgreSQL readiness
- Application: Checks HTTP endpoint availability
-
Database Connection Failed
- Ensure PostgreSQL is running
- Check database credentials
- Verify network connectivity in Docker
-
Port Already in Use
- Change the port in
docker-compose.ymlorapplication.yaml - Stop conflicting services
- Change the port in
-
Authentication Issues
- Verify JWT token is valid
- Check OAuth2 configuration
- Ensure
X-User-IDheader is provided
# View application logs
docker-compose logs app
# View database logs
docker-compose logs db
# Follow logs in real-time
docker-compose logs -f appThis project is licensed under the MIT License - see the LICENSE file for details.