Pass multiple allowed Khoj domains via KHOJ_DOMAIN env var

To add multiple allowed Khoj domains pass them as a comma separated
list of domains via the KHOJ_DOMAIN environment variable

Resolve comment in issue #662
This commit is contained in:
Debanjum Singh Solanky 2024-06-14 17:01:25 +05:30
parent 4daf16e5f9
commit d8a5a01cea

View file

@ -3,6 +3,7 @@
"""
from contextlib import redirect_stdout
import itertools
import logging
import io
import os
@ -66,19 +67,25 @@ else:
django_app = get_asgi_application()
# Add CORS middleware
KHOJ_DOMAIN = os.getenv("KHOJ_DOMAIN", "app.khoj.dev")
KHOJ_DOMAIN = os.getenv("KHOJ_DOMAIN", "app.khoj.dev").split(",")
scheme = "https" if not is_env_var_true("KHOJ_NO_HTTPS") else "http"
custom_origins = list(
itertools.chain.from_iterable(
[[f"{scheme}://{domain.strip()}", f"{scheme}://{domain.strip()}:*"] for domain in KHOJ_DOMAIN]
)
)
default_origins = [
"app://obsidian.md", # To allow access from Obsidian desktop app
"capacitor://localhost", # To allow access from Obsidian iOS app using Capacitor.JS
"http://localhost", # To allow access from Obsidian Android app
"http://localhost:*", # To allow access from localhost
"http://127.0.0.1:*", # To allow access from localhost
"app://khoj.dev", # To allow access from Khoj desktop app
]
app.add_middleware(
CORSMiddleware,
allow_origins=[
"app://obsidian.md",
"capacitor://localhost", # To allow access from Obsidian iOS app using Capacitor.JS
"http://localhost", # To allow access from Obsidian Android app
"http://localhost:*",
"http://127.0.0.1:*",
f"https://{KHOJ_DOMAIN}" if not is_env_var_true("KHOJ_NO_HTTPS") else f"http://{KHOJ_DOMAIN}",
f"https://{KHOJ_DOMAIN}:*" if not is_env_var_true("KHOJ_NO_HTTPS") else f"http://{KHOJ_DOMAIN}:*",
"app://khoj.dev",
],
allow_origins=default_origins + custom_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],