-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
161 lines (137 loc) · 5.11 KB
/
Copy pathmain.py
File metadata and controls
161 lines (137 loc) · 5.11 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""titiler-openeo app."""
import logging
from fastapi import FastAPI, HTTPException
from fastapi.exceptions import RequestValidationError
from openeo_pg_parser_networkx.process_registry import (
Process, # type: ignore[import-untyped]
)
from starlette.middleware.cors import CORSMiddleware
from starlette_cramjam.middleware import CompressionMiddleware
from . import __version__ as titiler_version
from .auth import get_auth
from .errors import ExceptionHandler, OpenEOException
from .factory import EndpointsFactory
from .health import register_health_endpoints
from .middleware import DynamicCacheControlMiddleware
from .processes import PROCESS_SPECIFICATIONS, process_registry
from .services import get_store, get_tile_store, get_udp_store
from .settings import ApiSettings, AuthSettings, BackendSettings
from .stacapi import LoadCollection, LoadStac, stacApiBackend
STAC_VERSION = "1.0.0"
api_settings = ApiSettings()
auth_settings = AuthSettings()
# BackendSettings requires stac_api_url and store_url to be set via environment variables
try:
backend_settings = BackendSettings() # type: ignore[call-arg]
except Exception as err:
raise ValueError(
"Missing required environment variables for BackendSettings. "
"Please set TITILER_OPENEO_STAC_API_URL and TITILER_OPENEO_STORE_URL"
) from err
stac_client = stacApiBackend(
str(backend_settings.stac_api_url),
exclude_collections=backend_settings.exclude_collections,
) # type: ignore
service_store = get_store(str(backend_settings.store_url))
udp_store = get_udp_store(str(backend_settings.store_url))
tile_store = (
get_tile_store(backend_settings.tile_store_url)
if backend_settings.tile_store_url
else None
)
auth = get_auth(auth_settings, store=service_store)
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://sentinel-hub.github.io/titiler-openeo" target="_blank">https://sentinel-hub.github.io/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(
DynamicCacheControlMiddleware,
static_paths=["/static/"],
dynamic_paths=[
"/processes/",
"/jobs/",
"/collections/",
"/services/",
"/results/",
],
)
# Register backend specific load_collection methods
loaders = LoadCollection(stac_client) # type: ignore
process_registry["load_collection"] = Process(
spec=PROCESS_SPECIFICATIONS["load_collection"],
implementation=loaders.load_collection,
)
loaders = LoadStac() # type: ignore
process_registry["load_stac"] = Process(
spec=PROCESS_SPECIFICATIONS["load_stac"],
implementation=loaders.load_stac,
)
# Register OpenEO endpoints
# Create endpoints with optional tile_store
factory_args = {
"services_store": service_store,
"udp_store": udp_store,
"stac_client": stac_client,
"process_registry": process_registry,
"auth": auth,
"default_services_file": backend_settings.default_services_file,
}
if tile_store:
factory_args["tile_store"] = tile_store
endpoints = EndpointsFactory(**factory_args)
app.include_router(endpoints.router)
app.endpoints = endpoints
# Kubernetes-style health endpoints. Registered after the OpenEO router
# so the explicit /healthz and /readyz paths take precedence and are
# excluded from the OpenAPI schema.
register_health_endpoints(
app,
service_store=service_store,
tile_store=tile_store,
stac_client=stac_client,
auth=auth,
)
# 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()