Spaces:
Build error
Build error
mattritchey
commited on
Commit
•
1e71909
1
Parent(s):
8539a96
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from fastapi import FastAPI, HTTPException
|
3 |
+
from fastapi.responses import FileResponse
|
4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
5 |
+
|
6 |
+
app = FastAPI()
|
7 |
+
|
8 |
+
# Add CORS middleware
|
9 |
+
app.add_middleware(
|
10 |
+
CORSMiddleware,
|
11 |
+
allow_origins=["*"],
|
12 |
+
allow_credentials=True,
|
13 |
+
allow_methods=["*"],
|
14 |
+
allow_headers=["*"],
|
15 |
+
)
|
16 |
+
|
17 |
+
COG_DIRECTORY = "/data/cogs"
|
18 |
+
|
19 |
+
@app.get("/")
|
20 |
+
async def root():
|
21 |
+
return {"message": "Welcome to the COG Server"}
|
22 |
+
|
23 |
+
@app.get("/cogs")
|
24 |
+
async def list_cogs():
|
25 |
+
cogs = [f for f in os.listdir(COG_DIRECTORY) if f.endswith('.tif')]
|
26 |
+
return {"cogs": cogs}
|
27 |
+
|
28 |
+
@app.get("/cogs/{cog_name}")
|
29 |
+
async def get_cog(cog_name: str):
|
30 |
+
file_path = os.path.join(COG_DIRECTORY, cog_name)
|
31 |
+
if not os.path.exists(file_path):
|
32 |
+
raise HTTPException(status_code=404, detail="COG not found")
|
33 |
+
return FileResponse(file_path, media_type="image/tiff")
|
34 |
+
|
35 |
+
if __name__ == "__main__":
|
36 |
+
import uvicorn
|
37 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|