Skip to content

Commit

Permalink
Merge pull request #3 from jaluebbe/dev/change_static_path
Browse files Browse the repository at this point in the history
moved static path references to host static files on nginx. Created redirect placeholders to support the static version on GitHub pages.
  • Loading branch information
jaluebbe committed Jan 30, 2021
2 parents e0787ed + 297d38c commit 4ae7e07
Show file tree
Hide file tree
Showing 17 changed files with 331 additions and 279 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8-slim
RUN pip install aiofiles redis
COPY static /app/static
COPY backend_vrs_db.py /app/
Expand Down
33 changes: 8 additions & 25 deletions backend_fastapi.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os
import json
from fastapi import FastAPI, Query, HTTPException, Response
from starlette.staticfiles import StaticFiles
from starlette.responses import FileResponse
from starlette.staticfiles import StaticFiles, FileResponse
from fastapi.middleware.gzip import GZipMiddleware
from pydantic import BaseModel, confloat, conint, constr
import redis
Expand All @@ -13,10 +11,10 @@
redis_connection = redis.Redis(os.getenv('REDIS_HOST'), decode_responses=True)

app = FastAPI(
openapi_prefix='',
title='FlightMapEuropeSimple',
description=''
)
openapi_url='/api/openapi.json',
docs_url="/api/docs"
)
app.add_middleware(GZipMiddleware, minimum_size=500)


Expand All @@ -34,27 +32,9 @@ class FlightSearch(BaseModel):
filterAirlineAlliance: constr(regex='^(Star Alliance|Oneworld|SkyTeam|)$')


app.mount("/static", StaticFiles(directory="static"), name="static")


@app.get("/", include_in_schema=False)
async def root():
return FileResponse('static/flightmap_europe_simple.html')


@app.get("/flightsearch.html", include_in_schema=False)
async def flightsearch():
return FileResponse('static/flightsearch.html')


@app.get("/statistics.html", include_in_schema=False)
async def statistics():
return FileResponse('static/statistics.html')


@app.get("/test.html", include_in_schema=False)
async def testpage():
return FileResponse('static/flightmap_test.html')
return FileResponse('static/index.html')


@app.get("/api/geojson/airports")
Expand Down Expand Up @@ -113,3 +93,6 @@ def get_fir_uir_statistics():
return Response(content=json_data, media_type="application/json")
else:
raise HTTPException(status_code=404, detail="Item not found")


app.mount("/", StaticFiles(directory="static"), name="static")
31 changes: 24 additions & 7 deletions backend_vrs_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
logger = logging.getLogger(__name__)


def timeit(method):
def timed(*args, **kw):
ts = time.time()
Expand All @@ -16,11 +17,12 @@ def timed(*args, **kw):
name = kw.get('log_name', method.__name__.upper())
kw['log_time'][name] = int((te - ts) * 1000)
else:
logger.warning('%r %2.2f ms' % \
logger.warning('%r %2.2f ms' %
(method.__name__, (te - ts) * 1000))
return result
return timed


directory = "flightroutes/"

# The following lists of airline alliances including affiliate members are
Expand All @@ -43,17 +45,20 @@ def timed(*args, **kw):
'DSM', 'SUS', 'IBS', 'SHT', 'SBI', 'ALK', 'LNE', 'RJA', 'QLK', 'SKW', 'LTM',
'PDT', 'RPA', 'CAW', 'NWK', 'LAN', 'LPE', 'CPA', 'HDA']


def get_distance(lat1: float, lon1: float, lat2: float, lon2: float) -> float:
degRad = 2 * math.pi / 360
deg_rad = 2 * math.pi / 360
distance = (
6.370e6 * math.acos(math.sin(lat1 * degRad) * math.sin(lat2 * degRad)
+ math.cos(lat1 * degRad) * math.cos(lat2 * degRad)
* math.cos((lon2 - lon1) * degRad)))
6.370e6 * math.acos(math.sin(lat1 * deg_rad) * math.sin(lat2 * deg_rad)
+ math.cos(lat1 * deg_rad) * math.cos(lat2 * deg_rad)
* math.cos((lon2 - lon1) * deg_rad)))
return distance


def cos_deg(angle: float) -> float:
return math.cos(angle * math.pi / 180)


def namedtuple_factory(cursor, row):
"""
Usage:
Expand All @@ -63,10 +68,12 @@ def namedtuple_factory(cursor, row):
Row = namedtuple("Row", fields)
return Row(*row)


def regexp(expr, item):
reg = re.compile(expr)
return reg.search(item) is not None


def get_geojson_airports():
try:
connection = sqlite3.connect("file:" + directory +
Expand Down Expand Up @@ -96,6 +103,7 @@ def get_geojson_airports():
airports_data = _collection
return airports_data


def get_airport_position(airport_icao):
try:
connection = sqlite3.connect("file:" + directory +
Expand All @@ -113,6 +121,7 @@ def get_airport_position(airport_icao):
return None
return result


def get_airport_positions():
try:
connection = sqlite3.connect("file:" + directory +
Expand All @@ -136,6 +145,7 @@ def get_airport_positions():
airport_positions[row.Icao] = [row.Longitude, row.Latitude]
return airport_positions


def get_distinct_routes_by_airport(airport_icao):
try:
connection = sqlite3.connect("file:" + directory +
Expand All @@ -154,6 +164,7 @@ def get_distinct_routes_by_airport(airport_icao):
if result is not None:
return [row.DirectRoute for row in result]


def get_distinct_routes_by_airline(operator_icao):
try:
connection = sqlite3.connect("file:" + directory +
Expand All @@ -178,6 +189,7 @@ def get_distinct_routes_by_airline(operator_icao):
'operator_iata': result[0].OperatorIata,
'operator_name': result[0].OperatorName}


def get_route_by_callsign(callsign):
try:
connection = sqlite3.connect("file:" + directory +
Expand All @@ -194,6 +206,7 @@ def get_route_by_callsign(callsign):
return None
return result


def get_geojson_callsign(callsign):
_flight = get_route_by_callsign(callsign)
if _flight is None:
Expand All @@ -207,7 +220,7 @@ def get_geojson_callsign(callsign):
return '{}'
_line_coordinates.append([_info.Longitude, _info.Latitude])
_airport_infos.append({'name': _info.Name, 'icao': _icao,
'iata': _info.Iata,})
'iata': _info.Iata})
_line_string = {
"type": "LineString",
"coordinates": _line_coordinates
Expand All @@ -225,6 +238,7 @@ def get_geojson_callsign(callsign):
_collection = {"type": "FeatureCollection", "features": [_feature]}
return _collection


def get_geojson_airport(icao):
_feature_collection = {
"type": "FeatureCollection", "features": [{"type": "Feature",
Expand Down Expand Up @@ -259,6 +273,7 @@ def get_geojson_airport(icao):
"coordinates": _coordinates}}]
return _feature_collection


def get_geojson_airline(icao):
_feature_collection = {
"type": "FeatureCollection", "features": [{"type": "Feature",
Expand Down Expand Up @@ -292,6 +307,7 @@ def get_geojson_airline(icao):
"coordinates": _coordinates}}]
return _feature_collection


@timeit
def flightsearch(request_data):
stops = request_data.numberOfStops
Expand Down Expand Up @@ -427,7 +443,7 @@ def flightsearch(request_data):
cursor.execute(sql_query_stopover_destinations)
stopover_destination_icaos = [x.Origin for x in cursor.fetchall()]
stopover_origins = ','.join(map(repr, stopover_origin_icaos))
stopover_destinations = ','.join(map(repr,stopover_destination_icaos))
stopover_destinations = ','.join(map(repr, stopover_destination_icaos))
logger.debug(f'stopover destinations: {stopover_destinations}')
logger.debug(f'stopover origins: {stopover_origins}')
sql_query = f"""
Expand Down Expand Up @@ -462,6 +478,7 @@ def flightsearch(request_data):
connection.close()
return direct_routes + single_stopover_routes + double_stopover_routes


@timeit
def get_geojson_flightsearch(request_data):
_airport_positions = get_airport_positions()
Expand Down
26 changes: 20 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ services:
# ports:
# - "6379:6379"

flightmap:
flightmap_api:
image: flightmap_europe_simple:alpine
# you may switch to the image provided on Docker Hub.
# image: jaluebbe/flightmap_europe_simple:alpine
volumes:
- $PWD/../FlightMapEuropeSimple/flightroutes:/app/flightroutes
- $PWD/flightroutes:/app/flightroutes
- $PWD/datenschutz.html:/app/static/datenschutz.html:ro
labels:
- "traefik.http.routers.flightmap.rule=Host(`${FLIGHTMAP_HOST}.${MY_DOMAIN}`)"
- "traefik.http.routers.flightmap.entrypoints=websecure"
- "traefik.http.routers.flightmap.tls=true"
- "traefik.http.routers.flightmap.tls.certresolver=leresolver"
- "traefik.http.routers.flightmap-api.rule=(Host(`${FLIGHTMAP_HOST}.${MY_DOMAIN}`) && PathPrefix(`/api`))"
- "traefik.http.routers.flightmap-api.entrypoints=websecure"
- "traefik.http.routers.flightmap-api.tls=true"
- "traefik.http.routers.flightmap-api.tls.certresolver=leresolver"
environment:
- REDIS_HOST=redis
depends_on:
Expand All @@ -57,3 +57,17 @@ services:
- STATISTICS_URL=${STATISTICS_URL}
depends_on:
- redis

nginx:
image: nginx:alpine
volumes:
- $PWD/static:/usr/share/nginx/html
- $PWD/datenschutz.html:/usr/share/nginx/html/datenschutz.html:ro
labels:
- "traefik.http.routers.flightmap.rule=Host(`${FLIGHTMAP_HOST}.${MY_DOMAIN}`)"
- "traefik.http.routers.flightmap.entrypoints=websecure"
- "traefik.http.routers.flightmap.tls=true"
- "traefik.http.routers.flightmap.tls.certresolver=leresolver"
depends_on:
- flightmap_api

79 changes: 2 additions & 77 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,11 @@
<html lang="en">

<head>
<link rel="shortcut icon" type="image/x-icon" href="static/favicon.ico">
<title>flightmap europe static</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.min.css" integrity="sha384-d7pQbIswLsqVbYoAoHHlzPt+fmjkMwiXW/fvtIgK2r1u1bZXvGzL9HICUg4DKSgO" crossorigin="anonymous">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/MarkerCluster.css" integrity="sha384-lPzjPsFQL6te2x+VxmV6q1DpRxpRk0tmnl2cpwAO5y04ESyc752tnEWPKDfl1olr" crossorigin="anonymous">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/MarkerCluster.Default.css" integrity="sha384-5kMSQJ6S4Qj5i09mtMNrWpSi8iXw230pKU76xTmrpezGnNJQzj0NzXjQLLg+jE7k" crossorigin="anonymous">
<style>
html, body {
height: 100%;
margin: 0;
min-height: 100%;
}

#map {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin-bottom: 52px;
}

.navbar {
background-color: #333;
overflow: hidden;
position: fixed;
bottom: 0;
width: 100%;
}

/* Style the links inside the navigation bar */
.navbar a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}

.navbar a:hover {
background-color: #ddd;
color: black;
}

.navbar a.active {
background-color: #4CAF50;
color: white;
}
</style>
<meta http-equiv="refresh" content="0; url=https://jaluebbe.github.io/FlightMapEuropeSimple/static/index.html" />
</head>

<body>
<div id="map"></div>
<div class="navbar">
<a href="./" class="active">Flight map</a>
<a href="./statistics.html">Flight statistics</a>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.min.js" integrity="sha384-vdvDM6Rl/coCrMsKwhal4uc9MUUFNrYa+cxp+nJQHy3TvozEpVKVexz/NTbE5VSO" crossorigin="anonymous"></script>
<script src="https://unpkg.com/[email protected]/dist/leaflet.markercluster-src.js" integrity="sha384-N9K+COcUk7tr9O2uHZVp6jl7ueGhWsT+LUKUhd/VpA0svQrQMGArhY8r/u/Pkwih" crossorigin="anonymous"></script>
<script src="https://unpkg.com/@turf/[email protected]/turf.min.js" integrity="sha384-nyGVw++SNyi6OAnN+1SO/ahaCqBIecgU/GX0DxmV56dIQ0QDizRZIp+qT2TCe39E" crossorigin="anonymous"></script>
<script src="static/leaflet.rotatedMarker.js"></script>
<script nomodule src="static/polyfill_string_includes.js"></script>
<script src="static/flightmap.js"></script>
<script src="static/airports_static.js"></script>
<script src="static/airspaces_static.js"></script>
<script src="static/upper_lower_airspace_limits.js"></script>
<script src="static/aircraft_static.js"></script>
<script>
loadAirportData();
loadFirUirShapes('./static/flightmap_europe_with_north_america.json');
if (!L.Browser.mobile) {
airportMarkers.addTo(map);
refreshAircraftPositions();
}
var myInterval = window.setInterval(refreshAircraftPositions, 10000);
</script>
<p><a href="https://jaluebbe.github.io/FlightMapEuropeSimple/static/index.html">Redirect to new URL</a></p>
</body>

</html>
2 changes: 1 addition & 1 deletion static/aircraft_interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function processedActiveFeature(feature) {
return false;
}
var activePlaneIcon = L.icon({
iconUrl: 'static/aiga_air_transportation_orange.svg',
iconUrl: 'aiga_air_transportation_orange.svg',
iconSize: [16, 16]
})

Expand Down
2 changes: 1 addition & 1 deletion static/aircraft_static.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var aircraftPositions;
var downloadingPositions = false;

var planeIcon = L.icon({
iconUrl: 'static/aiga_air_transportation.svg',
iconUrl: 'aiga_air_transportation.svg',
iconSize: [16, 16]
})
function processedActiveFeature(feature) {
Expand Down
2 changes: 1 addition & 1 deletion static/airports_static.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ layerControl.addOverlay(airportMarkers,
"<span style='background-color:rgba(213, 0, 0, 0.2)'>Airports</span>");

function loadAirportData(url) {
if (!url) url = './static/airports_static.json';
if (!url) url = './airports_static.json';
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.setRequestHeader('Content-Type', 'application/json');
Expand Down
2 changes: 1 addition & 1 deletion static/airspaces_static.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function refreshAirspaces() {
}

function loadFirUirShapes(shapefile) {
if (!shapefile) shapefile = './static/flightmap_europe_fir_uir.json';
if (!shapefile) shapefile = './flightmap_europe_fir_uir.json';
var xhr = new XMLHttpRequest();
xhr.open('GET', shapefile);
xhr.setRequestHeader('Content-Type', 'application/json');
Expand Down
Loading

0 comments on commit 4ae7e07

Please sign in to comment.