Skip to content

Commit e8de982

Browse files
feat: replace hardcoded ports/URLs with environment variables
- Add environment variable support for backend server configuration (BACKEND_HOST, BACKEND_PORT) - Add environment variable support for sync microservice configuration (SYNC_MICROSERVICE_HOST, SYNC_MICROSERVICE_PORT, SYNC_MICROSERVICE_URL) - Update frontend to use Vite environment variables with fallback defaults (VITE_BACKEND_URL, VITE_SYNC_MICROSERVICE_URL) - Update .env.example with documentation for all new environment variables This allows: - Running multiple instances on the same machine - Deploying to cloud platforms with dynamic ports - Easy switching between dev/staging/production environments Fixes #1134
1 parent c1a4ce9 commit e8de982

File tree

6 files changed

+82
-14
lines changed

6 files changed

+82
-14
lines changed

.env.example

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,36 @@
1+
# =============================================================================
2+
# PictoPy Environment Configuration
3+
# =============================================================================
4+
# Copy this file to .env and modify values as needed.
5+
# All variables have sensible defaults for local development.
6+
7+
# -----------------------------------------------------------------------------
8+
# Backend Server Configuration
9+
# -----------------------------------------------------------------------------
10+
# Host and port for the main PictoPy backend server
11+
BACKEND_HOST=localhost
12+
BACKEND_PORT=52123
13+
14+
# -----------------------------------------------------------------------------
15+
# Sync Microservice Configuration
16+
# -----------------------------------------------------------------------------
17+
# Host and port for the sync microservice
18+
SYNC_MICROSERVICE_HOST=localhost
19+
SYNC_MICROSERVICE_PORT=52124
20+
21+
# Optional: Override the full URLs (if not set, constructed from host:port)
22+
# PRIMARY_BACKEND_URL=http://localhost:52123
23+
# SYNC_MICROSERVICE_URL=http://localhost:52124
24+
25+
# -----------------------------------------------------------------------------
26+
# Frontend Configuration (Vite)
27+
# -----------------------------------------------------------------------------
28+
# These are used by the frontend during build/development
29+
VITE_BACKEND_URL=http://localhost:52123
30+
VITE_SYNC_MICROSERVICE_URL=http://localhost:52124
31+
32+
# -----------------------------------------------------------------------------
33+
# Signing Keys (for Tauri updater)
34+
# -----------------------------------------------------------------------------
135
private = dW50cnVzdGVkIGNvbW1lbnQ6IHJzaWduIGVuY3J5cHRlZCBzZWNyZXQga2V5ClJXUlRZMEl5TjRQaUpFRXRad0dDbkNpTHlVNFBOWmZCaDRRWGNDcDdHQ3c5a0hRSUE3NEFBQkFBQUFBQUFBQUFBQUlBQUFBQVhoT1pUY2xvak01d0ZKeVVYdlVnQnlGQXlkUFhuRkdBMEY0QmdYWCtXUCtvaDMvS2tNYnJHSytXcGluQytFQWxPYm1UbVZNdU85aU53aEdJT1IveE82LzB2K2swS0MyZlFybmt5SG51aEt3YlVmMWdDSHBkN1llelU2L0JKT1puVGZFc3liNXplRlk9Cg==
236
public = dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXk6IDY1MDM5OUIzNDQ5MTNEOTMKUldTVFBaRkVzNWtEWlRxWWJCeXhtMTV6ZXZ3OUs0SXhzQzBreVRoekl5bjdXS3hkZlZzQ3VvTkgK

backend/app/config/settings.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
from platformdirs import user_data_dir
22
import os
33

4+
# Server Configuration
5+
BACKEND_HOST = os.getenv('BACKEND_HOST', 'localhost')
6+
BACKEND_PORT = int(os.getenv('BACKEND_PORT', '52123'))
7+
8+
# Microservice Configuration
9+
SYNC_MICROSERVICE_HOST = os.getenv('SYNC_MICROSERVICE_HOST', 'localhost')
10+
SYNC_MICROSERVICE_PORT = int(os.getenv('SYNC_MICROSERVICE_PORT', '52124'))
11+
SYNC_MICROSERVICE_URL = os.getenv(
12+
'SYNC_MICROSERVICE_URL',
13+
f'http://{SYNC_MICROSERVICE_HOST}:{SYNC_MICROSERVICE_PORT}'
14+
)
15+
416
# Model Exports Path
517
MODEL_EXPORTS_PATH = "app/models/ONNX_Exports"
618

7-
# Microservice URLs
8-
SYNC_MICROSERVICE_URL = "http://localhost:52124"
9-
1019
CONFIDENCE_PERCENT = 0.6
1120
# Object Detection Models:
1221
SMALL_OBJ_DETECTION_MODEL = f"{MODEL_EXPORTS_PATH}/YOLOv11_Small.onnx"

backend/main.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
import os
77
import json
88

9-
from app.config.settings import DATABASE_PATH, THUMBNAIL_IMAGES_PATH
9+
from app.config.settings import (
10+
DATABASE_PATH,
11+
THUMBNAIL_IMAGES_PATH,
12+
BACKEND_HOST,
13+
BACKEND_PORT,
14+
)
1015
from uvicorn import Config, Server
1116
from fastapi import FastAPI
1217
from fastapi.middleware.cors import CORSMiddleware
@@ -77,7 +82,7 @@ async def lifespan(app: FastAPI):
7782
"url": "https://www.postman.com/aossie-pictopy/pictopy/overview",
7883
},
7984
servers=[
80-
{"url": "http://localhost:52123", "description": "Local Development server"}
85+
{"url": f"http://{BACKEND_HOST}:{BACKEND_PORT}", "description": "Local Development server"}
8186
],
8287
)
8388

@@ -148,8 +153,8 @@ async def root():
148153

149154
config = Config(
150155
app=app,
151-
host="localhost",
152-
port=52123,
156+
host=BACKEND_HOST,
157+
port=BACKEND_PORT,
153158
log_level="info",
154159
log_config=None, # This is crucial - disable Uvicorn's default logging config
155160
)

frontend/src/config/Backend.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
export const BACKEND_URL = 'http://localhost:52123';
2-
export const SYNC_MICROSERVICE_URL = 'http://localhost:52124';
1+
export const BACKEND_URL =
2+
import.meta.env.VITE_BACKEND_URL || 'http://localhost:52123';
3+
export const SYNC_MICROSERVICE_URL =
4+
import.meta.env.VITE_SYNC_MICROSERVICE_URL || 'http://localhost:52124';

sync-microservice/app/config/settings.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
from platformdirs import user_data_dir
22
import os
33

4+
# Backend Configuration
5+
BACKEND_HOST = os.getenv('BACKEND_HOST', 'localhost')
6+
BACKEND_PORT = int(os.getenv('BACKEND_PORT', '52123'))
7+
PRIMARY_BACKEND_URL = os.getenv(
8+
'PRIMARY_BACKEND_URL',
9+
f'http://{BACKEND_HOST}:{BACKEND_PORT}'
10+
)
11+
12+
# Sync Microservice Configuration
13+
SYNC_MICROSERVICE_HOST = os.getenv('SYNC_MICROSERVICE_HOST', 'localhost')
14+
SYNC_MICROSERVICE_PORT = int(os.getenv('SYNC_MICROSERVICE_PORT', '52124'))
15+
SYNC_MICROSERVICE_URL = os.getenv(
16+
'SYNC_MICROSERVICE_URL',
17+
f'http://{SYNC_MICROSERVICE_HOST}:{SYNC_MICROSERVICE_PORT}'
18+
)
19+
420
# Model Exports Path
521
MODEL_EXPORTS_PATH = "app/models/ONNX_Exports"
6-
PRIMARY_BACKEND_URL = "http://localhost:52123"
7-
SYNC_MICROSERVICE_URL = "http://localhost:52124"
822

923
# Object Detection Models:
1024
SMALL_OBJ_DETECTION_MODEL = f"{MODEL_EXPORTS_PATH}/YOLOv11_Small.onnx"

sync-microservice/main.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import logging
22
import os
3-
from app.config.settings import DATABASE_PATH
3+
from app.config.settings import (
4+
DATABASE_PATH,
5+
SYNC_MICROSERVICE_HOST,
6+
SYNC_MICROSERVICE_PORT,
7+
)
48
from fastapi import FastAPI
59
from uvicorn import Config, Server
610
from app.core.lifespan import lifespan
@@ -51,8 +55,8 @@
5155

5256
config = Config(
5357
app=app,
54-
host="localhost",
55-
port=52124,
58+
host=SYNC_MICROSERVICE_HOST,
59+
port=SYNC_MICROSERVICE_PORT,
5660
log_level="info",
5761
log_config=None, # Disable uvicorn's default logging config
5862
)

0 commit comments

Comments
 (0)