Skip to content

juliofortunato/foodiary-api

Repository files navigation

Foodiary API

A serverless API for a food diary application that helps users track their nutrition by analyzing meal photos and audio recordings using AI. The application automatically calculates nutritional information and helps users achieve their health goals.

🚀 Features

  • User Authentication: Secure signup and signin with JWT tokens
  • Meal Tracking: Upload photos or audio recordings of meals
  • AI-Powered Analysis: Uses OpenAI's GPT-4 Vision and Whisper models to analyze meals
  • Nutritional Calculation: Automatic calculation of calories, proteins, carbohydrates, and fats
  • Goal Setting: Personalized nutrition goals based on user profile (height, weight, activity level, goals)
  • Serverless Architecture: Built on AWS Lambda with S3 for file storage and SQS for async processing

🛠️ Technologies Used

Backend

  • Node.js 22.x - Runtime environment
  • TypeScript - Type-safe JavaScript
  • Serverless Framework - AWS Lambda deployment
  • Drizzle ORM - Database ORM with PostgreSQL
  • Neon Database - Serverless PostgreSQL

AWS Services

  • AWS Lambda - Serverless compute
  • Amazon S3 - File storage for meal images and audio
  • Amazon SQS - Message queuing for async meal processing
  • API Gateway - HTTP API endpoints

AI & Processing

  • OpenAI GPT-4 Vision - Image analysis for meal identification
  • OpenAI Whisper - Audio transcription
  • bcryptjs - Password hashing
  • jsonwebtoken - JWT authentication

Development

  • Serverless Offline - Local development
  • ESBuild - TypeScript compilation and bundling

📋 Prerequisites

  • Node.js 22.x or higher
  • AWS CLI configured with appropriate permissions
  • A Neon PostgreSQL database
  • OpenAI API key

🚀 Getting Started

1. Clone the repository

git clone <repository-url>
cd foodiary-api

2. Install dependencies

npm install

3. Environment Setup

Create a .env file in the root directory with the following variables:

DATABASE_URL=your_neon_database_url
JWT_SECRET=your_jwt_secret_key
OPENAI_API_KEY=your_openai_api_key

4. Database Setup

Run the database migrations:

npx drizzle-kit push

5. Local Development

Start the local development server:

npm run dev

The API will be available at http://localhost:3000

6. Deployment

Deploy to AWS:

serverless deploy

📚 API Endpoints

Authentication

POST /signup

Create a new user account.

Request Body:

{
  "name": "John Doe",
  "email": "john@example.com",
  "password": "password123",
  "goal": "lose",
  "gender": "male",
  "birthDate": "1990-01-01",
  "height": 175,
  "weight": 70,
  "activityLevel": 3
}

POST /signin

Authenticate user and get JWT token.

Request Body:

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

GET /me

Get current user profile (requires authentication).

Meals

POST /meals

Create a new meal entry with file upload.

Request Body:

{
  "inputType": "picture",
  "inputFileKey": "user-uploads/meal-123.jpg"
}

GET /meals

List all meals for the authenticated user.

GET /meals/{mealId}

Get specific meal details by ID.

🏗️ Architecture

The application follows a serverless architecture with the following components:

  1. API Gateway - Handles HTTP requests
  2. Lambda Functions - Process requests and business logic
  3. S3 Bucket - Stores uploaded meal images and audio files
  4. SQS Queue - Handles asynchronous meal processing
  5. PostgreSQL Database - Stores user data and meal information

Data Flow

  1. User uploads meal photo/audio → S3
  2. S3 triggers Lambda function → SQS
  3. SQS processes meal → AI analysis
  4. Results stored in database

🔧 Development

Project Structure

src/
├── clients/          # AWS service clients
├── controllers/      # Request/response handling
├── db/              # Database schema and connection
├── functions/       # Lambda function handlers
├── lib/             # Utility libraries
├── queues/          # SQS queue processing
├── services/        # Business logic services
├── types/           # TypeScript type definitions
└── utils/           # Helper utilities

Adding New Features

  1. Create Lambda function in src/functions/
  2. Add controller in src/controllers/
  3. Update serverless.yml with new endpoint
  4. Add database schema if needed
  5. Test locally with npm run dev

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors