Skip to content

Commit 57f9073

Browse files
authored
fix(UI): use https when calling the api (#139)
Signed-off-by: Michele Dolfi <[email protected]>
1 parent 525a43f commit 57f9073

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

docling_serve/gradio_ui.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import importlib
22
import json
33
import logging
4+
import ssl
45
import tempfile
56
from pathlib import Path
67

8+
import certifi
79
import gradio as gr
8-
import requests
10+
import httpx
911

1012
from docling_serve.helper_functions import _to_list_of_strings
1113
from docling_serve.settings import docling_serve_settings, uvicorn_settings
@@ -109,8 +111,29 @@
109111
#############
110112

111113

114+
def get_api_endpoint() -> str:
115+
protocol = "http"
116+
if uvicorn_settings.ssl_keyfile is not None:
117+
protocol = "https"
118+
return f"{protocol}://{docling_serve_settings.api_host}:{uvicorn_settings.port}"
119+
120+
121+
def get_ssl_context() -> ssl.SSLContext:
122+
ctx = ssl.create_default_context(cafile=certifi.where())
123+
kube_sa_ca_cert_path = Path(
124+
"/run/secrets/kubernetes.io/serviceaccount/service-ca.crt"
125+
)
126+
if (
127+
uvicorn_settings.ssl_keyfile is not None
128+
and ".svc." in docling_serve_settings.api_host
129+
and kube_sa_ca_cert_path.exists()
130+
):
131+
ctx.load_verify_locations(cafile=kube_sa_ca_cert_path)
132+
return ctx
133+
134+
112135
def health_check():
113-
response = requests.get(f"http://localhost:{uvicorn_settings.port}/health")
136+
response = httpx.get(f"{get_api_endpoint()}/health")
114137
if response.status_code == 200:
115138
return "Healthy"
116139
return "Unhealthy"
@@ -231,9 +254,12 @@ def process_url(
231254
logger.error("No input sources provided.")
232255
raise gr.Error("No input sources provided.", print_exception=False)
233256
try:
234-
response = requests.post(
235-
f"http://localhost:{uvicorn_settings.port}/v1alpha/convert/source",
257+
ssl_ctx = get_ssl_context()
258+
response = httpx.post(
259+
f"{get_api_endpoint()}/v1alpha/convert/source",
236260
json=parameters,
261+
verify=ssl_ctx,
262+
timeout=60,
237263
)
238264
except Exception as e:
239265
logger.error(f"Error processing URL: {e}")
@@ -287,10 +313,13 @@ def process_file(
287313
}
288314

289315
try:
290-
response = requests.post(
291-
f"http://localhost:{uvicorn_settings.port}/v1alpha/convert/file",
316+
ssl_ctx = get_ssl_context()
317+
response = httpx.post(
318+
f"{get_api_endpoint()}/v1alpha/convert/file",
292319
files=files_data,
293320
data=parameters,
321+
verify=ssl_ctx,
322+
timeout=60,
294323
)
295324
except Exception as e:
296325
logger.error(f"Error processing file(s): {e}")

docling_serve/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class DoclingServeSettings(BaseSettings):
3232
)
3333

3434
enable_ui: bool = False
35+
api_host: str = "localhost"
3536
artifacts_path: Optional[Path] = None
3637
static_path: Optional[Path] = None
3738
options_cache_size: int = 2

docs/deploy-examples/docling-serve-oauth.yaml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ spec:
107107
- name: api
108108
resources:
109109
limits:
110-
cpu: 500m
110+
cpu: 2000m
111111
memory: 2Gi
112112
requests:
113-
cpu: 250m
113+
cpu: 800m
114114
memory: 1Gi
115115
readinessProbe:
116116
httpGet:
@@ -128,13 +128,19 @@ spec:
128128
port: http
129129
scheme: HTTPS
130130
initialDelaySeconds: 3
131-
timeoutSeconds: 2
132-
periodSeconds: 5
131+
timeoutSeconds: 4
132+
periodSeconds: 10
133133
successThreshold: 1
134-
failureThreshold: 3
134+
failureThreshold: 5
135135
env:
136+
- name: NAMESPACE
137+
valueFrom:
138+
fieldRef:
139+
fieldPath: metadata.namespace
136140
- name: DOCLING_SERVE_ENABLE_UI
137141
value: 'true'
142+
- name: DOCLING_SERVE_API_HOST
143+
value: 'docling-serve.$(NAMESPACE).svc.cluster.local'
138144
- name: UVICORN_SSL_CERTFILE
139145
value: '/etc/tls/private/tls.crt'
140146
- name: UVICORN_SSL_KEYFILE

0 commit comments

Comments
 (0)