naglowek i mod healthchecka
This commit is contained in:
39
app.py
39
app.py
@@ -20,6 +20,14 @@ from sqlalchemy.engine import Engine
|
||||
from decimal import Decimal, InvalidOperation
|
||||
from flask import request, flash, abort
|
||||
|
||||
# do liczenia stats app
|
||||
import psutil
|
||||
import os
|
||||
|
||||
app_start_time = datetime.now()
|
||||
request_counter = 0
|
||||
|
||||
|
||||
try:
|
||||
from zoneinfo import ZoneInfo # Python 3.9+
|
||||
except ImportError:
|
||||
@@ -204,6 +212,12 @@ class UstawieniaGlobalne(db.Model):
|
||||
kolejnosc_rezerwowych = db.Column(db.String(20), default="id", nullable=False)
|
||||
|
||||
|
||||
# Middleware do liczenia requestów
|
||||
@app.before_request
|
||||
def count_requests():
|
||||
global request_counter
|
||||
request_counter += 1
|
||||
|
||||
@login_manager.user_loader
|
||||
def load_user(user_id):
|
||||
return db.session.get(Uzytkownik, int(user_id))
|
||||
@@ -920,6 +934,12 @@ def apply_headers(response):
|
||||
response.make_conditional(request)
|
||||
return response
|
||||
|
||||
if request.path == '/healthcheck':
|
||||
response.headers['Cache-Control'] = 'no-store, no-cache'
|
||||
response.headers.pop('ETag', None)
|
||||
response.headers.pop('Vary', None)
|
||||
return response
|
||||
|
||||
path_norm = request.path.lstrip("/")
|
||||
czy_admin = path_norm.startswith("admin/") or path_norm == "admin"
|
||||
|
||||
@@ -1622,10 +1642,25 @@ def favicon():
|
||||
def healthcheck():
|
||||
header_token = request.headers.get("X-Internal-Check")
|
||||
correct_token = app.config.get("HEALTHCHECK_TOKEN")
|
||||
|
||||
|
||||
if header_token != correct_token:
|
||||
abort(404)
|
||||
return "OK", 200
|
||||
|
||||
# Metryki
|
||||
uptime = datetime.now() - app_start_time
|
||||
uptime_str = str(uptime).split('.')[0] # bez mikrosekund
|
||||
|
||||
process = psutil.Process(os.getpid())
|
||||
memory_mb = process.memory_info().rss / 1024 / 1024
|
||||
|
||||
response_data = {
|
||||
"status": "OK",
|
||||
"uptime": uptime_str,
|
||||
"memory_mb": round(memory_mb, 2),
|
||||
"requests": request_counter
|
||||
}
|
||||
|
||||
return response_data, 200
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user