Skip to content

Commit

Permalink
added randomized port selection for production
Browse files Browse the repository at this point in the history
  • Loading branch information
ensaremirerol committed Jan 6, 2025
1 parent 0fd5c7f commit 0ee63a0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
11 changes: 10 additions & 1 deletion app/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ import React from 'react';
import App from './app';
import ApiService from './lib/services/api_service';

ApiService.registerWithNamespace('default', 'http://localhost:8000/api/');
// If environment is development, register the default namespace with the local API server

if (process.env.NODE_ENV === 'development') {
ApiService.registerWithNamespace('default', 'http://localhost:8000/api/');
}

// else use the default namespace with the production API server
else {
ApiService.registerWithNamespace('default', '/api/');
}

const root = createRoot(document.getElementById('root')!);

Expand Down
29 changes: 27 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import random
import socket
import threading

import webview
Expand Down Expand Up @@ -27,11 +29,34 @@
)


def get_free_port():
while True:
port = random.randint(
1024, 65535
) # Ports below 1024 are reserved
with socket.socket(
socket.AF_INET, socket.SOCK_STREAM
) as s:
try:
s.bind(
("", port)
) # Bind to the port on all network interfaces
s.listen(
1
) # Start listening to check if the port is available
return port # If binding succeeds, the port is free
except OSError:
continue # If binding fails, try another port


port = 8000 if DEBUG else get_free_port()


def start_fastapi():
import uvicorn

uvicorn.run(
app, host="0.0.0.0", port=8000, log_config=None
app, host="0.0.0.0", port=port, log_config=None
)


Expand All @@ -52,7 +77,7 @@ def on_closing():
# Create window
window = webview.create_window(
"RDFCraft",
"http://localhost:8000",
f"http://localhost:{port}",
width=800,
height=600,
resizable=True,
Expand Down

0 comments on commit 0ee63a0

Please sign in to comment.