Skip to content

Commit 0c3df11

Browse files
committedDec 24, 2024·
add nginx conf in code, setup gh actions to deploy nginx conf
1 parent f20f6a4 commit 0c3df11

File tree

5 files changed

+63
-10
lines changed

5 files changed

+63
-10
lines changed
 

Diff for: ‎.github/workflows/deploy.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,7 @@ jobs:
2929
cd ~/gitdiagram
3030
git fetch origin main
3131
git reset --hard origin/main # Force local to match remote main
32-
chmod +x ./backend/deploy.sh # Make the script executable
32+
sudo chmod +x ./backend/nginx/setup_nginx.sh
33+
sudo ./backend/nginx/setup_nginx.sh
34+
chmod +x ./backend/deploy.sh
3335
./backend/deploy.sh

Diff for: ‎backend/app/main.py

-8
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
from api_analytics.fastapi import Analytics
1010
import os
1111

12-
from time import sleep
13-
1412

1513
app = FastAPI()
1614
app.state.limiter = limiter
@@ -43,9 +41,3 @@
4341
@limiter.limit("100/day")
4442
async def root(request: Request):
4543
return {"message": "Hello from GitDiagram API!"}
46-
47-
48-
@app.get("/slow")
49-
async def slow(request: Request):
50-
sleep(120)
51-
return {"message": "slow"}

Diff for: ‎backend/entrypoint.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ if [ "$ENVIRONMENT" = "development" ]; then
77
exec uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
88
elif [ "$ENVIRONMENT" = "production" ]; then
99
echo "Starting in production mode with multiple workers..."
10-
exec uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 2
10+
exec uvicorn app.main:app --host 0.0.0.0 --port 8000 --timeout-keep-alive 300 --workers 2
1111
else
1212
echo "ENVIRONMENT must be set to either 'development' or 'production'"
1313
exit 1

Diff for: ‎backend/nginx/api.conf

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
server {
2+
server_name api.gitdiagram.com;
3+
4+
location / {
5+
proxy_pass http://127.0.0.1:8000; # Forward to FastAPI
6+
include proxy_params;
7+
proxy_redirect off;
8+
}
9+
10+
# Add timeout settings
11+
proxy_connect_timeout 300;
12+
proxy_send_timeout 300;
13+
proxy_read_timeout 300;
14+
send_timeout 300;
15+
16+
# todo: stop weird requests from being made
17+
18+
listen 443 ssl; # managed by Certbot
19+
ssl_certificate /etc/letsencrypt/live/api.gitdiagram.com/fullchain.pem; # managed by Certbot
20+
ssl_certificate_key /etc/letsencrypt/live/api.gitdiagram.com/privkey.pem; # managed by Certbot
21+
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
22+
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
23+
24+
}
25+
server {
26+
if ($host = api.gitdiagram.com) {
27+
return 301 https://$host$request_uri;
28+
} # managed by Certbot
29+
30+
31+
listen 80;
32+
server_name api.gitdiagram.com;
33+
return 404; # managed by Certbot
34+
}

Diff for: ‎backend/nginx/setup_nginx.sh

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
# Exit on any error
4+
set -e
5+
6+
# Check if running as root
7+
if [ "$EUID" -ne 0 ]; then
8+
echo "Please run as root or with sudo"
9+
exit 1
10+
fi
11+
12+
# Copy Nginx configuration
13+
echo "Copying Nginx configuration..."
14+
cp ./api.conf /etc/nginx/sites-available/api
15+
ln -sf /etc/nginx/sites-available/api /etc/nginx/sites-enabled/
16+
17+
# Test Nginx configuration
18+
echo "Testing Nginx configuration..."
19+
nginx -t
20+
21+
# Reload Nginx
22+
echo "Reloading Nginx..."
23+
systemctl reload nginx
24+
25+
echo "Nginx configuration updated successfully!"

0 commit comments

Comments
 (0)
Please sign in to comment.