# abhishek010-5/quiz-api-using-flask architecture

> Architecture diagram of the GitHub repository abhishek010-5/quiz-api-using-flask, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/abhishek010-5/quiz-api-using-flask
- Repository: https://github.com/abhishek010-5/quiz-api-using-flask

## Overview

##### Project Type and Purpose
- This is a RESTful API built using Flask that serves quiz questions across different collections and topics.
- It focuses on secure access (using API keys), rate limiting, and fetching quiz content from a MongoDB database.
- The primary goal is to provide endpoints that retrieve question data, with built-in error handling and security features.

##### File Structure
- The top-level "api" directory houses all backend logic:
- app.py – Likely the entry point that initializes the Flask application.
- routes.py – Contains the endpoint definitions (e.g., GET /, GET /get_questions/<collection_name>/<topic>).
- config.py – Manages configuration settings, potentially including API key validation, rate limits, and external resource configurations.
- database.py – Handles connections to MongoDB and database operations.
- Other files such as requirements.txt list dependencies, vercel.json configures deployment (hinting at Vercel deployment), and sample_key.txt provides a sample API key.
- The README outlines installation, usage, API endpoint details, rate limiting, and even provides a sample of the expected JSON schema from the MongoDB collections.

##### Architecture Components to Highlight
a. API Layer (Flask Application)
- app.py: Acts as the main entry.
- routes.py: Defines endpoints such as the home route and the questions retrieval route.
- API Key Authentication: Middleware or checks (in routes or before_request) ensuring that only valid API key holders can access the endpoints.
- Rate Limiting Module: Implements logic to restrict requests (50 per hour per IP) to protect the API.

b. Configuration Management
- config.py: Central location for configurable settings like rate limits, API key, and secret keys.
- .env File (mentioned in README): Environmental settings for sensitive information such as API_KEY, MongoDB connection URI, DB name, and Flask’s secret key.

c. Database Layer (MongoDB)
- database.py: Manages the connection to MongoDB, handling operations such as connecting to the DB and executing queries.
- Collection Structure: Different collections for topics like "current_affairs", "reasoning", and "aptitude" with a uniform document schema.

d. External Deployment / Configuration
- vercel.json: Configures the deployment environment on Vercel if applicable.
- Requirements and Environment Setup: requirements.txt ensures that the appropriate libraries are in place.

##### Relationships and Interactions
- The client sends HTTP requests to the Flask server.
- When a request is received:
- The API Endpoint (from routes.py) first validates the API key (using configuration from config.py).
- It then checks rate limit restrictions.
- Upon passing authentication and rate limit checks, the request is forwarded to interact with the database via database.py.
- MongoDB is queried for the required collection and topic. The response is then formatted as JSON and returned.
- Any failures (e.g., unauthorized access, missing data, or internal errors) generate appropriate error messages as depicted in the README.

##### Architectural Patterns and Principles
- Separation of Concerns: The logic is clearly divided among routing, configuration, and database access.
- Security: API key checks and proper error responses ensure that only valid, authorized requests are processed.
- Scalability Considerations: Although simple, rate limiting is employed to avoid abuse which is critical for public APIs.
- Externalization of Configuration: Using an .env file and config.py makes it easy to change settings without modifying source code.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client and External Deployment
    H["Client Request"]:::client
    G["Deployment Config (vercel.json)"]:::external

    %% Flask API Layer
    subgraph "Flask API Layer"
        A["Flask App (Entry: app.py)"]:::app
        B["API Endpoint Routes (routes.py)"]:::app
        C["Auth & Rate Limiting"]:::middleware
    end

    %% Configuration and Database Layers
    D["Configuration (config.py & .env)"]:::config
    E["Database Wrapper (database.py)"]:::app
    F(["MongoDB Database<br/>(current_affairs,reasoning,aptitude)"]):::db

    %% Data Flow
    H -->|"sends_request"| A
    A -->|"initializes"| B
    B -->|"triggers_auth"| C
    C -->|"queries_DB"| E
    E -->|"connects_to"| F
    F -->|"returns_data"| E
    E -->|"sends_response"| C
    C -->|"aggregates_data"| B
    B -->|"formats_response"| A
    A -->|"sends_response"| H

    %% Configuration Influence
    D -.->|"provides_settings"| A
    D -.->|"provides_settings"| B
    D -.->|"provides_settings"| C
    D -.->|"provides_settings"| E

    %% Deployment Influence
    G -.->|"deployment_settings"| A

    %% Click Events
    click A "https://github.com/abhishek010-5/quiz-api-using-flask/blob/main/api/app.py"
    click B "https://github.com/abhishek010-5/quiz-api-using-flask/blob/main/api/routes.py"
    click D "https://github.com/abhishek010-5/quiz-api-using-flask/blob/main/api/config.py"
    click E "https://github.com/abhishek010-5/quiz-api-using-flask/blob/main/api/database.py"
    click G "https://github.com/abhishek010-5/quiz-api-using-flask/blob/main/vercel.json"

    %% Styles
    classDef app fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef config fill:#F9E79F,stroke:#B9770E,stroke-width:2px;
    classDef db fill:#F5B7B1,stroke:#922B21,stroke-width:2px;
    classDef client fill:#D5F5E3,stroke:#196F3D,stroke-width:2px;
    classDef middleware fill:#FAD7A0,stroke:#7D6608,stroke-width:2px;
    classDef external fill:#E8DAEF,stroke:#6C3483,stroke-width:2px;
```
