diff --git a/app.py b/app.py index 24f3f41..500daa5 100644 --- a/app.py +++ b/app.py @@ -29,106 +29,109 @@ if getattr(sys, 'frozen', False) and sys.platform == "win32": sys.stdout = open(log_file, 'w', encoding='utf-8') sys.stderr = sys.stdout -# --- FLASK INIT --- -app = Flask( - __name__, - template_folder=get_bundled_path('templates'), - static_folder=get_bundled_path('static') -) -app.config['SECRET_KEY'] = 'seki_super_secret_key_99' +def create_app(mode=None): + app = Flask( + __name__, + template_folder=get_bundled_path('templates'), + static_folder=get_bundled_path('static') + ) + app.config['SECRET_KEY'] = 'seki_super_secret_key_99' -# --- PORT --- -PORT = int(os.environ.get('PORT', 5000)) + if mode is None: + mode = os.environ.get('SEKIPOS_MODE', 'desktop') -# --- MODE --- -SEKIPOS_MODE = os.environ.get('SEKIPOS_MODE', 'desktop') + PORT = int(os.environ.get('PORT', 5000)) + app.config['PORT'] = PORT -# --- SYNC SECRET --- -SYNC_SECRET = os.environ.get('SEKIPOS_SYNC_SECRET', '').strip() -if SEKIPOS_MODE == 'server' and not SYNC_SECRET: - print("[WARN] SEKIpos_SYNC_SECRET not set — sync API is unprotected!") + SYNC_SECRET = os.environ.get('SEKIPOS_SYNC_SECRET', '').strip() + if mode == 'server' and not SYNC_SECRET: + print("[WARN] SEKIpos_SYNC_SECRET not set — sync API is unprotected!") -# --- DIRECTORY SETUP --- -DB_DIR = os.environ.get('SEKIPOS_DB_DIR', get_persistent_path('db')) -os.makedirs(DB_DIR, exist_ok=True) -DB_FILE = os.path.join(DB_DIR, "pos_database.db") -app.config['DB_FILE'] = DB_FILE + DB_DIR = os.environ.get('SEKIPOS_DB_DIR', get_persistent_path('db')) + os.makedirs(DB_DIR, exist_ok=True) + DB_FILE = os.path.join(DB_DIR, "pos_database.db") + app.config['DB_FILE'] = DB_FILE -CACHE_DIR = get_persistent_path(os.path.join('static', 'cache')) -os.makedirs(CACHE_DIR, exist_ok=True) -app.config['CACHE_DIR'] = CACHE_DIR + CACHE_DIR = get_persistent_path(os.path.join('static', 'cache')) + os.makedirs(CACHE_DIR, exist_ok=True) + app.config['CACHE_DIR'] = CACHE_DIR -# --- INSTANCE CONFIG --- -INSTANCE_FILE = os.path.join(DB_DIR, 'instance.json') + # --- INSTANCE CONFIG --- + INSTANCE_FILE = os.path.join(DB_DIR, 'instance.json') -if not os.path.exists(INSTANCE_FILE): - config = { - "instance_id": str(uuid.uuid4()), - "display_name": "Caja Principal", - "mode": SEKIPOS_MODE, - "server_url": "", - "last_sync_at": None - } - with open(INSTANCE_FILE, 'w') as f: - json.dump(config, f, indent=2) -else: - with open(INSTANCE_FILE) as f: - config = json.load(f) - config['mode'] = SEKIPOS_MODE - with open(INSTANCE_FILE, 'w') as f: - json.dump(config, f, indent=2) + if not os.path.exists(INSTANCE_FILE): + config = { + "instance_id": str(uuid.uuid4()), + "display_name": "Caja Principal", + "mode": mode, + "server_url": "", + "last_sync_at": None + } + with open(INSTANCE_FILE, 'w') as f: + json.dump(config, f, indent=2) + else: + with open(INSTANCE_FILE) as f: + config = json.load(f) + config['mode'] = mode + with open(INSTANCE_FILE, 'w') as f: + json.dump(config, f, indent=2) -INSTANCE_ID = config['instance_id'] -DISPLAY_NAME = config.get('display_name', 'Caja Principal') -SERVER_URL = config.get('server_url', '') + INSTANCE_ID = config['instance_id'] + DISPLAY_NAME = config.get('display_name', 'Caja Principal') + SERVER_URL = config.get('server_url', '') -app.config['INSTANCE_ID'] = INSTANCE_ID -app.config['DISPLAY_NAME'] = DISPLAY_NAME -app.config['SERVER_URL'] = SERVER_URL -app.config['MODE'] = SEKIPOS_MODE -app.config['SYNC_SECRET'] = SYNC_SECRET + app.config['INSTANCE_ID'] = INSTANCE_ID + app.config['DISPLAY_NAME'] = DISPLAY_NAME + app.config['SERVER_URL'] = SERVER_URL + app.config['MODE'] = mode + app.config['SYNC_SECRET'] = SYNC_SECRET -# --- BLUEPRINT REGISTRATION --- -app.register_blueprint(auth_bp) -app.register_blueprint(finance_bp) -app.register_blueprint(inventory_bp) -app.register_blueprint(pos_bp) -app.register_blueprint(sales_bp) + # --- BLUEPRINT REGISTRATION --- + app.register_blueprint(auth_bp) + app.register_blueprint(finance_bp) + app.register_blueprint(inventory_bp) + app.register_blueprint(pos_bp) + app.register_blueprint(sales_bp) -if SEKIPOS_MODE == 'server': - app.register_blueprint(sync_bp) + if mode == 'server': + app.register_blueprint(sync_bp) -init_login_manager(app) -socketio.init_app(app, cors_allowed_origins="*", async_mode='threading') + init_login_manager(app) + socketio.init_app(app, cors_allowed_origins="*", async_mode='threading') -# --- DATABASE INITIALIZATION --- -init_db_core(DB_FILE) + # --- DATABASE INITIALIZATION --- + init_db_core(DB_FILE) -# --- ROOT ROUTE --- -@app.route('/') -@login_required -def index(): - return redirect(url_for('inventory.inventory')) + # --- ROOT ROUTE --- + @app.route('/') + @login_required + def index(): + return redirect(url_for('inventory.inventory')) -# --- SYNC CLIENT --- -if SEKIPOS_MODE == 'desktop' and SERVER_URL: - sync_secret = config.get('sync_secret', '') - sync_mgr = SyncManager(DB_FILE, INSTANCE_ID, SERVER_URL, DISPLAY_NAME, sync_secret) - sync_mgr.start() - print(f"[Sync] Desktop mode — syncing to {SERVER_URL}") + # --- SYNC CLIENT --- + if mode == 'desktop' and SERVER_URL: + sync_secret = config.get('sync_secret', '') + sync_mgr = SyncManager(DB_FILE, INSTANCE_ID, SERVER_URL, DISPLAY_NAME, sync_secret) + sync_mgr.start() + print(f"[Sync] Desktop mode — syncing to {SERVER_URL}") -# --- RUN FUNCTION --- -def start_server(): - socketio.run(app, host='127.0.0.1', port=PORT, log_output=False, allow_unsafe_werkzeug=True) + return app + +app = create_app() def run_standalone(): - t = threading.Thread(target=start_server) + local_app = create_app(mode='desktop') + port = local_app.config['PORT'] + t = threading.Thread(target=lambda: socketio.run(local_app, host='127.0.0.1', port=port, log_output=False, allow_unsafe_werkzeug=True)) t.daemon = True t.start() time.sleep(2) - webview.create_window('SekiPOS', f'http://127.0.0.1:{PORT}', width=1366, height=768, resizable=True, fullscreen=False, min_size=(800, 600), maximized=True) + webview.create_window('SekiPOS', f'http://127.0.0.1:{port}', width=1366, height=768, resizable=True, fullscreen=False, min_size=(800, 600), maximized=True) webview.start(private_mode=False) if __name__ == '__main__': - #run_standalone() # Uncomment for desktop app - socketio.run(app, host='0.0.0.0', port=PORT, debug=True, allow_unsafe_werkzeug=True) \ No newline at end of file + if os.environ.get('SEKIPOS_MODE') == 'server': + port = app.config['PORT'] + socketio.run(app, host='0.0.0.0', port=port, debug=True, allow_unsafe_werkzeug=True) + else: + run_standalone() diff --git a/db_client/instance.json b/db_client/instance.json index 5ff323a..4797cd9 100644 --- a/db_client/instance.json +++ b/db_client/instance.json @@ -2,6 +2,7 @@ "instance_id": "f35f82c4-2cc7-4a29-bce7-9c8b0d451a0e", "display_name": "Caja Principal", "mode": "desktop", - "server_url": "http://192.168.1.103:5000", - "last_sync_at": null + "server_url": "http://192.168.1.87:5000", + "last_sync_at": null, + "sync_secret": "secetkey" } \ No newline at end of file