Skip to content

refactor: refactor monolithic api.py and services.py into modular structure#22

Merged
allisson merged 4 commits intomainfrom
copilot/create-api-and-services-modules
Dec 31, 2025
Merged

refactor: refactor monolithic api.py and services.py into modular structure#22
allisson merged 4 commits intomainfrom
copilot/create-api-and-services-modules

Conversation

Copy link
Contributor

Copilot AI commented Dec 31, 2025

Split monolithic api.py (349 lines) and services.py (248 lines) into focused modules organized by domain responsibility.

Structure

API modules (fastpubsub/api/):

  • app.py - create_app() factory function
  • server.py - Gunicorn server configuration
  • middlewares.py - HTTP middleware
  • helpers.py - Error response utilities
  • routers/ - Route handlers by domain (topics, subscriptions, monitoring)

Service modules (fastpubsub/services/):

  • topics.py - Topic CRUD operations
  • subscriptions.py - Subscription CRUD operations
  • messages.py - Message publish/consume, DLQ, metrics
  • helpers.py - Shared database helpers

Test modules mirror production structure:

  • tests/api/routers/ - API endpoint tests by router
  • tests/services/ - Service layer tests by domain
  • tests/helpers.py - Shared test utilities

Usage

# Backward compatible - existing imports work unchanged
from fastpubsub.api import app, run_server
from fastpubsub import services

# New modular imports available
from fastpubsub.api.app import create_app
from fastpubsub.services.topics import create_topic

All 83 tests pass with maintained 91% coverage.

Original prompt

I want you to create some modules to separate the responsibilities we currently have in the project.

What I expect to find related to api.py:

api/init.py
api/app.py -> here, there should be a create_app function that will return the app fully configured.
api/server.py -> here, there should be things related to the server (CustomGunicornApp and run_server).
api/middlewares.py -> middlewares used.
api/helpers.py -> any helper that is used.
api/routers/init.py
api/routers/topics.py -> routes with the topics tag
api/routers/subscriptions.py -> routes with the subscriptions tag
api/routers/monitoring.py -> routes with the monitoring tag

What I expect to find related to services.py:

services/init.py
services/topics.py -> everything related to topics.
services/subscriptions.py -> everything related to subscriptions.
services/messages.py -> everything related to message publishing and consumption.
services/helpers.py -> any helper that is used.

I also want the tests to be refactored to follow the same hierarchy as the new modules.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits December 31, 2025 15:26
Co-authored-by: allisson <5202+allisson@users.noreply.github.com>
Co-authored-by: allisson <5202+allisson@users.noreply.github.com>
… helpers

Co-authored-by: allisson <5202+allisson@users.noreply.github.com>
Copilot AI changed the title [WIP] Create modules for api and services separation Refactor monolithic api.py and services.py into modular structure Dec 31, 2025
Copilot AI requested a review from allisson December 31, 2025 15:34
@allisson allisson marked this pull request as ready for review December 31, 2025 15:35
@allisson allisson changed the title Refactor monolithic api.py and services.py into modular structure refactor: refactor monolithic api.py and services.py into modular structure Dec 31, 2025
@allisson allisson merged commit 7665fe4 into main Dec 31, 2025
1 check passed
@allisson allisson deleted the copilot/create-api-and-services-modules branch December 31, 2025 15:38
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

Successfully merging this pull request may close these issues.

2 participants