2021-08-17 12:59:58 +02:00
|
|
|
# Standard Packages
|
2022-08-12 23:43:53 +02:00
|
|
|
from platform import system
|
2022-08-12 20:02:18 +02:00
|
|
|
import signal
|
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
|
2022-08-12 20:02:18 +02:00
|
|
|
from PyQt6.QtCore import QThread, QTimer
|
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 23:13:14 +02:00
|
|
|
set_state(args)
|
2022-08-10 21:42:32 +02:00
|
|
|
|
2022-08-11 19:59:57 +02:00
|
|
|
if args.no_gui:
|
|
|
|
# Start Server
|
|
|
|
configure_server(args, required=True)
|
|
|
|
start_server(app, host=args.host, port=args.port, socket=args.socket)
|
|
|
|
else:
|
|
|
|
# Setup GUI
|
|
|
|
gui = QtWidgets.QApplication([])
|
|
|
|
configure_screen = ConfigureScreen(args.config_file)
|
2022-08-12 23:43:53 +02:00
|
|
|
|
|
|
|
# System tray is only available on Windows, MacOS.
|
|
|
|
# On Linux (Gnome) the System tray is not supported.
|
|
|
|
# Since only the Configure Window is available
|
|
|
|
# Quitting it should quit the application
|
|
|
|
if system() in ['Windows', 'Darwin']:
|
|
|
|
gui.setQuitOnLastWindowClosed(False)
|
|
|
|
tray = create_system_tray(gui, configure_screen)
|
|
|
|
tray.show()
|
2022-08-11 19:59:57 +02:00
|
|
|
|
|
|
|
# Setup Server
|
|
|
|
configure_server(args, required=False)
|
|
|
|
server = ServerThread(app, args.host, args.port, args.socket)
|
|
|
|
|
2022-08-12 23:43:53 +02:00
|
|
|
# Show Configure Screen on Linux (etc.) or First Run Experience
|
|
|
|
if args.config is None or system() not in ['Windows', 'Darwin']:
|
2022-08-11 19:59:57 +02:00
|
|
|
configure_screen.show()
|
|
|
|
|
2022-08-12 20:02:18 +02:00
|
|
|
# Setup Signal Handlers
|
|
|
|
signal.signal(signal.SIGINT, sigint_handler)
|
|
|
|
# Invoke python Interpreter every 500ms to handle signals
|
|
|
|
timer = QTimer()
|
|
|
|
timer.start(500)
|
|
|
|
timer.timeout.connect(lambda: None)
|
|
|
|
|
2022-08-11 19:59:57 +02:00
|
|
|
# Start Application
|
|
|
|
server.start()
|
|
|
|
gui.aboutToQuit.connect(server.terminate)
|
|
|
|
gui.exec()
|
2022-08-05 22:49:48 +02:00
|
|
|
|
|
|
|
|
2022-08-12 20:02:18 +02:00
|
|
|
def sigint_handler(*args):
|
|
|
|
print("\nShutting down Khoj...")
|
|
|
|
QtWidgets.QApplication.quit()
|
|
|
|
|
|
|
|
|
2022-08-10 23:13:14 +02:00
|
|
|
def set_state(args):
|
|
|
|
state.config_file = args.config_file
|
|
|
|
state.config = args.config
|
|
|
|
state.verbose = args.verbose
|
|
|
|
|
|
|
|
|
2022-08-11 19:59:57 +02:00
|
|
|
def start_server(app, host=None, port=None, socket=None):
|
|
|
|
if socket:
|
|
|
|
uvicorn.run(app, proxy_headers=True, uds=socket)
|
|
|
|
else:
|
|
|
|
uvicorn.run(app, host=host, port=port)
|
|
|
|
|
|
|
|
|
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):
|
2022-08-11 19:59:57 +02:00
|
|
|
start_server(self.app, self.host, self.port, self.socket)
|
2022-08-05 22:49:48 +02:00
|
|
|
|
|
|
|
|
2022-08-02 19:13:14 +02:00
|
|
|
if __name__ == '__main__':
|
2022-08-09 15:53:07 +02:00
|
|
|
run()
|