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

161
app.py
View File

@@ -29,106 +29,109 @@ 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'),
static_folder=get_bundled_path('static') static_folder=get_bundled_path('static')
) )
app.config['SECRET_KEY'] = 'seki_super_secret_key_99' app.config['SECRET_KEY'] = 'seki_super_secret_key_99'
# --- PORT --- if mode is None:
PORT = int(os.environ.get('PORT', 5000)) mode = os.environ.get('SEKIPOS_MODE', 'desktop')
# --- MODE --- PORT = int(os.environ.get('PORT', 5000))
SEKIPOS_MODE = os.environ.get('SEKIPOS_MODE', 'desktop') app.config['PORT'] = PORT
# --- SYNC SECRET --- SYNC_SECRET = os.environ.get('SEKIPOS_SYNC_SECRET', '').strip()
SYNC_SECRET = os.environ.get('SEKIPOS_SYNC_SECRET', '').strip() if mode == 'server' and not SYNC_SECRET:
if SEKIPOS_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") app.config['DB_FILE'] = DB_FILE
app.config['DB_FILE'] = DB_FILE
CACHE_DIR = get_persistent_path(os.path.join('static', 'cache')) CACHE_DIR = get_persistent_path(os.path.join('static', 'cache'))
os.makedirs(CACHE_DIR, exist_ok=True) os.makedirs(CACHE_DIR, exist_ok=True)
app.config['CACHE_DIR'] = CACHE_DIR app.config['CACHE_DIR'] = CACHE_DIR
# --- INSTANCE CONFIG --- # --- INSTANCE CONFIG ---
INSTANCE_FILE = os.path.join(DB_DIR, 'instance.json') INSTANCE_FILE = os.path.join(DB_DIR, 'instance.json')
if not os.path.exists(INSTANCE_FILE): 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
} }
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)
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)
INSTANCE_ID = config['instance_id'] INSTANCE_ID = config['instance_id']
DISPLAY_NAME = config.get('display_name', 'Caja Principal') DISPLAY_NAME = config.get('display_name', 'Caja Principal')
SERVER_URL = config.get('server_url', '') 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 ---
app.register_blueprint(auth_bp) app.register_blueprint(auth_bp)
app.register_blueprint(finance_bp) app.register_blueprint(finance_bp)
app.register_blueprint(inventory_bp) 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)
socketio.init_app(app, cors_allowed_origins="*", async_mode='threading') socketio.init_app(app, cors_allowed_origins="*", async_mode='threading')
# --- DATABASE INITIALIZATION --- # --- DATABASE INITIALIZATION ---
init_db_core(DB_FILE) init_db_core(DB_FILE)
# --- ROOT ROUTE --- # --- ROOT ROUTE ---
@app.route('/') @app.route('/')
@login_required @login_required
def index(): 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"
} }