- Phase 2a marked as complete (DEV ready) - Updated metrics: 15,577 → 13,820 lines (-11.3%) - Documented Alias Bridge methodology - Updated harmonogram Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
5.5 KiB
5.5 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 - ✅ GOTOWA DO WDROŻENIA
Data zakończenia: 2026-01-31
Strategia: Alias Bridge (bezpieczna migracja)
Commity: d5adf02 (blueprinty), cdd050a (cleanup)
| Blueprint | Routes | Status |
|---|---|---|
| auth | 20 | ✅ Gotowy, aliasy aktywne |
| public | 11 | ✅ Gotowy, aliasy aktywne |
Pliki utworzone:
blueprints/auth/__init__.pyblueprints/auth/routes.py(1,040 linii)blueprints/public/__init__.pyblueprints/public/routes.py(862 linii)
Redukcja app.py:
- Przed: 15,577 linii
- Po: 13,820 linii
- Usunięto: 1,757 linii (11.3%)
Testy (DEV):
- ✅ Wszystkie endpointy działają
- ✅ Aliasy:
url_for('login')=url_for('auth.login') - ✅ Istniejące blueprinty działają
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 + cleanup | 31 | ✅ GOTOWA (DEV) |
| 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 (2026-01-28)
- app.py: 15,570 → 13,699 linii (-12%)
Po Fazie 2a (2026-01-31)
- app.py: 15,577 → 13,820 linii
- Usunięto: 1,757 linii (11.3%)
- Nowe pliki:
blueprints/auth/routes.py(1,040 linii)blueprints/public/routes.py(862 linii)
Łączna redukcja app.py
- Start: 15,570 linii
- Po Fazie 1: 13,699 linii (-12.0%)
- Po Fazie 2a: 13,820 linii (-11.2% od startu)
- Cel końcowy: ~500 linii
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