Initial import: WebAisMap
Closes TG-4
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import os
|
||||
import threading
|
||||
from urllib.request import urlopen
|
||||
from urllib.error import URLError
|
||||
|
||||
from routes import app, AIS_HUB_URL
|
||||
from ssl_utils import get_ssl_context, run_http_redirect, _get_local_ips
|
||||
|
||||
|
||||
def _check_ais_hub():
|
||||
"""Однократная проверка доступности ais_hub на старте (не критично)."""
|
||||
try:
|
||||
resp = urlopen(f"{AIS_HUB_URL}/api/v1/health", timeout=2.0)
|
||||
if getattr(resp, "status", 200) == 200:
|
||||
print(f"[ais_hub] OK: {AIS_HUB_URL}")
|
||||
return
|
||||
print(f"[ais_hub] unexpected status {resp.status} from {AIS_HUB_URL}")
|
||||
except URLError as e:
|
||||
print(f"[ais_hub] WARNING: {AIS_HUB_URL} недоступен ({e.reason}). "
|
||||
f"AIS-данные и /ws не будут работать, пока ais_hub не поднят.")
|
||||
except Exception as e:
|
||||
print(f"[ais_hub] WARNING: проверка {AIS_HUB_URL} не удалась: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
_check_ais_hub()
|
||||
|
||||
# Геолокация в браузере (телефон) требует secure context (HTTPS или localhost).
|
||||
# Поэтому HTTPS можно включать/выключать флагом окружения.
|
||||
use_https = os.environ.get("AISMAP_HTTPS", "1").strip().lower() not in ("0", "false", "no", "off")
|
||||
|
||||
if use_https:
|
||||
ssl_ctx = get_ssl_context()
|
||||
else:
|
||||
ssl_ctx = None
|
||||
|
||||
if ssl_ctx:
|
||||
https_port = int(os.environ.get("AISMAP_HTTPS_PORT", "443"))
|
||||
enable_redirect = os.environ.get("AISMAP_HTTP_REDIRECT", "1").strip().lower() not in ("0", "false", "no", "off")
|
||||
if enable_redirect:
|
||||
redir_thread = threading.Thread(target=run_http_redirect, args=(https_port,), daemon=True)
|
||||
redir_thread.start()
|
||||
|
||||
local_ips = _get_local_ips()
|
||||
print(f"[HTTPS] Сервер запускается на порту {https_port}")
|
||||
for ip in local_ips:
|
||||
port_suffix = f":{https_port}" if https_port != 443 else ""
|
||||
print(f"[HTTPS] Карта: https://{ip}{port_suffix}/")
|
||||
print(f"[HTTPS] Сертификат: https://{ip}{port_suffix}/cert")
|
||||
print("[HTTPS] Чтобы убрать предупреждение — откройте /cert и установите CA-сертификат")
|
||||
|
||||
app.run(host="0.0.0.0", port=https_port, debug=True,
|
||||
use_reloader=False, threaded=True, ssl_context=ssl_ctx)
|
||||
else:
|
||||
print("[HTTP] Запуск без HTTPS (геолокация телефона в браузере работать не будет)")
|
||||
local_ips = _get_local_ips()
|
||||
for ip in local_ips:
|
||||
print(f"[HTTP] Карта: http://{ip}/")
|
||||
app.run(host="0.0.0.0", port=80, debug=True,
|
||||
use_reloader=False, threaded=True)
|
||||
Reference in New Issue
Block a user