36 lines
976 B
Python
36 lines
976 B
Python
import json
|
|
import os
|
|
from urllib.parse import urlparse
|
|
|
|
CONFIG_PATH = os.path.join(os.path.dirname(__file__), "..", "config.json")
|
|
|
|
def load_config():
|
|
if not os.path.exists(CONFIG_PATH):
|
|
return {}
|
|
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
|
|
return json.load(f)
|
|
|
|
def save_config(new_config):
|
|
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
|
|
json.dump(new_config, f, indent=4, ensure_ascii=False)
|
|
|
|
|
|
def get_server_settings(config):
|
|
"""Return (scheme, host, port) using defaults when not configured."""
|
|
scheme = "https"
|
|
host = ""
|
|
port = config.get("server_port", "32400") or "32400"
|
|
|
|
url = config.get("server_url", "")
|
|
if url:
|
|
parsed = urlparse(url)
|
|
if parsed.scheme:
|
|
scheme = parsed.scheme
|
|
host = parsed.hostname or parsed.netloc
|
|
if parsed.port:
|
|
port = str(parsed.port)
|
|
else:
|
|
host = url
|
|
|
|
return scheme, host, port
|