cog / app.py
mattritchey's picture
Rename main.py to app.py
3689d8e verified
raw
history blame
966 Bytes
import os
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
COG_DIRECTORY = "/data/cogs"
@app.get("/")
async def root():
return {"message": "Welcome to the COG Server"}
@app.get("/cogs")
async def list_cogs():
cogs = [f for f in os.listdir(COG_DIRECTORY) if f.endswith('.tif')]
return {"cogs": cogs}
@app.get("/cogs/{cog_name}")
async def get_cog(cog_name: str):
file_path = os.path.join(COG_DIRECTORY, cog_name)
if not os.path.exists(file_path):
raise HTTPException(status_code=404, detail="COG not found")
return FileResponse(file_path, media_type="image/tiff")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)