2021-08-17 12:59:58 +02:00
|
|
|
# Standard Packages
|
2022-08-06 01:37:52 +02:00
|
|
|
import sys
|
2021-08-17 12:59:58 +02:00
|
|
|
|
|
|
|
# External Packages
|
|
|
|
import uvicorn
|
2022-08-06 01:37:52 +02:00
|
|
|
from fastapi import FastAPI
|
2021-11-27 16:49:33 +01:00
|
|
|
from fastapi.staticfiles import StaticFiles
|
2022-08-09 21:12:29 +02:00
|
|
|
from PyQt6 import QtWidgets
|
|
|
|
from PyQt6.QtCore import QThread
|
2021-08-17 12:59:58 +02:00
|
|
|
|
|
|
|
# Internal Packages
|
2022-08-06 02:20:04 +02:00
|
|
|
from src.configure import configure_server
|
2022-08-06 01:37:52 +02:00
|
|
|
from src.router import router
|
2022-08-06 02:05:35 +02:00
|
|
|
from src.utils import constants
|
2022-08-09 16:05:27 +02:00
|
|
|
from src.utils.cli import cli
|
2022-08-09 21:12:29 +02:00
|
|
|
from src.interface.desktop.configure_window import ConfigureWindow
|
|
|
|
from src.interface.desktop.system_tray import create_system_tray
|
2021-08-17 12:59:58 +02:00
|
|
|
|
2022-01-12 15:06:32 +01:00
|
|
|
|
2022-08-06 01:37:52 +02:00
|
|
|
# Initialize the Application Server
|
|
|
|
app = FastAPI()
|
|
|
|
app.mount("/static", StaticFiles(directory=constants.web_directory), name="static")
|
|
|
|
app.include_router(router)
|
2022-08-05 22:49:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
def run():
|
2022-08-09 16:05:27 +02:00
|
|
|
# Setup Base GUI
|
2022-08-05 22:49:48 +02:00
|
|
|
gui = QtWidgets.QApplication([])
|
|
|
|
gui.setQuitOnLastWindowClosed(False)
|
2022-08-06 14:18:28 +02:00
|
|
|
window = ConfigureWindow()
|
2022-08-09 16:05:27 +02:00
|
|
|
tray = create_system_tray(gui, window)
|
|
|
|
tray.show()
|
|
|
|
|
|
|
|
# Load config from CLI
|
|
|
|
args = cli(sys.argv[1:])
|
|
|
|
|
|
|
|
# Trigger First Run Experience, if required
|
|
|
|
if args.config is None:
|
|
|
|
window.show()
|
|
|
|
gui.exec()
|
|
|
|
|
|
|
|
# Reload config after first run
|
|
|
|
args = cli(sys.argv[1:])
|
|
|
|
# Quit if app still not configured
|
|
|
|
if args.config is None:
|
|
|
|
print('Exiting as Khoj is not configured. Configure the application to use it.')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Setup Application Server
|
|
|
|
host, port, socket = configure_server(args)
|
2022-08-05 22:49:48 +02:00
|
|
|
|
2021-08-16 02:50:08 +02:00
|
|
|
# Start Application Server
|
2022-08-05 22:49:48 +02:00
|
|
|
server = ServerThread(app, host, port, socket)
|
|
|
|
server.start()
|
|
|
|
gui.aboutToQuit.connect(server.terminate)
|
|
|
|
|
|
|
|
# Start the GUI
|
|
|
|
gui.exec()
|
|
|
|
|
|
|
|
|
2022-08-06 14:18:28 +02:00
|
|
|
class ServerThread(QThread):
|
2022-08-05 22:49:48 +02:00
|
|
|
def __init__(self, app, host=None, port=None, socket=None):
|
|
|
|
super(ServerThread, self).__init__()
|
|
|
|
self.app = app
|
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
self.socket = socket
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.wait()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
if self.socket:
|
|
|
|
uvicorn.run(app, proxy_headers=True, uds=self.socket)
|
|
|
|
else:
|
|
|
|
uvicorn.run(app, host=self.host, port=self.port)
|
|
|
|
|
|
|
|
|
2022-08-02 19:13:14 +02:00
|
|
|
if __name__ == '__main__':
|
2022-08-09 15:53:07 +02:00
|
|
|
run()
|