mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-28 19:06:19 -05:00
* Map tiles for ATAK and iTAK
A new service, wrolpi-tiles (tiles/), draws WROLPi's map as 256 px raster
tiles from the map's PMTiles with MapLibre Native (mapli), so ATAK and iTAK
can use the map as a map source, and ATAK can download an area of it.
- Tiles are drawn in blocks of 4x4 (ATAK asks for tiles in quadtree order),
each block once however many requests wait on it, in render processes of
their own. Phones are served in turn.
- A tile is always a 200: blank where there is no map, or when it was not
drawn before ATAK's 5 second timeout. Five failed tiles in a row stop an
ATAK download, and a 401/403 stops the map source.
- Drawn tiles are kept in /var/cache/wrolpi-tiles, with a size cap and a
free-space floor; JPEG, about a seventh of a PNG.
- The basemap is the web map's (protomaps-themes-base, light), kept equal
to it by a Jest test. Unreadable map files and missing map fonts are
left out rather than failing every tile.
- Caddy serves it at /tiles/ over HTTPS and on port 8088 over plain HTTP
(a phone must trust WROLPi's CA for HTTPS). /tiles/wrolpi.xml is the
MOBAC map source to import.
- A separate service with lower CPU priority and a memory cap, so a phone
downloading an area cannot slow the API. Docker runs it from the api
image.
- The map page has an ATAK tab: the map source links, how to use them, and
what the tile service is doing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Tiles: bound PMTiles decompression before it happens
A few bytes of a downloaded map file could decompress to far more than the
cap, which was applied only afterwards. Decompress at most MAX_METADATA_SIZE,
as modules.map.pmtiles does.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Tiles: never send a blank for a late tile; start render processes with the service
QA on a Pi 4 found the first tile after a start sent blank: starting the
render processes and the first draw took 4.5 s, past the deadline. A
downloader (ATAK's, iTAK's) keeps whatever a tile request returns, so a
blank sent for a late or failed tile is a hole in its offline map for
good. Such a tile is now a 503 (ATAK tries each tile twice; only 401 and
403 stop a map source), and the render processes start with the service.
Also:
- OOMScoreAdjust=500: Raspberry Pi OS disables the memory cgroup, so
MemoryMax does nothing there; the tile service is killed before the API.
- sanic-ext's auto extensions are off in the tile service.
- The map's ATAK tab is before Manage: Manage is the last tab.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Tiles: put backgroundColor back in the map source; iTAK requires it
iTAK refuses a MOBAC map source without <backgroundColor> ("The data couldn't
be read because it is missing"). ATAK ignores it. It is the map's own
background color.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Tiles: leave out the map files hidden on the Manage tab, as the web map does
maps.yaml's ignored_files names the map files the user hid; the web map
does not draw them, and the tiles drew them anyway.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Tiles: bound the render processes' memory and the tiles-per-second times
QA on a Pi 4, a phone taking 123,000 tiles offline: each render process
grew from about 270 MB to 370 MB (MapLibre keeps what it has read and
drawn) and did not shrink when idle. A render process is now replaced
after 2,000 blocks. The serve times behind tiles-per-second were only
forgotten when the status was asked for; they are forgotten as tiles are
served.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Map ATAK tab: iTAK takes an area offline too
An iPhone downloaded 123,000 tiles with iTAK and showed them offline with
none missing; the tab said only ATAK could.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Map ATAK tab: a button to clear the tiles the tile service keeps
POST /tiles/cache/clear on the tile service (which keeps the cache's size)
removes the kept tiles; they are drawn again when next asked for. The
button asks first, and says that maps already downloaded to phones are not
changed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Tiles: measure the cache as the disk does; clear it at once, delete in the background
QA on a Pi 4 after a 123,000-tile download: the cache counted 375 MB of tile
bytes while its files took 660 MB of the SD card (each small file takes a
whole 4 KiB block), so the 1 GB cap allowed about 1.7 GB. The cache is now
measured in disk blocks.
Clearing deleted 128,000 files in the request: about 40 seconds, with the
Pi's load warning on. A clear (or a new style) now renames the tiles out of
the way at once and deletes them in a lowest-priority thread, pausing every
500 files; a deletion cut short is finished at the next start. A tile drawn
again no longer counts twice.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Help: ATAK and iTAK
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Tiles: a page on port 8088 saying what it is
A person who finds port 8088 got a 404. / (and /tiles/) is now a short page:
WROLPi's map tile service for ATAK and iTAK, the map source to open on a
phone, and links to the WROLPi app and the map's ATAK tab, made from the
address the page was reached by (Docker publishes the app on
TILES_HTTPS_PORT). Port 8088 serves WROLPi's icon.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Tiles: review fixes
- Caddy: a tile request is never redirected to the fallback page when the
tile service is down or restarting (handle_errors on 443). ATAK keeps
whatever a tile request ends with, so the page was stored as the tile;
it is now a 503 with Retry-After, which ATAK tries again.
- The cache's oldest tiles are found and deleted in a lowest-priority
thread, pausing as it goes, not in the tile request which filled it.
- ATAK tab: only the newest status request writes the status, so a slow
answer can not undo a newer one (or a clear). It says when the page was
opened on localhost and the addresses work only there, and names each
map source as the app lists it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
421 lines
19 KiB
Python
421 lines
19 KiB
Python
"""The tile service's HTTP server.
|
|
|
|
GET /, /tiles/ a page saying what this is, for a person who finds the port
|
|
GET /tiles/{z}/{x}/{y}.jpg a 256 px tile
|
|
GET /tiles/wrolpi.xml the map source for ATAK and iTAK (MOBAC XML), with this server's address in it
|
|
GET /tiles/status what the service is doing (the map's ATAK tab shows it)
|
|
POST /tiles/cache/clear remove the kept tiles (the map's ATAK tab); they are drawn again when asked for
|
|
|
|
What ATAK does with a tile server (read from its source, `TileScraper` and `CustomMobacMapSource`), and what this
|
|
must do because of it:
|
|
|
|
- A downloader keeps whatever a tile request returns, whatever the response's headers say: a blank tile sent in
|
|
place of one which was late would be a hole in the phone's map for good. So a tile not drawn in time
|
|
(`DEADLINE`, under ATAK's 5 seconds), or which failed, is a 503: ATAK tries each tile twice, and the second
|
|
try finds the block drawn. Only five tiles in a row failing both tries stop a download, and only a 401 or
|
|
403 stops a map source; this sends neither. Where there is no map, a tile is drawn of the background.
|
|
- The render processes are started with the service, not by the first tile: starting one is seconds.
|
|
- It asks for 4 tiles at a time (iTAK: 6), of the same block. A block is drawn once however many ask for it.
|
|
- Phones are served in turn, a block of one then a block of the next, so one phone's download does not keep
|
|
another's tiles waiting past the deadline.
|
|
"""
|
|
import asyncio
|
|
import collections
|
|
import html
|
|
import logging
|
|
import multiprocessing
|
|
import os
|
|
import time
|
|
from urllib.parse import urlsplit
|
|
from concurrent.futures import ProcessPoolExecutor
|
|
from concurrent.futures.process import BrokenProcessPool
|
|
from email.utils import formatdate
|
|
from pathlib import Path
|
|
from typing import Deque, Dict, Optional, Tuple
|
|
from xml.sax.saxutils import escape
|
|
|
|
from sanic import Sanic, response
|
|
|
|
from tiles import maps, render
|
|
from tiles.blocks import MAX_ZOOM, Block, block_of, is_tile
|
|
from tiles.cache import TileCache
|
|
from tiles.style import background_color, build_style, style_id
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# ATAK gives up on a tile after 5 seconds; this leaves time for the tile to reach it.
|
|
DEADLINE = 4.0
|
|
# How long a phone keeps a tile. ATAK keeps one at least a week whatever this says.
|
|
EXPIRES_SECONDS = 7 * 24 * 60 * 60
|
|
# Blocks a client may have waiting. More is a phone which gave up on them; the oldest are dropped.
|
|
MAX_QUEUED_PER_CLIENT = 64
|
|
# How often the map directory is looked at for added, replaced and removed map files.
|
|
WATCH_SECONDS = 30
|
|
# Tiles per second is counted over this long.
|
|
RATE_SECONDS = 60
|
|
# A render process is replaced after drawing this many blocks. MapLibre keeps what it has read and drawn, and
|
|
# nothing bounds it: on a Raspberry Pi 4 a process grew from about 270 MB to 370 MB over 8,000 blocks (a phone
|
|
# taking 120,000 tiles offline). Starting one is about 2 seconds, a few minutes apart at most.
|
|
BLOCKS_PER_PROCESS = 2000
|
|
DEFAULT_MAX_ZOOM = 17
|
|
NAME = 'WROLPi Map'
|
|
|
|
|
|
def mobac_xml(base_url: str, name: str, max_zoom: int, background: str = '#cccccc') -> str:
|
|
"""The MOBAC map source ATAK and iTAK import.
|
|
|
|
`backgroundColor` is required by iTAK: without it, it refuses the file ("The data couldn't be read because it
|
|
is missing"). ATAK ignores it (it reads only `#` and one digit)."""
|
|
return f'''<?xml version="1.0" encoding="UTF-8"?>
|
|
<customMapSource>
|
|
<name>{escape(name)}</name>
|
|
<minZoom>0</minZoom>
|
|
<maxZoom>{max_zoom}</maxZoom>
|
|
<tileType>jpg</tileType>
|
|
<tileUpdate>None</tileUpdate>
|
|
<url>{escape(base_url)}/tiles/{{$z}}/{{$x}}/{{$y}}.jpg</url>
|
|
<backgroundColor>{escape(background)}</backgroundColor>
|
|
</customMapSource>
|
|
'''
|
|
|
|
|
|
class TileService:
|
|
|
|
def __init__(self, map_directory: Path, cache: TileCache, processes: int = 2, max_zoom: int = DEFAULT_MAX_ZOOM,
|
|
overview: Optional[Path] = maps.OVERVIEW):
|
|
self.map_directory = map_directory
|
|
self.cache = cache
|
|
self.processes = processes
|
|
self.max_zoom = max_zoom
|
|
self.overview = overview
|
|
self.maps = []
|
|
self.signature = None
|
|
self.style: Optional[dict] = None
|
|
self.style_id: Optional[str] = None
|
|
self.blank_tile = b''
|
|
self.executor: Optional[ProcessPoolExecutor] = None
|
|
self.inflight: Dict[Tuple[str, Block], asyncio.Future] = dict()
|
|
self.queues: 'collections.OrderedDict[str, Deque[Tuple[str, Block]]]' = collections.OrderedDict()
|
|
self.condition: Optional[asyncio.Condition] = None
|
|
self.tasks = []
|
|
self.rendering = 0
|
|
self.stats = collections.Counter()
|
|
self.clients: Dict[str, dict] = dict()
|
|
self.served = collections.deque() # times tiles were served, for tiles per second
|
|
|
|
# Map files and style.
|
|
|
|
def refresh_maps(self) -> bool:
|
|
"""Draw with the map files now in the map directory. Blocking. True when they changed."""
|
|
found = maps.find_maps(self.map_directory, self.overview)
|
|
signature = maps.signature(found)
|
|
if signature == self.signature:
|
|
return False
|
|
self.maps, self.signature = found, signature
|
|
self.style = build_style(found)
|
|
self.style_id = style_id(self.style, signature)
|
|
self.blank_tile = render.blank(background_color(self.style))
|
|
self.cache.use_style(self.style_id)
|
|
logger.info(f'Drawing tiles of {len(found)} map files (style {self.style_id})')
|
|
return True
|
|
|
|
async def _watch(self):
|
|
while True:
|
|
await asyncio.sleep(WATCH_SECONDS)
|
|
try:
|
|
await asyncio.to_thread(self.refresh_maps)
|
|
except Exception as e:
|
|
logger.error('Failed to look for map files', exc_info=e)
|
|
|
|
# Starting and stopping.
|
|
|
|
def _new_executor(self) -> ProcessPoolExecutor:
|
|
# spawn, not fork: a fork of this process would copy its event loop and threads.
|
|
return ProcessPoolExecutor(max_workers=self.processes, mp_context=multiprocessing.get_context('spawn'),
|
|
initializer=render.init_process, max_tasks_per_child=BLOCKS_PER_PROCESS)
|
|
|
|
async def start(self):
|
|
self.condition = asyncio.Condition()
|
|
await asyncio.to_thread(self.refresh_maps)
|
|
self.executor = self._new_executor()
|
|
self.tasks = [asyncio.create_task(self._render_loop()) for _ in range(self.processes)]
|
|
self.tasks.append(asyncio.create_task(self._watch()))
|
|
self.tasks.append(asyncio.create_task(self._warm()))
|
|
|
|
async def _warm(self):
|
|
"""Start every render process now, by drawing the world (zoom 0) in each, rather than when the first
|
|
tiles are asked for."""
|
|
loop = asyncio.get_running_loop()
|
|
sid, style, block = self.style_id, self.style, Block(0, 0, 0)
|
|
started = time.perf_counter()
|
|
results = await asyncio.gather(
|
|
*(loop.run_in_executor(self.executor, render.render_block, sid, style, block)
|
|
for _ in range(self.processes)), return_exceptions=True)
|
|
errors = [i for i in results if isinstance(i, BaseException)]
|
|
if errors:
|
|
logger.error('A render process could not draw', exc_info=errors[0])
|
|
return
|
|
await asyncio.to_thread(self.cache.put, block.z, results[0], sid)
|
|
logger.info(f'{self.processes} render processes ready in {time.perf_counter() - started:.1f}s')
|
|
|
|
async def stop(self):
|
|
for task in self.tasks:
|
|
task.cancel()
|
|
if self.executor:
|
|
self.executor.shutdown(wait=False, cancel_futures=True)
|
|
|
|
# Drawing.
|
|
|
|
async def _next(self) -> Tuple[str, Block]:
|
|
"""The next block to draw: the oldest of the client whose turn it is."""
|
|
async with self.condition:
|
|
await self.condition.wait_for(lambda: bool(self.queues))
|
|
client, queue = self.queues.popitem(last=False)
|
|
item = queue.popleft()
|
|
if queue:
|
|
self.queues[client] = queue # to the back of the line
|
|
return item
|
|
|
|
async def _render_loop(self):
|
|
loop = asyncio.get_running_loop()
|
|
while True:
|
|
sid, block = await self._next()
|
|
future = self.inflight.get((sid, block))
|
|
if future is None or future.done():
|
|
continue
|
|
self.rendering += 1
|
|
started = time.perf_counter()
|
|
try:
|
|
tiles = await loop.run_in_executor(self.executor, render.render_block, sid, self.style, block) \
|
|
if sid == self.style_id else None
|
|
except BrokenProcessPool as e:
|
|
# A render process died (the out-of-memory killer, most likely). Start them again.
|
|
logger.error('A render process died; starting new ones', exc_info=e)
|
|
self.executor = self._new_executor()
|
|
tiles, error = None, e
|
|
except Exception as e:
|
|
logger.error(f'Failed to draw block {block}', exc_info=e)
|
|
tiles, error = None, e
|
|
else:
|
|
error = None
|
|
finally:
|
|
self.rendering -= 1
|
|
self.inflight.pop((sid, block), None)
|
|
if tiles:
|
|
self.stats['blocks_drawn'] += 1
|
|
self.stats['draw_ms'] += int((time.perf_counter() - started) * 1000)
|
|
future.set_result(tiles)
|
|
asyncio.create_task(asyncio.to_thread(self.cache.put, block.z, tiles, sid))
|
|
elif error:
|
|
self.stats['blocks_failed'] += 1
|
|
future.set_exception(error)
|
|
else:
|
|
future.set_result(None) # the map files changed while it waited
|
|
|
|
async def _queue(self, client: str, sid: str, block: Block) -> asyncio.Future:
|
|
future = asyncio.get_running_loop().create_future()
|
|
self.inflight[(sid, block)] = future
|
|
async with self.condition:
|
|
queue = self.queues.setdefault(client, collections.deque())
|
|
queue.append((sid, block))
|
|
while len(queue) > MAX_QUEUED_PER_CLIENT:
|
|
dropped = self.inflight.pop(queue.popleft(), None)
|
|
if dropped and not dropped.done():
|
|
dropped.set_result(None)
|
|
self.stats['blocks_dropped'] += 1
|
|
self.condition.notify()
|
|
return future
|
|
|
|
async def tile(self, z: int, x: int, y: int, client: str) -> Tuple[Optional[bytes], bool]:
|
|
"""A tile, and whether it may be kept. None when it was not drawn in time or could not be drawn: the
|
|
phone must ask again (see the top of this module)."""
|
|
self._seen(client)
|
|
if not is_tile(z, x, y):
|
|
self.stats['tiles_outside'] += 1
|
|
return self.blank_tile, False
|
|
sid = self.style_id
|
|
data = await asyncio.to_thread(self.cache.get, z, x, y) if self.cache.enabled else None
|
|
if data:
|
|
self.stats['tiles_cached'] += 1
|
|
return data, True
|
|
|
|
block = block_of(z, x, y)
|
|
future = self.inflight.get((sid, block))
|
|
if future is None:
|
|
future = await self._queue(client, sid, block)
|
|
try:
|
|
tiles = await asyncio.wait_for(asyncio.shield(future), DEADLINE)
|
|
except asyncio.TimeoutError:
|
|
self.stats['tiles_late'] += 1
|
|
return None, False
|
|
except Exception:
|
|
self.stats['tiles_failed'] += 1
|
|
return None, False
|
|
if not tiles:
|
|
self.stats['tiles_dropped'] += 1
|
|
return None, False
|
|
self.stats['tiles_drawn'] += 1
|
|
return tiles[(x, y)], True
|
|
|
|
def _seen(self, client: str):
|
|
now = time.time()
|
|
entry = self.clients.setdefault(client, dict(tiles=0, first_seen=now))
|
|
entry['tiles'] += 1
|
|
entry['last_seen'] = now
|
|
self.served.append(now)
|
|
self._forget_served(now)
|
|
|
|
def _forget_served(self, now: float):
|
|
"""Tiles per second is of the last minute: forget tiles served before it (or a long download would keep a
|
|
time for every tile it took)."""
|
|
while self.served and self.served[0] < now - RATE_SECONDS:
|
|
self.served.popleft()
|
|
|
|
def status(self) -> dict:
|
|
now = time.time()
|
|
self._forget_served(now)
|
|
for client in [k for k, v in self.clients.items() if v['last_seen'] < now - 24 * 60 * 60]:
|
|
del self.clients[client]
|
|
drawn = self.stats['blocks_drawn']
|
|
return dict(
|
|
renderer=render.available(),
|
|
processes=self.processes,
|
|
max_zoom=self.max_zoom,
|
|
maps=[str(i) for i in self.maps],
|
|
style_id=self.style_id,
|
|
queued_blocks=sum(len(i) for i in self.queues.values()),
|
|
drawing_blocks=self.rendering,
|
|
tiles_per_second=round(len(self.served) / RATE_SECONDS, 1),
|
|
block_ms=round(self.stats['draw_ms'] / drawn) if drawn else None,
|
|
cache=dict(enabled=self.cache.enabled, bytes=self.cache.size, max_bytes=self.cache.max_bytes),
|
|
stats=dict(self.stats),
|
|
clients=[dict(client=k, tiles=v['tiles'], first_seen=round(v['first_seen']),
|
|
last_seen=round(v['last_seen']),
|
|
queued_blocks=len(self.queues.get(k, ())))
|
|
for k, v in sorted(self.clients.items(), key=lambda i: -i[1]['last_seen'])],
|
|
)
|
|
|
|
|
|
def client_of(request) -> str:
|
|
"""The phone asking: Caddy puts its address first in X-Forwarded-For."""
|
|
forwarded = request.headers.get('x-forwarded-for')
|
|
return forwarded.split(',')[0].strip() if forwarded else request.ip
|
|
|
|
|
|
def base_url_of(request) -> str:
|
|
"""The address the phone reached this server by, as Caddy says (the LAN, the hotspot, or a name)."""
|
|
scheme = request.headers.get('x-forwarded-proto') or request.scheme
|
|
host = request.headers.get('x-forwarded-host') or request.headers.get('host') or request.host
|
|
return f'{scheme}://{host}'
|
|
|
|
|
|
# The port of the WROLPi app's HTTPS (Caddy), for the page's links to it: 443 on a Raspberry Pi or Debian, the
|
|
# published port of the web container in Docker (8443 by default).
|
|
HTTPS_PORT = int(os.environ.get('TILES_HTTPS_PORT', 443))
|
|
|
|
INDEX_HTML = '''<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>WROLPi Map Tiles</title>
|
|
<style>
|
|
:root {{ color-scheme: light dark; --bg: #f3f4f3; --panel: #fff; --text: #222; --muted: #666; --border: #ccc;
|
|
--link: #2c5aa0; }}
|
|
@media (prefers-color-scheme: dark) {{
|
|
:root {{ --bg: #1b1c1d; --panel: #262728; --text: #ddd; --muted: #999; --border: #444; --link: #8ab4f8; }}
|
|
}}
|
|
body {{ margin: 0; padding: 16px; background: var(--bg); color: var(--text);
|
|
font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }}
|
|
main {{ max-width: 42rem; margin: 2rem auto; background: var(--panel); border: 1px solid var(--border);
|
|
border-radius: 6px; padding: 1.5rem; }}
|
|
h1 {{ margin-top: 0; font-size: 1.5rem; }}
|
|
a {{ color: var(--link); }}
|
|
code {{ word-break: break-all; }}
|
|
.muted {{ color: var(--muted); font-size: 0.875rem; }}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<main>
|
|
<h1>WROLPi Map Tiles</h1>
|
|
<p>This is the map tile service of a <a href="{app}">WROLPi</a>. It draws the WROLPi's map as tiles for
|
|
ATAK (Android) and iTAK (iPhone), which can use it as a map source and download areas of it for offline use.</p>
|
|
<p>To add the map to ATAK or iTAK, open this map source on the phone and open the file it downloads with the app:</p>
|
|
<p><a href="{xml}"><code>{xml}</code></a></p>
|
|
<p>The map's <a href="{atak}">ATAK tab</a> on the WROLPi has the steps, and what the tile service is doing.</p>
|
|
<p class="muted">{maps} · Map data © OpenStreetMap contributors</p>
|
|
</main>
|
|
</body>
|
|
</html>
|
|
'''
|
|
|
|
|
|
def index_html(base_url: str, map_count: int) -> str:
|
|
"""The page a person finds at this port: what it is, and links to the map source and to WROLPi."""
|
|
hostname = urlsplit(base_url).hostname or 'localhost'
|
|
if ':' in hostname:
|
|
hostname = f'[{hostname}]' # IPv6
|
|
app = f'https://{hostname}' + (f':{HTTPS_PORT}' if HTTPS_PORT != 443 else '')
|
|
maps_text = f'Drawn from {map_count} map file{"" if map_count == 1 else "s"}' if map_count else \
|
|
'There are no map files yet; download some on the WROLPi\'s Map page'
|
|
return INDEX_HTML.format(app=html.escape(f'{app}/'), atak=html.escape(f'{app}/map/atak'),
|
|
xml=html.escape(f'{base_url}/tiles/wrolpi.xml'), maps=html.escape(maps_text))
|
|
|
|
|
|
def create_app(service: TileService, name: str = 'wrolpi_tiles') -> Sanic:
|
|
app = Sanic(name)
|
|
# sanic-ext (installed for the API) would add OpenAPI docs and more, for nothing: seconds of startup on a Pi.
|
|
app.config.AUTO_EXTEND = False
|
|
app.ctx.service = service
|
|
|
|
@app.before_server_start
|
|
async def start(app_):
|
|
await app_.ctx.service.start()
|
|
|
|
@app.after_server_stop
|
|
async def stop(app_):
|
|
await app_.ctx.service.stop()
|
|
|
|
@app.get('/tiles/<z:int>/<x:int>/<name:str>')
|
|
async def get_tile(request, z: int, x: int, name: str):
|
|
y, _, suffix = name.partition('.')
|
|
if suffix != 'jpg' or not y.isdigit():
|
|
return response.text('not found', status=404)
|
|
data, keep = await app.ctx.service.tile(z, x, int(y), client_of(request))
|
|
if data is None:
|
|
# Not a 401 or 403: those stop an ATAK map source.
|
|
return response.text('Not drawn yet, try again', status=503,
|
|
headers={'Retry-After': '1', 'Cache-Control': 'no-store'})
|
|
if keep:
|
|
headers = {'Cache-Control': f'max-age={EXPIRES_SECONDS}',
|
|
'Expires': formatdate(time.time() + EXPIRES_SECONDS, usegmt=True)}
|
|
else:
|
|
headers = {'Cache-Control': 'no-store', 'Expires': formatdate(0, usegmt=True)}
|
|
return response.raw(data, content_type=render.CONTENT_TYPE, headers=headers)
|
|
|
|
@app.get('/', name='index')
|
|
@app.get('/tiles', name='tiles_index', strict_slashes=False)
|
|
async def get_index(request):
|
|
return response.html(index_html(base_url_of(request), len(app.ctx.service.maps)))
|
|
|
|
@app.get('/tiles/wrolpi.xml')
|
|
async def get_xml(request):
|
|
base_url = base_url_of(request)
|
|
https = base_url.startswith('https:')
|
|
name = f'{NAME} (HTTPS)' if https else NAME
|
|
filename = 'wrolpi-map-https.xml' if https else 'wrolpi-map.xml'
|
|
service_ = app.ctx.service
|
|
background = background_color(service_.style) if service_.style else '#cccccc'
|
|
return response.text(mobac_xml(base_url, name, service_.max_zoom, background), content_type='application/xml',
|
|
headers={'Content-Disposition': f'attachment; filename="{filename}"'})
|
|
|
|
@app.get('/tiles/status')
|
|
async def get_status(request):
|
|
return response.json(app.ctx.service.status())
|
|
|
|
@app.post('/tiles/cache/clear')
|
|
async def post_clear_cache(request):
|
|
freed = await asyncio.to_thread(app.ctx.service.cache.clear)
|
|
return response.json(dict(freed_bytes=freed))
|
|
|
|
return app
|