Phase 2a of modular monolith refactoring:
New blueprints:
- blueprints/auth/routes.py (1,040 lines, 20 routes)
- login, logout, register, verify_2fa, settings_2fa
- forgot_password, reset_password, verify_email
- konto_dane, konto_prywatnosc, konto_bezpieczenstwo, konto_blokady
- blueprints/public/routes.py (862 lines, 11 routes)
- index, company_detail, person_detail, search
- dashboard, events, new_members, release_notes
Alias Bridge strategy:
- Both url_for('login') and url_for('auth.login') work
- Templates don't require changes (backward compatible)
- Original routes in app.py marked with _old_ prefix (dead code)
Next step: Cleanup dead code from app.py after production verification
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
5.3 KiB
5.3 KiB
Status Refaktoringu app.py
Ostatnia aktualizacja: 2026-01-31 Autor sesji: Claude Opus 4.5
Stan obecny
Faza 1 - ✅ WDROŻONA NA PRODUKCJĘ
Commit: 66856a6
Data wdrożenia: 2026-01-28
| Blueprint | URL Prefix | Routes | Status |
|---|---|---|---|
| reports | /raporty |
4 | ✅ Przetestowane |
| contacts | /kontakty |
6 | ✅ Przetestowane |
| classifieds | /tablica |
4 | ✅ Przetestowane |
| calendar | /kalendarz |
3 | ✅ Przetestowane |
| education | /edukacja |
2 | ✅ Przetestowane |
Faza 2a - 🔄 W TRAKCIE (DEV)
Data rozpoczęcia: 2026-01-31 Strategia: Alias Bridge (bezpieczna migracja)
| Blueprint | Routes | Status |
|---|---|---|
| auth | 20 | ✅ Utworzony, aliasy aktywne |
| public | 11 | ✅ Utworzony, aliasy aktywne |
Pliki utworzone:
blueprints/auth/__init__.pyblueprints/auth/routes.py(1,040 linii)blueprints/public/__init__.pyblueprints/public/routes.py(862 linii)
Stan app.py:
- Duplikaty tras zakomentowane (prefix
_old_) - Aliasy aktywne w
blueprints/__init__.py - Oczekuje na cleanup martwego kodu
Metodologia Refaktoringu (Alias Bridge)
Dlaczego ta metoda?
Problem: 125+ wywołań url_for() w szablonach i kodzie używa starych nazw (url_for('login')).
Rozwiązanie: Aliasy pozwalają na stopniową migrację bez "Big Bang".
Procedura dla każdej fazy
Krok 1: Utworzenie blueprintu
mkdir -p blueprints/<nazwa>
touch blueprints/<nazwa>/__init__.py
touch blueprints/<nazwa>/routes.py
# blueprints/<nazwa>/__init__.py
from flask import Blueprint
bp = Blueprint('<nazwa>', __name__)
from . import routes
Krok 2: Przeniesienie tras
- Skopiuj funkcje z app.py do
blueprints/<nazwa>/routes.py - Zmień
@app.routena@bp.route - Zaktualizuj importy (używaj
from extensions import limiter) - Wewnętrzne url_for: użyj
.endpoint(z kropką)
Krok 3: Rejestracja + aliasy
# blueprints/__init__.py
from blueprints.<nazwa> import bp as <nazwa>_bp
app.register_blueprint(<nazwa>_bp)
_create_endpoint_aliases(app, <nazwa>_bp, {
'stara_nazwa': '<nazwa>.nowa_nazwa',
# ...
})
Krok 4: Dezaktywacja duplikatów w app.py
# PRZED:
@app.route('/endpoint')
def funkcja():
# PO:
# @app.route('/endpoint') # MOVED TO <nazwa>.<endpoint>
def _old_funkcja():
Krok 5: Test
python3 -c "from app import app; print('OK')"
# Test wszystkich endpointów
Krok 6: Cleanup (po weryfikacji na PROD)
Usuń funkcje z prefiksem _old_ z app.py.
Harmonogram
Szczegółowy plan: docs/MODULAR_MONOLITH_PLAN.md
Podsumowanie faz
| Faza | Zakres | Routes | Status |
|---|---|---|---|
| 1 | reports, community, education | 19 | ✅ WDROŻONA |
| 2a | auth + public | 31 | 🔄 DEV - aliasy aktywne |
| 2b | cleanup app.py | - | ⏳ Po teście PROD |
| 3 | account, forum | ~25 | ⏳ |
| 4 | messages, notifications | ~10 | ⏳ |
| 5 | chat | ~8 | ⏳ |
| 6 | admin (8 modułów) | ~60 | ⏳ |
| 7 | audits (6 modułów) | ~35 | ⏳ |
| 8 | zopk (5 modułów) | ~32 | ⏳ |
| 9 | api misc, honeypot | ~25 | ⏳ |
| 10 | final cleanup | - | ⏳ |
Cel końcowy: Redukcja app.py z 15,570 → ~500 linii
Metryki optymalizacji
Po Fazie 1
- app.py: 15,570 → 13,699 linii (-12%)
Po Fazie 2a (przed cleanup)
- app.py: 15,576 linii (+6 komentarzy)
- Nowe: auth/routes.py (1,040) + public/routes.py (862)
- Martwy kod do usunięcia: ~1,500 linii
Po Fazie 2a cleanup (oczekiwane)
- app.py: ~14,000 linii (-10% od stanu wyjściowego)
Weryfikacja przed wdrożeniem
Checklist DEV
# 1. Import aplikacji
python3 -c "from app import app; print('OK')"
# 2. Test endpointów
python3 -c "
from app import app
with app.test_client() as c:
assert c.get('/').status_code == 200
assert c.get('/login').status_code == 200
assert c.get('/health').status_code == 200
print('All endpoints OK')
"
# 3. Test url_for (aliasy)
python3 -c "
from app import app
from flask import url_for
with app.test_request_context():
assert url_for('login') == '/login'
assert url_for('auth.login') == '/login'
assert url_for('index') == '/'
assert url_for('public.index') == '/'
print('Aliases OK')
"
Checklist PROD
# Po wdrożeniu
curl -sI https://nordabiznes.pl/health | head -1
curl -sI https://nordabiznes.pl/ | head -1
curl -sI https://nordabiznes.pl/login | head -1
curl -sI https://nordabiznes.pl/release-notes | head -1
Lekcje na przyszłość
-
url_for w blueprintach:
- Wewnątrz blueprintu:
url_for('.endpoint')(z kropką) - W szablonach:
url_for('blueprint.endpoint')(pełna nazwa) - Aliasy:
url_for('stara_nazwa')=url_for('blueprint.nowa_nazwa')
- Wewnątrz blueprintu:
-
Kolejność operacji:
- Najpierw blueprinty + aliasy
- Potem dezaktywacja duplikatów
- Cleanup dopiero po teście PROD
-
Bezpieczeństwo:
- Zawsze zachowuj martwy kod do weryfikacji
- Prefix
_old_dla zdezaktywowanych funkcji - Rollback: odkomentuj
@app.route
Kontakt
W razie problemów z wdrożeniem sprawdź:
- Logi:
/var/log/nordabiznes/ - Health check:
curl https://nordabiznes.pl/health - Rollback:
git revert HEAD && git push