Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor main.py #1126

Open
eniiku opened this issue Feb 28, 2025 · 5 comments
Open

chore: refactor main.py #1126

eniiku opened this issue Feb 28, 2025 · 5 comments

Comments

@eniiku
Copy link
Contributor

eniiku commented Feb 28, 2025

Description

The main.py file currently contains a mix of configuration, exception handling, routing, and initialization logic, making it difficult to maintain, extend, and test.

Introducing this refactoring will make the codebase more maintainable.


Proposed Changes

1. Move Uvicorn Port to Environment File

  • Update .env.example with PORT variable. Setting a value of 7001 as it's default.
  • Update main.py to read the port from the Settings class in api/utils/settings.py.

2. Extract Configuration to a config file (api/utils/config.py):

  • Create functions like setup_cors and setup_middleware.
  • Update main.py to use these functions.

3. Move Exception Handlers to a Dedicated File

  • Create api/utils/exceptions.py with handlers for HTTPException, RequestValidationError, IntegrityError, and generic exceptions.
  • Update main.py to use these handlers.

4. Use a Router for Root Endpoints

  • Create api/core/routers/root.py with an APIRouter for the root endpoints (/ and /probe).
  • Update main.py to include the new router.

Acceptance Criteria

  • Environment Configuration:

    • The PORT variable is defined in .env.example.
    • The application reads the port from the Settings class in api/utils/settings.py.
  • Configuration Modularity:

    • Configuration-related code is moved to api/utils/config.py.
    • The application runs with the new configuration setup.
  • Exception Handling:

    • Exception handlers are moved to api/utils/exceptions.py.
    • The application handles exceptions correctly.
  • Root Endpoints:

    • Root endpoints are moved to api/core/routers/root.py.
    • The application serves the root endpoints correctly.

Testing

  • Verify that the application starts using the port specified in .env.
  • Test all endpoints to ensure they work as expected.
  • Verify that exceptions are handled correctly.
  • Check that directories are created correctly.
@eniiku
Copy link
Contributor Author

eniiku commented Feb 28, 2025

@Takinnuoye5

@Takinnuoye5
Copy link
Contributor

@joboy-dev come see this

@joboy-dev
Copy link
Contributor

Wow
Only that port thing is valid
Every other one will cause problems so mo

@eniiku
Copy link
Contributor Author

eniiku commented Feb 28, 2025

can i create a separate issue to work on the port thing then

@eniiku
Copy link
Contributor Author

eniiku commented Feb 28, 2025

#1135

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants