host_manager

This commit is contained in:
Zsolt Ero
2024-01-03 20:48:03 +01:00
parent 3654d523d2
commit 9059c27707
7 changed files with 59 additions and 42 deletions

View File

@@ -2,7 +2,7 @@ import subprocess
import sys
from pathlib import Path
from http_host_lib import DEFAULT_RUNS_DIR, MNT_DIR, TEMPLATES_DIR
from http_host_lib import DEFAULT_RUNS_DIR, MNT_DIR, OFM_CONFIG_DIR, TEMPLATES_DIR
def write_nginx_config():
@@ -15,9 +15,7 @@ def write_nginx_config():
for subdir in MNT_DIR.iterdir():
if not subdir.is_dir():
continue
area, version = subdir.name.split('-')
location_str += create_version_location(area, version, subdir)
if not curl_text:
@@ -27,8 +25,7 @@ def write_nginx_config():
f'curl -I https://tiles.openfreemap.org/{area}/{version}/14/8529/5975.pbf'
)
for area in ['monaco', 'planet']:
location_str += create_latest_location(area)
location_str += create_latest_locations()
nginx_template = nginx_template.replace('___LOCATION_BLOCKS___', location_str)
@@ -71,7 +68,43 @@ def create_version_location(area: str, version: str, subdir: Path) -> str:
# TODO # target 10y
return f"""
location /{area}/{version} {{ # no trailing hash
location /{area}/{version} {{ # no trailing hash
alias {tilejson_path}; # no trailing hash
default_type application/json;
add_header 'Access-Control-Allow-Origin' '*' always;
add_header Cache-Control public;
expires 1d;
}}
location /{area}/{version}/ {{ # trailing hash
alias {subdir}/tiles/; # trailing hash
try_files $uri @empty;
add_header Content-Encoding gzip;
add_header 'Access-Control-Allow-Origin' '*' always;
add_header Cache-Control public;
expires 1d; # target 10y
}}
"""
def create_latest_locations() -> str:
location_str = ''
local_version_files = OFM_CONFIG_DIR.glob('tileset_version_*.txt')
for file in local_version_files:
area = file.stem.split('_')[-1]
with open(file) as fp:
version = fp.read().strip()
print(f' setting latest version for {area}: {version}')
run_dir = DEFAULT_RUNS_DIR / area / version
tilejson_path = run_dir / 'tilejson-tiles-org.json'
assert tilejson_path.exists()
location_str += f"""
location /{area} {{ # no trailing hash
alias {tilejson_path}; # no trailing hash
default_type application/json;
@@ -79,26 +112,6 @@ def create_version_location(area: str, version: str, subdir: Path) -> str:
add_header Cache-Control public;
expires 1d;
}}
"""
location /{area}/{version}/ {{ # trailing hash
alias {subdir}/tiles/; # trailing hash
try_files $uri @empty;
add_header Content-Encoding gzip;
add_header 'Access-Control-Allow-Origin' '*' always;
add_header Cache-Control public;
expires 1d; # target 10y
}}
"""
def create_latest_location(area: str) -> str:
local_version_file = Path(f'/data/ofm/config/deployed_tiles_{area}.txt')
if not local_version_file.exists():
return ''
with open(local_version_file) as fp:
version_str = fp.read().strip()
print(version_str)
return location_str