Untitled

 avatar
unknown
python
2 years ago
1.6 kB
7
Indexable
from fastapi import FastAPI, Request, HTTPException
import httpx
import uvicorn

app = FastAPI()

GEOSERVER_URL = 'http://127.0.0.1:8080'

async def forward_request(request: Request):
    async with httpx.AsyncClient() as client:
        response = await client.request(
            method=request.method,
            url=GEOSERVER_URL + request.url.path,
            params=request.query_params,
            headers=request.headers,
            content=await request.body()
        )
        return response

def valid_getcapabilities(request: Request):
    allowed_params = {'request', 'service'}
    p = request.query_params
    return p.get('service') == 'WMS' and p.keys() == allowed_params

def valid_getmap(request: Request):
    allowed_params = {
        'bboxsr', 'exceptions', 'format', 'height', 'imagesr', 'layers',
        'request', 'service', 'srs', 'styles', 'time', 'transparent',
        'version', 'width', 'bbox'
    }
    p = request.query_params
    return p.keys() == allowed_params and \
        p.get('service') == 'WMS' and \
        p.get('format') == 'image/png' and \
        p.get('version') == '1.1.1'

@app.get("/wms")
async def wms_proxy(request: Request):
    q_req = request.query_params.get('request')
    if (q_req == 'GetCapabilities' and valid_getcapabilities(request)) or \
        (q_req == 'GetMap' and valid_getmap(request)):
        response = await forward_request(request)
        return response.content
    raise HTTPException(status_code=400, detail="Invalid Request")

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=3000)
Editor is loading...
Leave a Comment