diff --git a/README.md b/README.md index 4df60b0..3b48223 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,7 @@ Hi! I'm Sean, the creator of AdventureLog. I'm a college student and software de - WorldTravel Dataset [dr5hn/countries-states-cities-database](https://github.com/dr5hn/countries-states-cities-database) ### Top Supporters 💖 + - [Veymax](https://x.com/veymax) - [nebriv](https://github.com/nebriv) - [Victor Butler](https://x.com/victor_butler) diff --git a/cdn/.gitignore b/cdn/.gitignore new file mode 100644 index 0000000..adbb97d --- /dev/null +++ b/cdn/.gitignore @@ -0,0 +1 @@ +data/ \ No newline at end of file diff --git a/cdn/Dockerfile b/cdn/Dockerfile new file mode 100644 index 0000000..5d23f50 --- /dev/null +++ b/cdn/Dockerfile @@ -0,0 +1,33 @@ +# Use an official Python image as a base +FROM python:3.11-slim + +# Set the working directory +WORKDIR /app + +# Install required Python packages +RUN pip install --no-cache-dir requests osm2geojson + +# Copy the script and data folder into the container +COPY main.py /app/main.py +COPY data /app/data/ + +# Ensure the data folder exists +RUN mkdir -p /app/data + +# Run the script to generate GeoJSON files +RUN python /app/main.py + +# Install Nginx +RUN apt update && apt install -y nginx && rm -rf /var/lib/apt/lists/* + +# Copy the entire data folder to the Nginx serving directory +RUN mkdir -p /var/www/html/data && cp -r /app/data/* /var/www/html/data/ + +# Copy Nginx configuration +COPY nginx.conf /etc/nginx/nginx.conf + +# Expose port 80 for Nginx +EXPOSE 80 + +# Start Nginx in the foreground +CMD ["nginx", "-g", "daemon off;"] diff --git a/cdn/README.md b/cdn/README.md new file mode 100644 index 0000000..f935c14 --- /dev/null +++ b/cdn/README.md @@ -0,0 +1,3 @@ +This folder contains the scripts to generate AdventureLOG CDN files. + +Special thanks to [@larsl-net](https://github.com/larsl-net) for the GeoJSON generation script. diff --git a/cdn/docker-compose.yml b/cdn/docker-compose.yml new file mode 100644 index 0000000..89e7393 --- /dev/null +++ b/cdn/docker-compose.yml @@ -0,0 +1,9 @@ +services: + geojson: + build: . + container_name: adventurelog-cdn + ports: + - "8080:80" + restart: unless-stopped + volumes: + - ./data:/app/data # Ensures new data files persist diff --git a/cdn/main.py b/cdn/main.py new file mode 100644 index 0000000..36df16c --- /dev/null +++ b/cdn/main.py @@ -0,0 +1,56 @@ +import requests +import json +import os + +# https://github.com/dr5hn/countries-states-cities-database/tags +COUNTRY_REGION_JSON_VERSION = 'v2.5' # Should match the version stated in the settings.py file of AdventureLog + +def downloadCountriesStateCities(): + res = requests.get(f'https://raw.githubusercontent.com/dr5hn/countries-states-cities-database/{COUNTRY_REGION_JSON_VERSION}/json/countries%2Bstates%2Bcities.json') + +def downloadGeojson(iso_code=None, region_name=None): + """ + Download geojson data for a specific region using the Overpass API. + :param iso_code: ISO 3166-2 code for the region (e.g. "US-CT") + :param region_name: Name of the region (e.g. "Connecticut") + :return: Geojson data for the region + """ + base_url = "https://overpass-api.de/api/interpreter" + + # Get the directory where the script is located + script_dir = os.path.dirname(__file__) + + # Ensure the ./data directory exists + data_dir = os.path.join(script_dir, "data") + os.makedirs(data_dir, exist_ok=True) + + # Set the path for the geojson file + geojson_path = os.path.join(data_dir, "geojson.json") + + if iso_code: + query = f'[out:json];relation["boundary"="administrative"]["admin_level"="4"]["ISO3166-2"="{iso_code}"];out body;way(r);(._;>;);out geom;' + response = requests.post(base_url, data=query) + if response.ok and response.json().get("elements"): + data = response.json() + with open(geojson_path, "w", encoding="utf-8") as f: + json.dump(data, f, indent=4) + return data + + if region_name: + query = f'[out:json];relation["boundary"="administrative"]["admin_level"="4"]["name"="{region_name}"];out body;way(r);(._;>;);out geom;' + response = requests.post(base_url, data=query) + if response.ok and response.json().get("elements"): + data = response.json() + with open(geojson_path, "w", encoding="utf-8") as f: + json.dump(data, f, indent=4) + return data + + return None # No results found + +# Example usage +data = downloadGeojson(iso_code="US-CT", region_name="Connecticut") + +if data: + print(f"Region data found and saved to {os.path.join(os.path.dirname(__file__), 'data', 'geojson.json')}!") +else: + print("No region data found.") diff --git a/cdn/nginx.conf b/cdn/nginx.conf new file mode 100644 index 0000000..44a0df9 --- /dev/null +++ b/cdn/nginx.conf @@ -0,0 +1,13 @@ +events {} + +http { + server { + listen 80; + server_name _; + + location /data/ { + root /var/www/html; + autoindex on; # Enable directory listing + } + } +} diff --git a/cdn/requirements.txt b/cdn/requirements.txt new file mode 100644 index 0000000..1f11fab --- /dev/null +++ b/cdn/requirements.txt @@ -0,0 +1 @@ +osm2geojson==0.2.5 \ No newline at end of file