fix trayicon

This commit is contained in:
jxxghp 2023-10-10 14:38:50 +08:00
parent 91085d13a3
commit 375e16e0dc
2 changed files with 29 additions and 14 deletions

View File

@ -25,6 +25,8 @@ class Settings(BaseSettings):
HOST: str = "0.0.0.0" HOST: str = "0.0.0.0"
# API监听端口 # API监听端口
PORT: int = 3001 PORT: int = 3001
# 前端监听端口
NGINX_PORT: int = 3000
# 是否调试模式 # 是否调试模式
DEBUG: bool = False DEBUG: bool = False
# 是否开发模式 # 是否开发模式

View File

@ -1,8 +1,10 @@
import multiprocessing import multiprocessing
import threading
from pathlib import Path from pathlib import Path
import pystray import pystray
import uvicorn as uvicorn import uvicorn as uvicorn
from PIL import Image
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from uvicorn import Config from uvicorn import Config
@ -31,6 +33,11 @@ App.add_middleware(
) )
# uvicorn服务
Server = uvicorn.Server(Config(App, host=settings.HOST, port=settings.PORT,
reload=settings.DEV, workers=multiprocessing.cpu_count()))
def init_routers(): def init_routers():
""" """
初始化路由 初始化路由
@ -71,14 +78,6 @@ def stop_frontend():
subprocess.Popen(f"killall nginx", shell=True) subprocess.Popen(f"killall nginx", shell=True)
def open_web():
"""
调用浏览器打开前端页面
"""
import webbrowser
webbrowser.open(f"http://{settings.HOST}:{settings.PORT}")
@App.on_event("shutdown") @App.on_event("shutdown")
def shutdown_server(): def shutdown_server():
""" """
@ -121,14 +120,26 @@ def start_module():
start_frontend() start_frontend()
# uvicorn服务 def open_web():
Server = uvicorn.Server(Config(App, host=settings.HOST, port=settings.PORT, """
reload=settings.DEV, workers=multiprocessing.cpu_count())) 调用浏览器打开前端页面
"""
import webbrowser
webbrowser.open(f"http://localhost:{settings.NGINX_PORT}")
def quit_app():
"""
退出程序
"""
TrayIcon.stop()
Server.should_exit = True
# 托盘图标 # 托盘图标
TrayIcon = pystray.Icon( TrayIcon = pystray.Icon(
settings.PROJECT_NAME, settings.PROJECT_NAME,
icon=settings.ROOT_PATH / 'app.ico', icon=Image.open(settings.ROOT_PATH / 'app.ico'),
menu=pystray.Menu( menu=pystray.Menu(
pystray.MenuItem( pystray.MenuItem(
'打开', '打开',
@ -136,7 +147,7 @@ TrayIcon = pystray.Icon(
), ),
pystray.MenuItem( pystray.MenuItem(
'退出', '退出',
shutdown_server, quit_app,
) )
) )
) )
@ -146,5 +157,7 @@ if __name__ == '__main__':
init_db() init_db()
# 更新数据库 # 更新数据库
update_db() update_db()
# 启动服务 # 启动托盘
threading.Thread(target=TrayIcon.run, daemon=True).start()
# 启动API服务
Server.run() Server.run()