Configure API routes after set up search types while configuring server

Configure app routes after configuring server.
Import API routers after search type is dynamically populated.
Allow API to recognize the dynamically populated plugin search types
as valid type query param.
Enable searching for plugin type content.
This commit is contained in:
Debanjum Singh Solanky 2023-02-24 03:06:58 -06:00
parent d91c7e2761
commit 68bd5d9ebc
3 changed files with 20 additions and 11 deletions

View file

@ -6,6 +6,7 @@ from enum import Enum
# External Packages # External Packages
import schedule import schedule
from fastapi.staticfiles import StaticFiles
# Internal Packages # Internal Packages
from khoj.processor.ledger.beancount_to_jsonl import BeancountToJsonl from khoj.processor.ledger.beancount_to_jsonl import BeancountToJsonl
@ -13,8 +14,8 @@ from khoj.processor.jsonl.jsonl_to_jsonl import JsonlToJsonl
from khoj.processor.markdown.markdown_to_jsonl import MarkdownToJsonl from khoj.processor.markdown.markdown_to_jsonl import MarkdownToJsonl
from khoj.processor.org_mode.org_to_jsonl import OrgToJsonl from khoj.processor.org_mode.org_to_jsonl import OrgToJsonl
from khoj.search_type import image_search, text_search from khoj.search_type import image_search, text_search
from khoj.utils import constants, state
from khoj.utils.config import SearchType, SearchModels, ProcessorConfigModel, ConversationProcessorConfigModel from khoj.utils.config import SearchType, SearchModels, ProcessorConfigModel, ConversationProcessorConfigModel
from khoj.utils import state
from khoj.utils.helpers import LRU, resolve_absolute_path, merge_dicts from khoj.utils.helpers import LRU, resolve_absolute_path, merge_dicts
from khoj.utils.rawconfig import FullConfig, ProcessorConfig from khoj.utils.rawconfig import FullConfig, ProcessorConfig
from khoj.search_filter.date_filter import DateFilter from khoj.search_filter.date_filter import DateFilter
@ -48,6 +49,18 @@ def configure_server(args, required=False):
state.search_index_lock.release() state.search_index_lock.release()
def configure_routes(app):
# Import APIs here to setup search types before while configuring server
from khoj.routers.api import api
from khoj.routers.api_beta import api_beta
from khoj.routers.web_client import web_client
app.mount("/static", StaticFiles(directory=constants.web_directory), name="static")
app.include_router(api, prefix="/api")
app.include_router(api_beta, prefix="/api/beta")
app.include_router(web_client)
@schedule.repeat(schedule.every(1).hour) @schedule.repeat(schedule.every(1).hour)
def update_search_index(): def update_search_index():
state.search_index_lock.acquire() state.search_index_lock.acquire()

View file

@ -14,18 +14,14 @@ warnings.filterwarnings("ignore", message=r"legacy way to download files from th
# External Packages # External Packages
import uvicorn import uvicorn
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from PyQt6 import QtWidgets from PyQt6 import QtWidgets
from PyQt6.QtCore import QThread, QTimer from PyQt6.QtCore import QThread, QTimer
from rich.logging import RichHandler from rich.logging import RichHandler
import schedule import schedule
# Internal Packages # Internal Packages
from khoj.configure import configure_server from khoj.configure import configure_routes, configure_server
from khoj.routers.api import api from khoj.utils import state
from khoj.routers.api_beta import api_beta
from khoj.routers.web_client import web_client
from khoj.utils import constants, state
from khoj.utils.cli import cli from khoj.utils.cli import cli
from khoj.interface.desktop.main_window import MainWindow from khoj.interface.desktop.main_window import MainWindow
from khoj.interface.desktop.system_tray import create_system_tray from khoj.interface.desktop.system_tray import create_system_tray
@ -33,10 +29,6 @@ from khoj.interface.desktop.system_tray import create_system_tray
# Initialize the Application Server # Initialize the Application Server
app = FastAPI() app = FastAPI()
app.mount("/static", StaticFiles(directory=constants.web_directory), name="static")
app.include_router(api, prefix="/api")
app.include_router(api_beta, prefix="/api/beta")
app.include_router(web_client)
# Setup Logger # Setup Logger
rich_handler = RichHandler(rich_tracebacks=True) rich_handler = RichHandler(rich_tracebacks=True)
@ -78,6 +70,7 @@ def run():
poll_task_scheduler() poll_task_scheduler()
# Start Server # Start Server
configure_server(args, required=False) configure_server(args, required=False)
configure_routes(app)
start_server(app, host=args.host, port=args.port, socket=args.socket) start_server(app, host=args.host, port=args.port, socket=args.socket)
else: else:
# Setup GUI # Setup GUI
@ -95,6 +88,7 @@ def run():
# Setup Server # Setup Server
configure_server(args, required=False) configure_server(args, required=False)
configure_routes(app)
server = ServerThread(app, args.host, args.port, args.socket) server = ServerThread(app, args.host, args.port, args.socket)
# Show Main Window on First Run Experience or if on Linux # Show Main Window on First Run Experience or if on Linux

View file

@ -9,6 +9,7 @@ from fastapi.testclient import TestClient
# Internal Packages # Internal Packages
from khoj.main import app from khoj.main import app
from khoj.configure import configure_routes
from khoj.utils.state import model, config from khoj.utils.state import model, config
from khoj.search_type import text_search, image_search from khoj.search_type import text_search, image_search
from khoj.utils.rawconfig import ContentConfig, SearchConfig from khoj.utils.rawconfig import ContentConfig, SearchConfig
@ -19,6 +20,7 @@ from khoj.search_filter.file_filter import FileFilter
# Arrange # Arrange
# ---------------------------------------------------------------------------------------------------- # ----------------------------------------------------------------------------------------------------
configure_routes(app)
client = TestClient(app) client = TestClient(app)