Skip to content

Commit

Permalink
Auto redirect requests to use HTTPS if server is using SSL certs
Browse files Browse the repository at this point in the history
  • Loading branch information
debanjum committed Dec 16, 2024
1 parent 132f2c9 commit 9c64275
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/khoj/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from starlette.middleware import Middleware
from starlette.middleware.authentication import AuthenticationMiddleware
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.middleware.httpsredirect import HTTPSRedirectMiddleware
from starlette.middleware.sessions import SessionMiddleware
from starlette.requests import HTTPConnection
from starlette.types import ASGIApp, Receive, Scope, Send
Expand All @@ -43,7 +44,6 @@
from khoj.database.models import ClientApplication, KhojUser, ProcessLock, Subscription
from khoj.processor.embeddings import CrossEncoderModel, EmbeddingsModel
from khoj.routers.api_content import configure_content, configure_search
from khoj.routers.helpers import update_telemetry_state
from khoj.routers.twilio import is_twilio_enabled
from khoj.utils import constants, state
from khoj.utils.config import SearchType
Expand Down Expand Up @@ -343,7 +343,7 @@ def configure_routes(app):
logger.info("📞 Enabled Twilio")


def configure_middleware(app):
def configure_middleware(app, ssl_enabled: bool = False):
class NextJsMiddleware(Middleware):
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if scope["type"] == "http" and scope["path"].startswith("/_next"):
Expand All @@ -354,6 +354,8 @@ def __init__(self, app: ASGIApp) -> None:
super().__init__(app)
self.app = app

if ssl_enabled:
app.add_middleware(HTTPSRedirectMiddleware)
app.add_middleware(AsyncCloseConnectionsMiddleware)
app.add_middleware(AuthenticationMiddleware, backend=UserAuthenticationBackend())
app.add_middleware(NextJsMiddleware)
Expand Down
2 changes: 1 addition & 1 deletion src/khoj/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def run(should_start_server=True):
app.mount(f"/static", StaticFiles(directory=static_dir), name=static_dir)

# Configure Middleware
configure_middleware(app)
configure_middleware(app, state.ssl_config)

initialize_server(args.config)

Expand Down

0 comments on commit 9c64275

Please sign in to comment.