Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 PrintQue

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
37 changes: 31 additions & 6 deletions api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,32 @@ def cleanup_on_exit():
# Register the cleanup function
atexit.register(cleanup_on_exit)

def open_browser():
def find_available_port(start_port: int, max_attempts: int = 10) -> int:
"""Find an available port, starting from start_port and incrementing if taken."""
import socket

for attempt in range(max_attempts):
port = start_port + attempt
try:
# Try to bind to the port to check availability
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('0.0.0.0', port))
sock.close()

if port != start_port:
logging.warning(f"Port {start_port} was in use, using port {port} instead")
return port
except OSError:
logging.debug(f"Port {port} is in use, trying next...")
continue

raise RuntimeError(f"Could not find available port after {max_attempts} attempts starting from {start_port}")

def open_browser(port: int):
"""Open the default web browser after a short delay"""
time.sleep(2) # Wait for server to start
url = f"http://localhost:{Config.PORT}"
url = f"http://localhost:{port}"
try:
webbrowser.open(url)
logging.info(f"Opened browser to {url}")
Expand All @@ -259,6 +281,9 @@ def open_browser():
# Start console capture
console_capture.start()

# Find available port (auto-increment if default is taken)
actual_port = find_available_port(Config.PORT)

# Start the application without password check
start_background_tasks(socketio, app)

Expand All @@ -274,18 +299,18 @@ def open_browser():
logging.info(" ║ ║")
logging.info(" ╚═══════════════════════════════════════════════╝")
logging.info("")
logging.info(f" Server running at: http://localhost:{Config.PORT}")
logging.info(f" Network access: http://0.0.0.0:{Config.PORT}")
logging.info(f" Server running at: http://localhost:{actual_port}")
logging.info(f" Network access: http://0.0.0.0:{actual_port}")
logging.info("")
logging.info("=" * 60)
logging.info("=" * 60)
logging.info("")

# Open browser automatically (only for packaged builds or when explicitly requested)
if getattr(sys, 'frozen', False) or os.environ.get('OPEN_BROWSER', '').lower() == 'true':
browser_thread = threading.Thread(target=open_browser, daemon=True)
browser_thread = threading.Thread(target=open_browser, args=(actual_port,), daemon=True)
browser_thread.start()

# Run the Flask app
# Added app.config['DEBUG'] for the debug flag
socketio.run(app, host='0.0.0.0', port=Config.PORT, debug=app.config.get('DEBUG', False))
socketio.run(app, host='0.0.0.0', port=actual_port, debug=app.config.get('DEBUG', False))
Loading