Skip to content

Commit

Permalink
Compression of geometries experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
GISRedeDev committed Jan 19, 2025
1 parent 2451c01 commit 418b1eb
Show file tree
Hide file tree
Showing 9 changed files with 2,696 additions and 0 deletions.
14 changes: 14 additions & 0 deletions api/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from flask_limiter.util import get_remote_address
from flask_sqlalchemy import SQLAlchemy
from flask_jwt_extended import JWTManager
import json

from alembic import command
from alembic.config import Config
Expand All @@ -20,6 +21,13 @@
from app.models import User

BASE = Path(__file__).resolve().parent
SUBNATIONAL_GEOMS = BASE.parent.joinpath("scripts", "data", "simplified_l1_v2_1_13.geojson")
NATIONAL_GEOMS = BASE.parent.joinpath("scripts", "data", "simplified_l0_v2.geojson")
with open(SUBNATIONAL_GEOMS, "r", encoding='utf-8') as f:
SUBNATIONAL_GEOMS = json.load(f)

with open(NATIONAL_GEOMS, "r", encoding='utf-8') as f:
NATIONAL_GEOMS = json.load(f)


def get_nginx_ip():
Expand Down Expand Up @@ -60,6 +68,12 @@ def create_app(config_name: str) -> Flask:
app.config["ENV"] = config_name
Compress(app)
app.config["COMPRESS_ALWAYS"] = True
app.config['COMPRESS_MIMETYPES'] = ['application/json'] # Compress only JSON responses
app.config['COMPRESS_LEVEL'] = 6 # Compression level (default: 6)
app.config['COMPRESS_MIN_SIZE'] = 500
app.config["SUBNATIONAL_GEOMS"] = SUBNATIONAL_GEOMS
app.config["NATIONAL_GEOMS"] = NATIONAL_GEOMS

db.init_app(app)
bcrypt.init_app(app)
_ = JWTManager(app)
Expand Down
42 changes: 42 additions & 0 deletions api/app/api-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,48 @@ paths:
description: "Successfully initialized"
"500":
description: "Internal server error"
/subnational_geometries:
get:
tags:
- default
summary: "Get subnational geometries"
x-swagger-router-controller: app.endpoints
operationId: get_subnational_geometries
responses:
"200":
description: "Successfully retrieved data"
content:
application/json:
schema:
type: object
properties:
data:
type: array
items:
type: object
"500":
description: "Internal server error"
/national_geometries:
get:
tags:
- default
summary: "Get national geometries"
x-swagger-router-controller: app.endpoints
operationId: get_national_geometries
responses:
"200":
description: "Successfully retrieved data"
content:
application/json:
schema:
type: object
properties:
data:
type: array
items:
type: object
"500":
description: "Internal server error"
/valid_outcomes_by_date:
get:
tags:
Expand Down
17 changes: 17 additions & 0 deletions api/app/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ def init_data() -> Response:
except Exception as e:
return make_response({"error": str(e)}, 500)


def get_subnational_geometries() -> Response:
try:
response = current_app.config["SUBNATIONAL_GEOMS"]
return make_response(response, 200)
except Exception as e:
return make_response({"error": str(e)}, 500)


def get_national_geometries() -> Response:
try:
response = current_app.config["NATIONAL_GEOMS"]
return make_response(response, 200)
except Exception as e:
return make_response({"error": str(e)}, 500)


@validate_request_params
def valid_outcomes_by_date(date: str) -> Response:
try:
Expand Down
268 changes: 268 additions & 0 deletions api/scripts/data/simplified_l0_v2.geojson

Large diffs are not rendered by default.

2,314 changes: 2,314 additions & 0 deletions api/scripts/data/simplified_l1_v2_1_13.geojson

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ services:
- type: bind
source: ./www/public_html/
target: /var/www/public_html/
- type: bind
source: ./www/nginx.conf
target: /etc/nginx/nginx.conf
ports:
- "80:80"
- "443:443"
Expand Down
18 changes: 18 additions & 0 deletions www/conf.d/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ server {
# proxy_buffers 8 512k;
# proxy_busy_buffers_size 512k;
# proxy_temp_file_write_size 512k;
location / {
try_files $uri $uri/ =404;
}

location ~ \.js$ {
types { application/javascript js; }
add_header Content-Type application/javascript always;
}

location /api/ {
proxy_pass http://flask-app/api/;
Expand All @@ -39,6 +47,16 @@ server {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept, Authorization';

proxy_set_header Accept-Encoding ""; # Allow compressed responses from Flask
proxy_set_header Host $host; # Pass the original Host header
proxy_http_version 1.1; # Use HTTP/1.1 for proper handling
}

location /subnational_geometries {
proxy_pass http://127.0.0.1:5000/geojson;
expires 30d; # Cache the response for 1 day
add_header Cache-Control "public";
}
}

Expand Down
1 change: 1 addition & 0 deletions www/mime.types
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application/javascript js;
19 changes: 19 additions & 0 deletions www/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
events {
# You can leave this block empty if you don't need specific settings
}


http {
include /etc/nginx/mime.types; # Include standard MIME types
default_type application/octet-stream;

include /etc/nginx/conf.d/*.conf; # Includes all per-site configs

gzip on; # Enable gzip compression
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_min_length 1024; # Only compress responses larger than 1KB
gzip_proxied any; # Compress responses even for proxied requests
gzip_comp_level 6; # Compression level (1=fastest, 9=best compression)
gzip_buffers 16 8k; # Buffers for gzip compression
gzip_http_version 1.1; # Use gzip for HTTP/1.1 clients
}

0 comments on commit 418b1eb

Please sign in to comment.