Skip to content

Commit

Permalink
♻️ Centralize TMS handler
Browse files Browse the repository at this point in the history
  • Loading branch information
akrherz committed Mar 7, 2025
1 parent 93a0586 commit 09e7979
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 65 deletions.
33 changes: 3 additions & 30 deletions pylib/iemweb/c/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import os

from pyiem.database import get_sqlalchemy_conn
from sqlalchemy import text
from TileCache.Service import Service

# https://github.com/akrherz/tilecache
from TileCache import InvalidTMSRequest
from TileCache.Service import Service, wsgiHandler
from iemweb.util import tms_handler

tilecachepath, wsgi_file = os.path.split(__file__)
cfgfiles = os.path.join(tilecachepath, "tilecache.cfg")
Expand All @@ -18,28 +15,4 @@ def application(environ, start_response):
"""Go service."""
if not theService["app"]:
theService["app"] = Service.load(cfgfiles)
try:
return wsgiHandler(environ, start_response, theService["app"])
except InvalidTMSRequest:
with get_sqlalchemy_conn("mesosite") as conn:
conn.execute(
text("""
insert into weblog(client_addr, uri, referer, http_status,
x_forwarded_for)
VALUES (:addr, :uri, :ref, :status, :for)
"""),
{
"addr": environ.get(
"HTTP_X_FORWARDED_FOR", environ.get("REMOTE_ADDR")
)
.split(",")[0]
.strip(),
"uri": environ.get("PATH_INFO"),
"ref": environ.get("HTTP_REFERER"),
"status": 404,
"for": environ.get("HTTP_X_FORWARDED_FOR"),
},
)
conn.commit()
start_response("404 Not Found", [("Content-Type", "text/plain")])
return [b"Invalid TMS request"]
return tms_handler(environ, start_response, theService["app"])
38 changes: 3 additions & 35 deletions pylib/iemweb/cache/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import os

from pyiem.database import get_sqlalchemy_conn
from sqlalchemy import text
from TileCache.Service import Service

# https://github.com/akrherz/tilecache
from TileCache import InvalidTMSRequest
from TileCache.Service import Service, wsgiHandler
from iemweb.util import tms_handler

tilecachepath, wsgi_file = os.path.split(__file__)
cfgfiles = os.path.join(tilecachepath, "tilecache.cfg")
Expand All @@ -18,33 +15,4 @@ def application(environ, start_response):
"""Go service."""
if not theService["app"]:
theService["app"] = Service.load(cfgfiles)
try:
return wsgiHandler(environ, start_response, theService["app"])
except InvalidTMSRequest:
with get_sqlalchemy_conn("mesosite") as conn:
# 5x the 404 logging
conn.execute(
text("""
insert into weblog(client_addr, uri, referer, http_status,
x_forwarded_for)
VALUES (:addr, :uri, :ref, :status, :for),
(:addr, :uri, :ref, :status, :for),
(:addr, :uri, :ref, :status, :for),
(:addr, :uri, :ref, :status, :for),
(:addr, :uri, :ref, :status, :for)
"""),
{
"addr": environ.get(
"HTTP_X_FORWARDED_FOR", environ.get("REMOTE_ADDR")
)
.split(",")[0]
.strip(),
"uri": environ.get("PATH_INFO"),
"ref": environ.get("HTTP_REFERER"),
"status": 404,
"for": environ.get("HTTP_X_FORWARDED_FOR"),
},
)
conn.commit()
start_response("404 Not Found", [("Content-Type", "text/plain")])
return [b"Invalid TMS request"]
return tms_handler(environ, start_response, theService["app"])
34 changes: 34 additions & 0 deletions pylib/iemweb/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,40 @@

from datetime import datetime

from pyiem.database import get_sqlalchemy_conn, sql_helper
from TileCache import InvalidTMSRequest
from TileCache.Service import wsgiHandler


def tms_handler(environ: dict, start_response: callable, service: dict):
"""Handler for TMS requests and subsequent failures."""
try:
return wsgiHandler(environ, start_response, service)
except InvalidTMSRequest:
with get_sqlalchemy_conn("mesosite") as conn:
conn.execute(
sql_helper("""
insert into weblog(client_addr, uri, referer, http_status,
x_forwarded_for, domain)
VALUES (:addr, :uri, :ref, :status, :for, :domain)
"""),
{
"addr": environ.get(
"HTTP_X_FORWARDED_FOR", environ.get("REMOTE_ADDR")
)
.split(",")[0]
.strip(),
"uri": environ.get("PATH_INFO"),
"ref": environ.get("HTTP_REFERER"),
"status": 404,
"for": environ.get("HTTP_X_FORWARDED_FOR"),
"domain": environ.get("HTTP_HOST"),
},
)
conn.commit()
start_response("404 Not Found", [("Content-Type", "text/plain")])
return [b"Invalid TMS request"]


def month2months(month: str) -> list[int]:
"""
Expand Down

0 comments on commit 09e7979

Please sign in to comment.