Skip to content

Commit bf0325d

Browse files
committed
2 parents 4141a97 + 08ddc31 commit bf0325d

5 files changed

Lines changed: 137 additions & 25 deletions

File tree

.github/workflows/CD.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ name: Docker Image CD
22

33
on:
44
push:
5-
branches: [ master, next ]
65

76
jobs:
87
docker-build-squash-push:
98
uses: Geode-solutions/actions/.github/workflows/docker-build-squash-push.yml@master
109
with:
11-
image_name: 'opengeodeweb-router'
1210
tag: ${{ github.ref_name }}
13-
secrets:
14-
TOKEN: ${{secrets.GITHUB_TOKEN}}
15-
11+
secrets: inherit

Dockerfile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
FROM nginx:alpine
2+
3+
RUN apk add python3 py3-pip supervisor
4+
RUN pip3 install --break-system-packages google-cloud-run
5+
26
COPY nginx.conf /etc/nginx/nginx.conf
37

4-
RUN \
5-
apk add openssl && \
6-
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/nginx.key -out /etc/nginx/nginx.crt -subj "/C=FR/ST=France/L=Pau/O=Geode-solutions"
8+
COPY supervisord.conf /etc/supervisord.conf
9+
RUN mkdir -p /var/log/supervisor
710

11+
COPY cleanup_watcher.py /usr/local/bin/cleanup_watcher.py
12+
RUN chmod +x /usr/local/bin/cleanup_watcher.py
813

9-
EXPOSE 443
14+
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]

cleanup_watcher.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
import time
3+
import requests
4+
from google.cloud import run_v2
5+
6+
7+
PARENT = os.getenv("PARENT")
8+
SERVICE_NAME = os.getenv("K_SERVICE")
9+
10+
11+
def delete_service():
12+
print("Deleting service")
13+
client = run_v2.ServicesClient()
14+
name = f"{PARENT}/services/{SERVICE_NAME}"
15+
try:
16+
print(f"Flask appears down → Deleting service {name}")
17+
client.delete_service(name=name)
18+
except Exception as e:
19+
print(f"Delete failed: {e}")
20+
21+
22+
while True:
23+
time.sleep(30)
24+
try:
25+
response = requests.get("http://127.0.0.1/geode/health", timeout=5)
26+
print("response", response, flush=True)
27+
if response.status_code != 200:
28+
raise Exception("Bad status")
29+
except Exception:
30+
delete_service()
31+
break

nginx.conf

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,47 @@ events {
33
}
44

55
http {
6-
# Nginx will handle gzip compression of responses from the app server
6+
# Map only allows your geode-solutions.com domains (including next.vease.geode-solutions.com)
7+
map $http_origin $allow_origin {
8+
~^https://(.*\.)?geode-solutions\.com$ $http_origin;
9+
default "";
10+
}
11+
712
gzip on;
813
gzip_proxied any;
914
gzip_types text/plain application/json;
1015
gzip_min_length 1000;
1116

1217
server {
13-
listen 443 ssl;
18+
listen 80;
1419
server_name localhost;
15-
16-
ssl_certificate nginx.crt;
17-
ssl_certificate_key nginx.key;
18-
1920
client_max_body_size 0;
2021

21-
location ~ "^/[a-z0-9]{32}/geode/" {
22-
if ($request_method !~ ^(DELETE|GET|POST|PUT|OPTIONS)$) {
23-
return 405;
22+
# ====================== /geode/ location ======================
23+
location ~ "^/geode/" {
24+
# Preflight OPTIONS - handled by nginx (fast, no hit to Flask)
25+
if ($request_method = 'OPTIONS') {
26+
add_header 'Access-Control-Allow-Origin' $allow_origin always;
27+
add_header 'Access-Control-Allow-Credentials' 'true' always;
28+
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
29+
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-CSRF-Token' always;
30+
add_header 'Access-Control-Max-Age' 1728000 always; # 20 days
31+
add_header 'Content-Type' 'text/plain; charset=utf-8';
32+
add_header 'Content-Length' 0;
33+
return 204;
2434
}
25-
rewrite "^/[a-z0-9]{32}/geode/(.*)" /$1 break;
35+
36+
# Normal requests
37+
limit_except DELETE GET POST PUT OPTIONS { deny all; }
38+
39+
add_header 'Access-Control-Allow-Origin' $allow_origin always;
40+
add_header 'Access-Control-Allow-Credentials' 'true' always;
41+
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
42+
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-CSRF-Token' always;
43+
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
44+
add_header 'Vary' 'Origin' always;
45+
46+
rewrite "^/geode/(.*)" /$1 break;
2647
proxy_pass http://localhost:5000;
2748
proxy_http_version 1.1;
2849
proxy_set_header Host $host;
@@ -31,20 +52,56 @@ http {
3152
proxy_set_header X-Forwarded-Proto $scheme;
3253
}
3354

34-
location ~ "^/[a-z0-9]{32}/viewer/" {
35-
if ($request_method !~ ^(GET|POST|OPTIONS)$) {
36-
return 405;
55+
# ====================== /viewer/ location ======================
56+
location ~ "^/viewer/" {
57+
if ($request_method = 'OPTIONS') {
58+
add_header 'Access-Control-Allow-Origin' $allow_origin always;
59+
add_header 'Access-Control-Allow-Credentials' 'true' always;
60+
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
61+
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-CSRF-Token' always;
62+
add_header 'Access-Control-Max-Age' 1728000 always;
63+
add_header 'Content-Type' 'text/plain; charset=utf-8';
64+
add_header 'Content-Length' 0;
65+
return 204;
3766
}
38-
rewrite "^/[a-z0-9]{32}/viewer/(.*)" /$1 break;
67+
68+
limit_except GET POST OPTIONS { deny all; }
69+
70+
add_header 'Access-Control-Allow-Origin' $allow_origin always;
71+
add_header 'Access-Control-Allow-Credentials' 'true' always;
72+
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
73+
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-CSRF-Token' always;
74+
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
75+
add_header 'Vary' 'Origin' always;
76+
77+
rewrite "^/viewer/(.*)" /$1 break;
3978
proxy_pass http://localhost:1234;
4079
proxy_http_version 1.1;
4180
proxy_set_header Host $host;
4281
proxy_set_header X-Real-IP $remote_addr;
4382
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
4483
proxy_set_header X-Forwarded-Proto $scheme;
45-
4684
proxy_set_header Connection "keep-alive, Upgrade";
4785
proxy_set_header Upgrade websocket;
4886
}
87+
88+
# Catch-all for anything else (optional, returns proper CORS even on 404)
89+
location / {
90+
if ($request_method = 'OPTIONS') {
91+
add_header 'Access-Control-Allow-Origin' $allow_origin always;
92+
add_header 'Access-Control-Allow-Credentials' 'true' always;
93+
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
94+
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-CSRF-Token' always;
95+
add_header 'Access-Control-Max-Age' 1728000 always;
96+
add_header 'Content-Type' 'text/plain; charset=utf-8';
97+
add_header 'Content-Length' 0;
98+
return 204;
99+
}
100+
101+
add_header 'Access-Control-Allow-Origin' $allow_origin always;
102+
add_header 'Access-Control-Allow-Credentials' 'true' always;
103+
add_header 'Vary' 'Origin' always;
104+
return 404;
105+
}
49106
}
50-
}
107+
}

supervisord.conf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[supervisord]
2+
nodaemon=true
3+
logfile=/dev/stdout
4+
logfile_maxbytes=0
5+
pidfile=/var/run/supervisord.pid
6+
7+
[program:nginx]
8+
command=nginx -g 'daemon off;'
9+
autostart=true
10+
autorestart=true
11+
stdout_logfile=/dev/stdout
12+
stdout_logfile_maxbytes=0
13+
stderr_logfile=/dev/stderr
14+
stderr_logfile_maxbytes=0
15+
16+
[program:cleanup-watcher]
17+
command=python3 /usr/local/bin/cleanup_watcher.py
18+
autostart=true
19+
autorestart=true
20+
stdout_logfile=/dev/stdout
21+
stdout_logfile_maxbytes=0
22+
stderr_logfile=/dev/stderr
23+
stderr_logfile_maxbytes=0

0 commit comments

Comments
 (0)