agrinix-server

NestJS Logo

๐ŸŒฑ Agrinix Server

A robust, scalable API for crop disease detection, powered by AI and built with NestJS.

Quick Start API Docs Features



NestJS Node.js TypeScript PostgreSQL Redis MIT License


๐Ÿš€ Get Started

๐ŸŒฑ Agrinix Server - Crop Disease Detection API

NestJS Node.js TypeScript PostgreSQL Redis License

A robust and scalable NestJS server for the Agrinix crop disease detection application. This server processes crop leaf images through advanced AI models to detect diseases and provide comprehensive agricultural insights to help farmers protect their crops.


๐Ÿš€ Features

Core Functionality

Advanced Features


๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Mobile App    โ”‚    โ”‚   Agrinix       โ”‚    โ”‚   External      โ”‚
โ”‚   (Client)      โ”‚โ—„โ”€โ”€โ–บโ”‚   Server        โ”‚โ—„โ”€โ”€โ–บโ”‚   Services      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚                        โ”‚
                              โ–ผ                        โ–ผ
                       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                       โ”‚   PostgreSQL    โ”‚    โ”‚   Roboflow      โ”‚
                       โ”‚   Database      โ”‚    โ”‚   AI Models     โ”‚
                       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚                        โ”‚
                              โ–ผ                        โ–ผ
                       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                       โ”‚   Redis Cache   โ”‚    โ”‚   DeepSeek AI   โ”‚
                       โ”‚   & Sessions    โ”‚    โ”‚   Analysis      โ”‚
                       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ› ๏ธ Technology Stack

Backend Framework

Database & Storage

AI & Machine Learning

Authentication & Security

Development Tools


๐Ÿ“‹ Prerequisites

Before you begin, ensure you have the following installed:


๐Ÿš€ Quick Start

1. Clone the Repository

git clone https://github.com/techpepson/agrinix-server.git
cd agrinix-server

2. Install Dependencies

npm install

3. Environment Configuration

Create a .env file in the root directory:

cp .env.example .env

Fill in the required environment variables:

# Database Configuration
DATABASE_URL="postgresql://username:password@localhost:5432/agrinix_db"

# Redis Configuration
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=your_redis_password
REDIS_USER=default

# JWT Configuration
JWT_SECRET=your_jwt_secret_key
JWT_SECRET_OR_KEY=your_jwt_secondary_key

# Email Configuration
EMAIL_USER=your_email@gmail.com
EMAIL_PASS=your_app_password

# Cloudinary Configuration
CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secret

# Roboflow Configuration
ROBOFLOW_PRIVATE_API_KEY=your_roboflow_private_key
ROBOFLOW_PUBLISHABLE_API_KEY=your_roboflow_publishable_key
ROBOFLOW_ENDPOINT=https://serverless.roboflow.com/infer/workflows/agrinix/agrinix-workflow-3

# OpenRouter Configuration
OPENROUTER_API_KEY=your_openrouter_api_key

# Application Configuration
PORT=3000
SERVER_BASE_URL=http://localhost:3000
AGRINIX_LOGO_URL=https://your-domain.com/logo.jpg

4. Database Setup

PostgreSQL Setup

  1. Create a PostgreSQL database:
CREATE DATABASE agrinix_db;
  1. Run Prisma migrations:
npx prisma migrate dev
  1. Generate Prisma client:
npx prisma generate

Redis Setup

  1. Install Redis on your system
  2. Start Redis server:
redis-server

5. Start the Development Server

# Development mode with hot reload
npm run start:dev

# Production mode
npm run start:prod

# Debug mode
npm run start:debug

The server will be available at http://localhost:8000


๐Ÿ“š API Documentation

Authentication Endpoints

Register User

POST /api/auth/register
Content-Type: application/json

{
  "name": "John Doe",
  "email": "john@example.com",
  "password": "securepassword123"
}

Login User

POST /auth/login
Content-Type: application/json

{
  "email": "john@example.com",
  "password": "securepassword123"
}

Crop Disease Detection

Upload and Detect Disease

POST /api/crops/roboflow-detect-disease
Authorization: Bearer <jwt_token>
Content-Type: multipart/form-data

{
  "image": <file>
}

Response Format

{
  "success": true,
  "data": {
    "diseaseClass": "healthy",
    "confidence": 0.95,
    "description": "Detailed disease description",
    "causes": ["Environmental factors", "Pathogen presence"],
    "symptoms": ["Leaf spots", "Wilting"],
    "prevention": ["Proper spacing", "Fungicide application"],
    "treatment": ["Remove infected plants", "Apply treatment"]
  }
}

Community Features

Create Message

POST /api/community/messages
Authorization: Bearer <jwt_token>
Content-Type: application/json

{
  "messageTitle": "Help with tomato disease",
  "messageBody": "I found spots on my tomato leaves...",
  "messageImage": "optional_image_url"
}

Get Messages

GET /api/community/messages
Authorization: Bearer <jwt_token>

๐Ÿ”ง Development

Project Structure

src/
โ”œโ”€โ”€ auth/                 # Authentication module
โ”œโ”€โ”€ crops/               # Crop disease detection
โ”œโ”€โ”€ community/           # Community features
โ”œโ”€โ”€ notifications/       # Notification system
โ”œโ”€โ”€ processors/          # Background job processors
โ”œโ”€โ”€ helpers/            # Utility services
โ”œโ”€โ”€ config/             # Configuration management
โ”œโ”€โ”€ guards/             # Authentication guards
โ”œโ”€โ”€ dto/                # Data transfer objects
โ””โ”€โ”€ prisma/             # Database service

Available Scripts

# Development
npm run start:dev          # Start development server
npm run start:debug        # Start with debug mode

# Production
npm run build             # Build the application
npm run start:prod        # Start production server

# Testing
npm run test              # Run unit tests
npm run test:watch        # Run tests in watch mode
npm run test:e2e          # Run end-to-end tests
npm run test:cov          # Run tests with coverage

# Code Quality
npm run lint              # Run ESLint
npm run format            # Format code with Prettier

# Database
npx prisma migrate dev    # Run database migrations
npx prisma generate       # Generate Prisma client
npx prisma studio         # Open Prisma Studio

Environment Variables

Variable Description Required
DATABASE_URL PostgreSQL connection string โœ…
REDIS_HOST Redis server host โœ…
REDIS_PORT Redis server port โœ…
JWT_SECRET JWT signing secret โœ…
EMAIL_USER SMTP email username โœ…
EMAIL_PASS SMTP email password โœ…
CLOUDINARY_CLOUD_NAME Cloudinary cloud name โœ…
CLOUDINARY_API_KEY Cloudinary API key โœ…
CLOUDINARY_API_SECRET Cloudinary API secret โœ…
ROBOFLOW_PRIVATE_API_KEY Roboflow private API key โœ…
OPENROUTER_API_KEY OpenRouter API key โœ…

๐Ÿงช Testing

Running Tests

# Unit tests
npm run test

# E2E tests
npm run test:e2e

# Test coverage
npm run test:cov

Test Structure

test/
โ”œโ”€โ”€ app.e2e-spec.ts      # End-to-end tests
โ””โ”€โ”€ jest-e2e.json        # Jest E2E configuration

๐Ÿš€ Deployment

Production Build

# Build the application
npm run build

# Start production server
npm run start:prod

Docker Deployment

Create a Dockerfile:

FROM node:18-alpine

WORKDIR /app

COPY package*.json ./
RUN npm ci --only=production

COPY . .
RUN npm run build

EXPOSE 3000

CMD ["npm", "run", "start:prod"]

Environment Setup for Production

  1. Set up PostgreSQL database
  2. Configure Redis instance
  3. Set up Cloudinary account
  4. Configure Roboflow API keys
  5. Set up OpenRouter API access
  6. Configure email service (Gmail, SendGrid, etc.)

๐Ÿ“Š Monitoring & Logging

Logging

The application uses NestJS built-in logging with different levels:

Performance Monitoring


๐Ÿ”’ Security Features


๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Code Style


๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.


๐Ÿ†˜ Support


๐Ÿ™ Acknowledgments


Made with โค๏ธ for farmers and agricultural communities