-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
130 lines (106 loc) · 4.28 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""titiler-openeo app."""
import logging
from fastapi import FastAPI, HTTPException
from fastapi.exceptions import RequestValidationError
from openeo_pg_parser_networkx.process_registry import Process
from starlette.middleware.cors import CORSMiddleware
from starlette_cramjam.middleware import CompressionMiddleware
from titiler.core.middleware import CacheControlMiddleware
from titiler.openeo import __version__ as titiler_version
from titiler.openeo.auth import get_auth
from titiler.openeo.errors import ExceptionHandler, OpenEOException
from titiler.openeo.factory import EndpointsFactory
from titiler.openeo.processes import PROCESS_SPECIFICATIONS, process_registry
from titiler.openeo.services import get_store
from titiler.openeo.settings import ApiSettings, AuthSettings, BackendSettings
from titiler.openeo.stacapi import LoadCollection, stacApiBackend
STAC_VERSION = "1.0.0"
api_settings = ApiSettings()
auth_settings = AuthSettings()
# BackendSettings requires stac_api_url and service_store_url to be set via environment variables
try:
backend_settings = BackendSettings()
except Exception as err:
raise ValueError(
"Missing required environment variables for BackendSettings. "
"Please set TITILER_OPENEO_STAC_API_URL and TITILER_OPENEO_SERVICE_STORE_URL"
) from err
stac_client = stacApiBackend(str(backend_settings.stac_api_url)) # type: ignore
service_store = get_store(str(backend_settings.service_store_url))
auth = get_auth(auth_settings)
###############################################################################
def create_app():
app = FastAPI(
title=api_settings.name,
openapi_url="/api",
docs_url="/api.html",
description="""TiTiler backend for openEO.
---
**Documentation**: <a href="https://developmentseed.org/titiler-openeo/" target="_blank">https://developmentseed.org/titiler-openeo/</a>
**Source Code**: <a href="https://github.com/sentinel-hub/titiler-openeo" target="_blank">https://github.com/sentinel-hub/titiler-openeo</a>
---
""",
version=titiler_version,
root_path=api_settings.root_path,
debug=api_settings.debug,
)
# Set all CORS enabled origins
if api_settings.cors_origins:
app.add_middleware(
CORSMiddleware,
allow_origins=api_settings.cors_origins,
allow_credentials=True,
allow_methods=api_settings.cors_allow_methods,
allow_headers=["*"],
expose_headers=["*"],
)
app.add_middleware(
CompressionMiddleware,
minimum_size=0,
exclude_mediatype={
"image/jpeg",
"image/jpg",
"image/png",
"image/jp2",
"image/webp",
},
compression_level=6,
)
app.add_middleware(
CacheControlMiddleware,
cachecontrol=api_settings.cachecontrol,
)
# Register backend specific load_collection methods
loaders = LoadCollection(stac_client) # type: ignore
process_registry["load_collection"] = process_registry["load_collection"] = Process(
spec=PROCESS_SPECIFICATIONS["load_collection"],
implementation=loaders.load_collection,
)
process_registry["load_collection_and_reduce"] = process_registry[
"load_collection_and_reduce"
] = Process(
spec=PROCESS_SPECIFICATIONS["load_collection_and_reduce"],
implementation=loaders.load_collection_and_reduce,
)
# Register OpenEO endpoints
endpoints = EndpointsFactory(
services_store=service_store,
stac_client=stac_client,
process_registry=process_registry,
auth=auth,
)
app.include_router(endpoints.router)
app.endpoints = endpoints
# Create exception handler instance
exception_handler = ExceptionHandler(logger=logging.getLogger(__name__))
# Add OpenEO-specific exception handlers
app.add_exception_handler(
OpenEOException, exception_handler.openeo_exception_handler
)
app.add_exception_handler(
RequestValidationError, exception_handler.validation_exception_handler
)
app.add_exception_handler(HTTPException, exception_handler.http_exception_handler)
app.add_exception_handler(Exception, exception_handler.general_exception_handler)
return app
app = create_app()