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-10 23:10:21 +02:00
|
|
|
from src.utils import constants, state
|
2022-08-09 16:05:27 +02:00
|
|
|
from src.utils.cli import cli
|
2022-08-09 21:41:23 +02:00
|
|
|
from src.interface.desktop.configure_screen import ConfigureScreen
|
2022-08-09 21:12:29 +02:00
|
|
|
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-10 21:42:32 +02:00
|
|
|
# Load config from CLI
|
2022-08-10 23:10:21 +02:00
|
|
|
state.cli_args = sys.argv[1:]
|
|
|
|
args = cli(state.cli_args)
|
2022-08-10 21:42:32 +02:00
|
|
|
|
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-10 21:42:32 +02:00
|
|
|
configure_screen = ConfigureScreen(args.config_file)
|
2022-08-09 21:41:23 +02:00
|
|
|
tray = create_system_tray(gui, configure_screen)
|
2022-08-09 16:05:27 +02:00
|
|
|
tray.show()
|
|
|
|
|
|
|
|
# Trigger First Run Experience, if required
|
|
|
|
if args.config is None:
|
2022-08-09 21:41:23 +02:00
|
|
|
configure_screen.show()
|
2022-08-09 16:05:27 +02:00
|
|
|
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()
|