How joaquinn6/habits-api works
Main Components
• API Server (FastAPI): This is the entry point (main.py) that initializes the server and connects all parts.
• Controllers: (Files in the “controllers” folder like habit_controller, mark_controller, stats_controller, and users_controller) manage the routing and incoming HTTP API requests.
• Services: (Inside the “services” folder) house the business logic for handling habits, marks, stats, and user-related operations.
• Repositories: (From the “repositories” folder) abstract the interactions with the database (MongoDB) and provide methods to perform CRUD operations.
• Models and Schemas: (Found in “models” and “schemas” folders) define the data structures for internal logic and input/output validations (e.g., using Pydantic).
• Core Module: (Contained in the “core” folder) manages essential functions such as authentication (JWT), database connection, environment variables, and helper utilities.
• External Systems:
• Database: MongoDB is used for data persistence.
• Frontend Client: A React application makes HTTP requests to the API endpoints.
• Infrastructure Scripts/Configs: Though not numerous here, files like Pipfile, requirements.txt, and start.sh indicate dependency management and startup configurations.
And Illustrate the Relationships
• The React Frontend sends API calls to the FastAPI server.
• FastAPI (in main.py) routes incoming requests to the appropriate controller based on URL endpoints.
• Controllers:
• Receive and validate requests.
• Invoke the corresponding Service functions (e.g., habit_service, mark_service, etc.).
• Services:
• Contain the business logic.
• Call repository functions to interact with the MongoDB database.
• Repositories:
• Encapsulate database queries and persistence logic.
• Use connection helpers and environment variables from the Core module.
• Core Utilities:
• Handle configurations, authentication (JWT handling with libraries like python-jose and password management via passlib), and maintenance of environment-related variables.
• Data Models/Schemas:
Important Architectural Patterns
• Layered Architecture: Separation between controllers (API layer), services (business logic layer), and repositories (data access layer) ensures modularity.
• Security by Design: Authentication and authorization are implemented via JWT and password security libraries.
• Integration & Extensibility: The REST API is designed to be consumed by external clients (React App) and has clean separation of concerns that make extensions easier.