Skip to content

Commit 2f6e48b

Browse files
committed
feat(deploy): Implement automated HTTPS and fix critical bugs
This commit introduces a fully automated production startup script and fixes several critical bugs: - Creates 'start_production.sh' to orchestrate Caddy (for HTTPS) and Waitress with a single command. Camera access over the network is now functional. - Implements robust, pre-boot IP detection to correctly configure ALLOWED_HOSTS. - Fixes a NameError crash on the Team Management page. - Corrects the signup view redirect and template inheritance to resolve multiple crashes and styling issues.
1 parent 8a0e998 commit 2f6e48b

File tree

6 files changed

+88
-52
lines changed

6 files changed

+88
-52
lines changed

Caddyfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Caddyfile for Sherlock (auto-generated)
2+
3+
192.168.31.243:8443, Chirags-Air-4.lan:8443, localhost:8443, 127.0.0.1:8443 {
4+
# 'tls internal' goes INSIDE the site block to apply a self-signed certificate.
5+
tls internal
6+
7+
# Forward all traffic to our Waitress server.
8+
reverse_proxy 127.0.0.1:8000
9+
}

inventory/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def signup(request):
4040
if form.is_valid():
4141
user = form.save()
4242
login(request, user)
43-
return redirect('inventory:section_list')
43+
return redirect('inventory:dashboard')
4444
else:
4545
form = UserCreationForm()
4646
return render(request, 'registration/signup.html', {'form': form})

run.py

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,18 @@
11
# sherlock-python/run.py
2-
32
"""
4-
Production Server Entry Point for Sherlock.
5-
6-
This script is the main entry point when running the application from a
7-
packaged executable (via PyInstaller). It performs two key functions:
8-
9-
1. Checks if the database exists on first run and, if not,
10-
automatically runs the initial Django migrations to create it.
11-
2. Starts the production-grade Waitress WSGI server to serve the
12-
Sherlock application.
3+
This script's ONLY job is to start the production Waitress server.
4+
All configuration and setup is handled by the start_production.sh script.
135
"""
14-
156
import os
16-
import sys
17-
import socket
187
from waitress import serve
8+
from sherlock.wsgi import application
199

2010

21-
def discover_and_set_host_ip():
22-
"""Discovers the primary network IP and sets it as an environment variable."""
23-
detected_ip = '127.0.0.1'
24-
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
25-
s.settimeout(0)
26-
try:
27-
s.connect(('10.254.254.254', 1))
28-
detected_ip = s.getsockname()[0]
29-
print(f"--- Automatically detected server IP: {detected_ip} ---")
30-
except Exception:
31-
print("--- Warning: Could not auto-detect network IP. Defaulting to localhost. ---")
32-
finally:
33-
s.close()
34-
os.environ['SHERLOCK_ALLOWED_IP'] = detected_ip
35-
return detected_ip
36-
37-
def run_migrations():
38-
"""Checks if the database exists and runs migrations if it doesn't."""
39-
from django.core.management import execute_from_command_line
40-
if not os.path.exists(DB_FILE):
41-
print("--- Database not found. Running initial setup... ---")
42-
args = [sys.argv[0], 'migrate']
43-
execute_from_command_line(args)
44-
print("--- Database created successfully. ---")
45-
else:
46-
print("--- Database found. ---")
11+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sherlock.settings')
4712

48-
if __name__ == "__main__":
49-
server_ip = discover_and_set_host_ip()
50-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sherlock.settings')
5113

52-
DB_FILE = 'db.sqlite3'
53-
run_migrations()
14+
HOST = '127.0.0.1'
15+
PORT = 8000
5416

55-
from sherlock.wsgi import application
5617

57-
HOST = '0.0.0.0'
58-
PORT = 8000
59-
print("--- Starting Sherlock Production Server ---")
60-
print(f"Your application should be available at: http://{server_ip}:{PORT}")
61-
print("Press Ctrl+C to stop the server.")
62-
serve(application, host=HOST, port=PORT)
18+
serve(application, host=HOST, port=PORT)

sherlock/settings.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,11 @@
185185

186186
SESSION_COOKIE_AGE = 1800
187187

188+
if not DEBUG:
189+
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'httpss')
190+
SESSION_COOKIE_SECURE = True
191+
CSRF_COOKIE_SECURE = True
192+
193+
CSRF_TRUSTED_ORIGINS = [f'https://{host}:8443' for host in ALLOWED_HOSTS]
194+
195+
CSRF_TRUSTED_ORIGINS.extend(['https://localhost:8443', 'https://127.0.0.1:8443'])

start_development.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
echo "--- Starting Sherlock in DEVELOPMENT mode (HTTP) ---"
3+
echo "Access at: http://127.0.0.1:8000"
4+
python manage.py runserver

start_production.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
3+
4+
set -e
5+
6+
echo "--- Starting Sherlock in PRODUCTION mode (HTTPS) ---"
7+
8+
9+
IP_ADDRESS=$(python -c "import socket;s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM);s.settimeout(0);s.connect(('10.254.254.254', 1));print(s.getsockname()[0]);s.close()")
10+
HOSTNAME=$(hostname)
11+
export SHERLOCK_ALLOWED_IP=$IP_ADDRESS
12+
13+
echo "--- Detected IP: $IP_ADDRESS ---"
14+
echo "--- Detected Hostname: $HOSTNAME ---"
15+
16+
17+
echo "--- Generating Caddyfile... ---"
18+
19+
cat > Caddyfile <<- EOM
20+
# Caddyfile for Sherlock (auto-generated)
21+
22+
$IP_ADDRESS:8443, $HOSTNAME:8443, localhost:8443, 127.0.0.1:8443 {
23+
# 'tls internal' goes INSIDE the site block to apply a self-signed certificate.
24+
tls internal
25+
26+
# Forward all traffic to our Waitress server.
27+
reverse_proxy 127.0.0.1:8000
28+
}
29+
EOM
30+
31+
32+
echo "--- Running database migrations... ---"
33+
python manage.py migrate --noinput
34+
echo "--- Collecting static files... ---"
35+
python manage.py collectstatic --noinput
36+
37+
38+
cleanup() {
39+
echo -e "\n--- Shutting down Caddy server... ---"
40+
caddy stop
41+
exit
42+
}
43+
trap cleanup SIGINT SIGTERM
44+
45+
echo "--- Starting Caddy in the background... ---"
46+
caddy start
47+
48+
echo "--- Starting Sherlock application server (Waitress)... ---"
49+
echo
50+
echo "====================================================================="
51+
echo " Sherlock is now running securely!"
52+
echo " Access it from any device at: https://$IP_ADDRESS:8443"
53+
echo " Or on this machine at: https://localhost:8443"
54+
echo "====================================================================="
55+
echo
56+
echo "Waitress is running in the foreground. Press Ctrl+C to stop everything."
57+
58+
59+
python run.py

0 commit comments

Comments
 (0)