modified: app.py

modified:   db_client/instance.json
This commit is contained in:
2026-06-23 16:12:35 -04:00
parent 0e91ff9874
commit 5ba1c40359
2 changed files with 85 additions and 81 deletions

43
app.py
View File

@@ -29,7 +29,7 @@ if getattr(sys, 'frozen', False) and sys.platform == "win32":
sys.stdout = open(log_file, 'w', encoding='utf-8') sys.stdout = open(log_file, 'w', encoding='utf-8')
sys.stderr = sys.stdout sys.stderr = sys.stdout
# --- FLASK INIT --- def create_app(mode=None):
app = Flask( app = Flask(
__name__, __name__,
template_folder=get_bundled_path('templates'), template_folder=get_bundled_path('templates'),
@@ -37,18 +37,16 @@ app = Flask(
) )
app.config['SECRET_KEY'] = 'seki_super_secret_key_99' app.config['SECRET_KEY'] = 'seki_super_secret_key_99'
# --- PORT --- if mode is None:
mode = os.environ.get('SEKIPOS_MODE', 'desktop')
PORT = int(os.environ.get('PORT', 5000)) PORT = int(os.environ.get('PORT', 5000))
app.config['PORT'] = PORT
# --- MODE ---
SEKIPOS_MODE = os.environ.get('SEKIPOS_MODE', 'desktop')
# --- SYNC SECRET ---
SYNC_SECRET = os.environ.get('SEKIPOS_SYNC_SECRET', '').strip() SYNC_SECRET = os.environ.get('SEKIPOS_SYNC_SECRET', '').strip()
if SEKIPOS_MODE == 'server' and not SYNC_SECRET: if mode == 'server' and not SYNC_SECRET:
print("[WARN] SEKIpos_SYNC_SECRET not set — sync API is unprotected!") 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')) DB_DIR = os.environ.get('SEKIPOS_DB_DIR', get_persistent_path('db'))
os.makedirs(DB_DIR, exist_ok=True) os.makedirs(DB_DIR, exist_ok=True)
DB_FILE = os.path.join(DB_DIR, "pos_database.db") DB_FILE = os.path.join(DB_DIR, "pos_database.db")
@@ -65,7 +63,7 @@ if not os.path.exists(INSTANCE_FILE):
config = { config = {
"instance_id": str(uuid.uuid4()), "instance_id": str(uuid.uuid4()),
"display_name": "Caja Principal", "display_name": "Caja Principal",
"mode": SEKIPOS_MODE, "mode": mode,
"server_url": "", "server_url": "",
"last_sync_at": None "last_sync_at": None
} }
@@ -74,7 +72,7 @@ if not os.path.exists(INSTANCE_FILE):
else: else:
with open(INSTANCE_FILE) as f: with open(INSTANCE_FILE) as f:
config = json.load(f) config = json.load(f)
config['mode'] = SEKIPOS_MODE config['mode'] = mode
with open(INSTANCE_FILE, 'w') as f: with open(INSTANCE_FILE, 'w') as f:
json.dump(config, f, indent=2) json.dump(config, f, indent=2)
@@ -85,7 +83,7 @@ SERVER_URL = config.get('server_url', '')
app.config['INSTANCE_ID'] = INSTANCE_ID app.config['INSTANCE_ID'] = INSTANCE_ID
app.config['DISPLAY_NAME'] = DISPLAY_NAME app.config['DISPLAY_NAME'] = DISPLAY_NAME
app.config['SERVER_URL'] = SERVER_URL app.config['SERVER_URL'] = SERVER_URL
app.config['MODE'] = SEKIPOS_MODE app.config['MODE'] = mode
app.config['SYNC_SECRET'] = SYNC_SECRET app.config['SYNC_SECRET'] = SYNC_SECRET
# --- BLUEPRINT REGISTRATION --- # --- BLUEPRINT REGISTRATION ---
@@ -95,7 +93,7 @@ app.register_blueprint(inventory_bp)
app.register_blueprint(pos_bp) app.register_blueprint(pos_bp)
app.register_blueprint(sales_bp) app.register_blueprint(sales_bp)
if SEKIPOS_MODE == 'server': if mode == 'server':
app.register_blueprint(sync_bp) app.register_blueprint(sync_bp)
init_login_manager(app) init_login_manager(app)
@@ -111,24 +109,29 @@ def index():
return redirect(url_for('inventory.inventory')) return redirect(url_for('inventory.inventory'))
# --- SYNC CLIENT --- # --- SYNC CLIENT ---
if SEKIPOS_MODE == 'desktop' and SERVER_URL: if mode == 'desktop' and SERVER_URL:
sync_secret = config.get('sync_secret', '') sync_secret = config.get('sync_secret', '')
sync_mgr = SyncManager(DB_FILE, INSTANCE_ID, SERVER_URL, DISPLAY_NAME, sync_secret) sync_mgr = SyncManager(DB_FILE, INSTANCE_ID, SERVER_URL, DISPLAY_NAME, sync_secret)
sync_mgr.start() sync_mgr.start()
print(f"[Sync] Desktop mode — syncing to {SERVER_URL}") print(f"[Sync] Desktop mode — syncing to {SERVER_URL}")
# --- RUN FUNCTION --- return app
def start_server():
socketio.run(app, host='127.0.0.1', port=PORT, log_output=False, allow_unsafe_werkzeug=True) app = create_app()
def run_standalone(): 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.daemon = True
t.start() t.start()
time.sleep(2) 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) webview.start(private_mode=False)
if __name__ == '__main__': if __name__ == '__main__':
#run_standalone() # Uncomment for desktop app if os.environ.get('SEKIPOS_MODE') == 'server':
socketio.run(app, host='0.0.0.0', port=PORT, debug=True, allow_unsafe_werkzeug=True) port = app.config['PORT']
socketio.run(app, host='0.0.0.0', port=port, debug=True, allow_unsafe_werkzeug=True)
else:
run_standalone()

View File

@@ -2,6 +2,7 @@
"instance_id": "f35f82c4-2cc7-4a29-bce7-9c8b0d451a0e", "instance_id": "f35f82c4-2cc7-4a29-bce7-9c8b0d451a0e",
"display_name": "Caja Principal", "display_name": "Caja Principal",
"mode": "desktop", "mode": "desktop",
"server_url": "http://192.168.1.103:5000", "server_url": "http://192.168.1.87:5000",
"last_sync_at": null "last_sync_at": null,
"sync_secret": "secetkey"
} }