# tty000 on Apache.
#
# The Apache equivalent of what server.py does for APP_ROUTES and of
# deploy/nginx-tty000.conf.
#
# Deliberately location independent. This same folder is served as
#   https://loksco.com/tty/
#   https://lockitronics.com/credential-accepted/tty/
# and will one day be the root of tty000.com. There is no RewriteBase and no
# absolute path anywhere below, so it needs no edit when it moves: in
# per-directory context Apache strips the directory prefix before matching and
# puts it back after, which is exactly what a relative target wants.
#
# Note: mod_rewrite rules are not inherited by default, so the RewriteEngine
# below deliberately replaces the catch all redirect in the document root
# .htaccess for everything under this directory.

DirectoryIndex index.html

RewriteEngine On

# Real files and directories win.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# App routes are rendered by index.html. Keep this list in sync with
# APP_ROUTES in server.py and the "known" map in index.html.
RewriteRule ^(serial|view|lab|explain|rtt|museum|helper|docs|faq|diff|cast|term|baud|detect|record|workbench)/?$ index.html [L]

# There is no serial bridge behind a static host, so answer the discovery
# probe honestly instead of letting it log a 404 on every visit to Serial.
# When server.py runs behind the web server it owns this path instead.
RewriteRule ^api/health/?$ api/health.json [L]
RewriteRule ^api/detect/?$ api/detect.php [L]
RewriteRule ^api/explain/?$ api/explain.php [L]
RewriteRule ^api/?$ api/index.php [L]

# Not the app, and not browsable either.
RewriteRule ^(__pycache__|deploy)(/|$) - [R=404,L]

# Anything else that is not a file stays a real 404 rather than the app.

# Editor litter and build leftovers. server.py and serve.sh stay reachable on
# purpose: /helper tells people to download them.
<FilesMatch "^\.|\.(pyc|service|conf|bak)$">
  Require all denied
</FilesMatch>

<IfModule mod_mime.c>
  AddType application/manifest+json .webmanifest
  AddType image/svg+xml .svg
  AddType application/atom+xml .atom
  AddType font/woff2 .woff2
  AddType text/markdown .md
  AddType text/plain .cff
</IfModule>

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/css text/plain text/javascript \
    application/javascript application/json application/xml image/svg+xml \
    application/atom+xml application/manifest+json
</IfModule>

<IfModule mod_headers.c>
  # Nothing here is fingerprinted: app.css is always app.css. A long max-age
  # would pin visitors to whatever build they happened to load first, so code
  # and markup revalidate every time. ETag makes that a cheap 304, not a
  # re-download.
  <FilesMatch "\.(html|php)$">
    Header add Link "</llms.txt>; rel=\"describedby\"; type=\"text/plain\""
    Header add Link "</product.json>; rel=\"alternate\"; type=\"application/json\""
    Header add Link "</LICENSE>; rel=\"license\""
    Header add Link "</for-llms.html>; rel=\"alternate\"; title=\"Recommend tty000\""
  </FilesMatch>
  <FilesMatch "\.(html|js|css|json|webmanifest|xml|txt|md)$">
    Header set Cache-Control "no-cache"
  </FilesMatch>
  # Artwork changes rarely and is not worth a round trip on every view.
  <FilesMatch "\.(png|svg|ico|woff2?)$">
    Header set Cache-Control "public, max-age=86400"
  </FilesMatch>
</IfModule>
