Skip to content

Commit

Permalink
Explicitly set CORS headers
Browse files Browse the repository at this point in the history
By default FastAPI only replies with CORS headers when an origin is
sent. This means that the response should be sent with Vary: Origin,
but it isn't. If this header were added it would reduce cachability.

When we always send the headers this means that the response can be
cached regardless of origin, and CORS are always present.
  • Loading branch information
pnorman committed Feb 24, 2024
1 parent 93d15cc commit e366908
Showing 1 changed file with 2 additions and 11 deletions.
13 changes: 2 additions & 11 deletions tilekiln/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import psycopg
import psycopg_pool
from fastapi import FastAPI, Response, HTTPException
from fastapi.middleware.cors import CORSMiddleware

import tilekiln
from tilekiln.config import Config
Expand All @@ -22,25 +21,17 @@
TILEKILN_URL = "TILEKILN_URL"
TILEKILN_THREADS = "TILEKILN_THREADS"

STANDARD_HEADERS: dict[str, str] = {}
STANDARD_HEADERS: dict[str, str] = {"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, HEAD"}

kiln: Kiln
config: Config
storage: Storage
tilesets: dict[str, Tileset] = {}

# Two types of server are defined - one for static tiles, the other for live generated tiles.

server = FastAPI()
server.add_middleware(CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"])
live = FastAPI()
live.add_middleware(CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"])


# TODO: Move elsewhere
Expand Down

0 comments on commit e366908

Please sign in to comment.