diff --git a/.gitignore b/.gitignore index f994e0f..3ea4c5e 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ instance/ # Environment .env +config.json .env.local .env.development.local .env.test.local diff --git a/app.py b/app.py index 33ae537..1785036 100644 --- a/app.py +++ b/app.py @@ -39,6 +39,7 @@ Socket events (server → client) """ import os +import json import time import uuid import base64 @@ -57,21 +58,42 @@ from flask_socketio import SocketIO, emit, join_room, disconnect from database import db, init_db from models import User, Message, UserIgnore +# --------------------------------------------------------------------------- +# Configuration Loader +# --------------------------------------------------------------------------- + +def load_config(): + conf = {} + config_path = os.path.join(os.path.dirname(__file__), "config.json") + if os.path.exists(config_path): + try: + with open(config_path, "r") as f: + conf = json.load(f) + except Exception as e: + print(f"⚠️ Warning: Failed to load config.json: {e}") + return conf + +_CONFIG = load_config() + +def _get_conf(key, default=None): + # Order: Env Var > Config File > Default + return os.environ.get(key, _CONFIG.get(key, default)) + # --------------------------------------------------------------------------- # Configuration # --------------------------------------------------------------------------- -SECRET_KEY = os.environ.get("SECRET_KEY", uuid.uuid4().hex) -JWT_SECRET = os.environ.get("JWT_SECRET", uuid.uuid4().hex) -ADMIN_PASSWORD = os.environ.get("ADMIN_PASSWORD", "admin1234") +SECRET_KEY = _get_conf("SECRET_KEY", uuid.uuid4().hex) +JWT_SECRET = _get_conf("JWT_SECRET", uuid.uuid4().hex) +ADMIN_PASSWORD = _get_conf("ADMIN_PASSWORD", "admin1234") MAX_MSG_LEN = 500 LOBBY = "lobby" -AI_FREE_LIMIT = 3 +AI_FREE_LIMIT = int(_get_conf("AI_FREE_LIMIT", 3)) AI_BOT_NAME = "Violet" # Ollama -OLLAMA_URL = os.environ.get("OLLAMA_URL", "http://localhost:11434") -VIOLET_MODEL = os.environ.get("VIOLET_MODEL", "sam860/dolphin3-llama3.2:3b") +OLLAMA_URL = _get_conf("OLLAMA_URL", "http://localhost:11434") +VIOLET_MODEL = _get_conf("VIOLET_MODEL", "sam860/dolphin3-llama3.2:3b") VIOLET_SYSTEM = ( "You are Violet, a flirtatious and sophisticated nightclub hostess at " "an exclusive, dimly-lit members-only club. You are charming, witty, " diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000..f81632f --- /dev/null +++ b/config.example.json @@ -0,0 +1,12 @@ +{ + "HOST": "0.0.0.0", + "PORT": 5000, + "SECRET_KEY": "sexchat-very-secret-key-change-me", + "JWT_SECRET": "sexchat-jwt-secret-key-change-me", + "ADMIN_PASSWORD": "admin", + "OLLAMA_URL": "http://localhost:11434", + "VIOLET_MODEL": "sam860/dolphin3-llama3.2:3b", + "DATABASE_URL": "sqlite:///instance/sexchat.db", + "REDIS_URL": "redis://localhost:6379/0", + "AI_FREE_LIMIT": 3 +} diff --git a/start.py b/start.py index 02f8564..51cbc9c 100644 --- a/start.py +++ b/start.py @@ -12,6 +12,7 @@ Usage: import os import sys +import json import subprocess import signal import time @@ -23,6 +24,21 @@ eventlet.monkey_patch() # PID file to track the daemon process PID_FILE = "sexchat.pid" +def load_config(): + conf = {} + config_path = os.path.join(os.path.dirname(__file__), "config.json") + if os.path.exists(config_path): + try: + with open(config_path, "r") as f: + conf = json.load(f) + except Exception: + pass + return conf + +def _get_conf(key, default=None): + conf = load_config() + return os.environ.get(key, conf.get(key, default)) + def get_pid(): if os.path.exists(PID_FILE): with open(PID_FILE, "r") as f: @@ -52,7 +68,7 @@ def start_daemon(): "gunicorn", "--worker-class", "eventlet", "-w", "1", - "--bind", f"{os.environ.get('HOST', '0.0.0.0')}:{os.environ.get('PORT', '5000')}", + "--bind", f"{_get_conf('HOST', '0.0.0.0')}:{_get_conf('PORT', 5000)}", "--daemon", "--pid", PID_FILE, "--access-logfile", "access.log", @@ -108,7 +124,7 @@ def run_debug(): "gunicorn", "--worker-class", "eventlet", "-w", "1", - "--bind", f"{os.environ.get('HOST', '0.0.0.0')}:{os.environ.get('PORT', '5000')}", + "--bind", f"{_get_conf('HOST', '0.0.0.0')}:{_get_conf('PORT', 5000)}", "--log-level", "debug", "--access-logfile", "-", "--error-logfile", "-",