README
ΒΆ
DM-Backend
A scalable Go backend service for AI-powered chat applications with support for medical/healthcare domain recommendations.
Table of Contents
- Overview
- Features
- Architecture
- Getting Started
- API Reference
- Project Structure
- Database
- Testing
- Development
- Datasets
- Models
- Contributing
- License
Overview
DM-Backend is a robust backend service designed to power AI chat applications with a focus on healthcare and medical recommendations. It provides a REST API for chat interactions, user management, and conversation history storage.
Features
- π High Performance: Built with Go and Gin framework for optimal performance
- πΎ Flexible Storage: Dual database support with LevelDB (NoSQL) and SQLite (SQL)
- π Security: Built-in middleware for authentication, rate limiting, and CORS
- π Scalable: Sharding support for horizontal scaling
- π§ͺ Well Tested: Comprehensive test coverage for all components
- π API Versioning: RESTful API with versioned endpoints
Architecture
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Client Applications β
ββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Gin HTTP Server β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β Middleware Stack ββ
β β βββββββββββ βββββββββ ββββββββββββ ββββββββββ ββββββββββββ ββ
β β β CORS β βLogger β βRateLimiterβ β Auth β βRequestID β ββ
β β βββββββββββ βββββββββ ββββββββββββ ββββββββββ ββββββββββββ ββ
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β API Routes β
β βββββββββββ βββββββββββββββ βββββββββββββββββ β
β β /health β β /api/v1/chatβ β /api/v1/users β β
β βββββββββββ βββββββββββββββ βββββββββββββββββ β
ββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Business Logic β
β βββββββββββββββ βββββββββββββββββ βββββββββββββββββββββββ β
β β User Model β β Chat Model β β Fragmentation Logic β β
β βββββββββββββββ βββββββββββββββββ βββββββββββββββββββββββ β
ββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Database Layer β
β ββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββββ β
β β LevelDB β β SQLite β β
β β (NoSQL Key-Value) β β (Relational Data) β β
β ββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Getting Started
Prerequisites
- Go 1.22.2 or higher
- Protocol Buffers compiler (protoc) - for generating model files
- Git
Installation
-
Clone the repository:
git clone https://github.com/TeamPentagon/DM-Backend.git cd DM-Backend -
Install dependencies:
go mod download -
Generate Protocol Buffer files:
# Install protoc-gen-go if not already installed go install google.golang.org/protobuf/cmd/protoc-gen-go@latest # Generate Go files from proto files cd internal/model protoc --go_out=. --go_opt=paths=source_relative user.proto protoc --go_out=. --go_opt=paths=source_relative chat.proto cd ../.. -
Build the application:
go build -o dm-backend . -
Run the application:
./dm-backend
Configuration
The application can be configured using environment variables:
| Variable | Description | Default |
|---|---|---|
PORT |
Server port | 8085 |
AI_ENDPOINT |
AI service endpoint URL | https://herbal-pmc-allowance-cognitive.trycloudflare.com/api/v1/generate |
Example:
export PORT=8080
export AI_ENDPOINT="https://your-ai-service.com/api/v1/generate"
./dm-backend
API Reference
Health Check
Endpoint: GET /health
Response:
{
"success": true,
"data": {
"status": "healthy"
}
}
Chat
Endpoint: POST /api/v1/chat
Query Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
response |
string | Yes | The user's message/prompt |
Response:
{
"success": true,
"data": {
"results": [
{
"text": "AI response here..."
}
]
}
}
Error Response:
{
"success": false,
"error": "Error description"
}
Legacy Endpoint
For backward compatibility, the chat endpoint is also available at:
Endpoint: POST /chat
Project Structure
DM-Backend/
βββ main.go # Application entry point
βββ go.mod # Go module definition
βββ go.sum # Dependency checksums
βββ README.md # This file
βββ LICENSE # MIT License
βββ internal/ # Internal packages
β βββ database/ # Database utilities
β β βββ database.go # DB connection management
β β βββ database_test.go # DB connection tests
β β βββ fragmentation.go # Shard management
β β βββ fragmentation_test.go
β βββ middleware/ # HTTP middleware
β β βββ middleware.go # CORS, Auth, RateLimit, etc.
β β βββ middleware_test.go
β βββ model/ # Data models
β βββ user.go # User CRUD operations
β βββ user.proto # User Protocol Buffer schema
β βββ user.pb.go # Generated User types
β βββ chat.go # Chat operations
β βββ chat.proto # Chat Protocol Buffer schema
β βββ chat.pb.go # Generated Chat types
βββ test/ # Integration tests
β βββ user_test.go # User integration tests
β βββ chat_test.go # Chat integration tests
βββ Database/ # Database files (created at runtime)
βββ Common/ # Common data shards
βββ GlobalSchema/ # Fragmentation schema
βββ NOSQL/ # NoSQL message storage
Database
LevelDB (Key-Value Store)
Used for:
- User data storage (serialized with Protocol Buffers)
- Chat messages and history
- Fragmentation schema (shard mapping)
SQLite (Relational)
Available for:
- Complex queries
- Relational data that requires joins
- Reporting and analytics
Sharding
The application supports horizontal sharding through the fragmentation module:
// Add a key to shard mapping
database.FragmentationAdd(shardNumber, "user_key_1", "user_key_2")
// Get shard for a key
shard, err := database.FragmentationGet("user_key_1")
// Update shard mapping
database.FragmentationUpdate("user_key_1", newShardNumber)
// Remove mapping
database.FragmentationRemove("user_key_1")
Testing
Run all tests:
go test ./... -v
Run tests with coverage:
go test ./... -cover -coverprofile=coverage.out
go tool cover -html=coverage.out -o coverage.html
Run specific package tests:
# Database tests
go test ./internal/database/... -v
# Middleware tests
go test ./internal/middleware/... -v
# Integration tests
go test ./test/... -v
Development
Adding a New Model
-
Create the Protocol Buffer schema in
internal/model/:syntax = "proto3"; package model; option go_package = ".;model"; message YourModel { string id = 1; string name = 2; } -
Generate Go code:
cd internal/model protoc --go_out=. --go_opt=paths=source_relative yourmodel.proto -
Implement CRUD operations in a new Go file
Adding Middleware
- Add your middleware function in
internal/middleware/middleware.go - Follow the Gin middleware pattern:
func YourMiddleware() gin.HandlerFunc { return func(c *gin.Context) { // Pre-processing c.Next() // Post-processing } } - Add tests in
middleware_test.go
Datasets
The project references the following datasets for training/fine-tuning:
- Doctor Review Dataset
- Healthcare Practitioner Dataset
- Patient Reviews (German)
- Healthcare NLP: LLMs, Transformers
- Medical Recommendation Dataset
- WebMD Drug Reviews Dataset
Models
Supported AI models for backend integration:
| Model | Provider | Link |
|---|---|---|
| Mixtral | Mistral AI | Kaggle |
| Flan-T5 | Kaggle | |
| Mistral | Mistral AI | Kaggle |
| Llama 2 | Meta | Kaggle |
| Llama 3 | Meta | Kaggle |
| Phi | Microsoft | Kaggle |
| Gemma | Kaggle |
Contributing
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature - Commit your changes:
git commit -am 'Add your feature' - Push to the branch:
git push origin feature/your-feature - Submit a pull request
Code Style
- Follow the official Go Code Review Comments
- Run
go fmtbefore committing - Run
go vetto check for common issues - Add tests for new functionality
License
This project is licensed under the MIT License - see the LICENSE file for details.
Team Pentagon - Building intelligent healthcare solutions
Documentation
ΒΆ
Overview ΒΆ
Package main is the entry point for the DM-Backend service. It sets up the HTTP server with Gin framework and configures API routes.
Directories
ΒΆ
| Path | Synopsis |
|---|---|
|
internal
|
|
|
database
Package database provides database connection utilities for SQLite and LevelDB.
|
Package database provides database connection utilities for SQLite and LevelDB. |
|
middleware
Package middleware provides HTTP middleware components for the DM-Backend service.
|
Package middleware provides HTTP middleware components for the DM-Backend service. |
|
model
Package model provides chat-related data models and database operations.
|
Package model provides chat-related data models and database operations. |