添加plugin与配置热重载
This commit is contained in:
parent
5ff223f655
commit
987cd9ec9e
|
@ -2,13 +2,20 @@ from fastapi import FastAPI, Response, Cookie
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import json
|
import json
|
||||||
from . import db
|
from server import db
|
||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
from .cfg import config
|
from server import cfg
|
||||||
|
|
||||||
|
|
||||||
FL_SERVER = config["flarum"]["fl_server"]
|
FL_SERVER = cfg.config["flarum"]["fl_server"]
|
||||||
FL_APIKEY = config["flarum"]["fl_apikey"]
|
FL_APIKEY = cfg.config["flarum"]["fl_apikey"]
|
||||||
|
|
||||||
|
|
||||||
|
async def reload():
|
||||||
|
global FL_SERVER
|
||||||
|
global FL_APIKEY
|
||||||
|
FL_SERVER = cfg.config["flarum"]["fl_server"]
|
||||||
|
FL_APIKEY = cfg.config["flarum"]["fl_apikey"]
|
||||||
|
|
||||||
|
|
||||||
def main(app: FastAPI, ROOT: str, apikeys: dict):
|
def main(app: FastAPI, ROOT: str, apikeys: dict):
|
|
@ -2,3 +2,9 @@ import tomllib
|
||||||
|
|
||||||
with open("config.toml", "rb") as f:
|
with open("config.toml", "rb") as f:
|
||||||
config = tomllib.load(f)
|
config = tomllib.load(f)
|
||||||
|
|
||||||
|
|
||||||
|
def reload():
|
||||||
|
global config
|
||||||
|
with open("config.toml", "rb") as f:
|
||||||
|
config = tomllib.load(f)
|
||||||
|
|
|
@ -1,14 +1,21 @@
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
from email.utils import parseaddr, formataddr
|
|
||||||
from email.header import Header
|
from email.header import Header
|
||||||
import aiosmtplib
|
import aiosmtplib
|
||||||
from .cfg import config
|
from . import cfg
|
||||||
|
|
||||||
|
|
||||||
SMTP_SRV = config["email"]["smtp_srv"]
|
def load_cfg():
|
||||||
PORT = config["email"]["port"]
|
global SMTP_SRV
|
||||||
ADDR = config["email"]["addr"]
|
global PORT
|
||||||
PASSWD = config["email"]["passwd"]
|
global ADDR
|
||||||
|
global PASSWD
|
||||||
|
SMTP_SRV = cfg.config["email"]["smtp_srv"]
|
||||||
|
PORT = cfg.config["email"]["port"]
|
||||||
|
ADDR = cfg.config["email"]["addr"]
|
||||||
|
PASSWD = cfg.config["email"]["passwd"]
|
||||||
|
|
||||||
|
|
||||||
|
load_cfg()
|
||||||
|
|
||||||
|
|
||||||
async def sendemail(to_addr, links):
|
async def sendemail(to_addr, links):
|
||||||
|
|
|
@ -6,24 +6,39 @@ from datetime import timedelta, datetime
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from . import db
|
from . import db
|
||||||
from . import email as email_sys
|
from . import email as email_sys
|
||||||
from . import reg
|
from . import cfg
|
||||||
import hashlib
|
import hashlib
|
||||||
import uuid
|
import uuid
|
||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
import re
|
import re
|
||||||
import asyncio
|
import asyncio
|
||||||
import uvicorn
|
import uvicorn
|
||||||
from .cfg import config
|
from . import cfg
|
||||||
import pygtrie
|
import pygtrie
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import importlib
|
||||||
|
|
||||||
ALGORITHM = config["common"]["algorithm"]
|
|
||||||
ACCESS_TOKEN_EXPIRE_MINUTES = config["common"]["access_token_expire_minutes"]
|
|
||||||
ACCESS_EMAIL_EXPIRE_MINUTES = config["common"]["access_email_expire_minutes"]
|
|
||||||
ROOT = config["common"]["root"]
|
|
||||||
CLEAN_TIMEOUT = config["common"]["clean_timeout"]
|
|
||||||
MANAGE_KEY = config["common"]["manage_key"]
|
|
||||||
REDIRECT_URL_WHITELIST = config["common"]["redirect_url_whitelist"]
|
|
||||||
|
|
||||||
|
def load_cfg():
|
||||||
|
global ALGORITHM
|
||||||
|
global ACCESS_TOKEN_EXPIRE_MINUTES
|
||||||
|
global ACCESS_EMAIL_EXPIRE_MINUTES
|
||||||
|
global ROOT
|
||||||
|
global CLEAN_TIMEOUT
|
||||||
|
global MANAGE_KEY
|
||||||
|
global REDIRECT_URL_WHITELIST
|
||||||
|
|
||||||
|
ALGORITHM = cfg.config["common"]["algorithm"]
|
||||||
|
ACCESS_TOKEN_EXPIRE_MINUTES = cfg.config["common"]["access_token_expire_minutes"]
|
||||||
|
ACCESS_EMAIL_EXPIRE_MINUTES = cfg.config["common"]["access_email_expire_minutes"]
|
||||||
|
ROOT = cfg.config["common"]["root"]
|
||||||
|
CLEAN_TIMEOUT = cfg.config["common"]["clean_timeout"]
|
||||||
|
MANAGE_KEY = cfg.config["common"]["manage_key"]
|
||||||
|
REDIRECT_URL_WHITELIST = cfg.config["common"]["redirect_url_whitelist"]
|
||||||
|
|
||||||
|
|
||||||
|
load_cfg()
|
||||||
|
|
||||||
tokens = pygtrie.StringTrie()
|
tokens = pygtrie.StringTrie()
|
||||||
apikeys = pygtrie.StringTrie()
|
apikeys = pygtrie.StringTrie()
|
||||||
|
@ -42,6 +57,7 @@ def clean_uuid(uuid: str):
|
||||||
async def clean_sys():
|
async def clean_sys():
|
||||||
while 1:
|
while 1:
|
||||||
await asyncio.sleep(CLEAN_TIMEOUT)
|
await asyncio.sleep(CLEAN_TIMEOUT)
|
||||||
|
sys.stderr.write("==> clean\n")
|
||||||
for k, v in tokens.items():
|
for k, v in tokens.items():
|
||||||
if (v[1] < datetime.now()):
|
if (v[1] < datetime.now()):
|
||||||
del tokens[k]
|
del tokens[k]
|
||||||
|
@ -279,7 +295,36 @@ async def init(key: str):
|
||||||
return 1
|
return 1
|
||||||
await db.create_db()
|
await db.create_db()
|
||||||
return 0
|
return 0
|
||||||
reg.main(app, ROOT, apikeys)
|
|
||||||
|
plugins = []
|
||||||
|
|
||||||
|
|
||||||
|
async def reload_cfg():
|
||||||
|
global plugins
|
||||||
|
cfg.reload()
|
||||||
|
load_cfg()
|
||||||
|
email_sys.load_cfg()
|
||||||
|
for i in plugins:
|
||||||
|
await i.reload()
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/manager/reload")
|
||||||
|
async def reload(key: str):
|
||||||
|
sys.stderr.write("==> reload config\n")
|
||||||
|
if (key != MANAGE_KEY):
|
||||||
|
return 1
|
||||||
|
await reload_cfg()
|
||||||
|
return 0
|
||||||
|
|
||||||
|
for i in os.listdir("plugin"):
|
||||||
|
if (len(i.split(".")) != 2 or i.split(".")[1] != 'py'):
|
||||||
|
continue
|
||||||
|
if (i.split(".")[0] not in cfg.config):
|
||||||
|
sys.stderr.write("--> disable "+i.split(".")[0]+"\n")
|
||||||
|
continue
|
||||||
|
sys.stderr.write("==> load "+i.split(".")[0]+"\n")
|
||||||
|
plugins.append(importlib.import_module("plugin."+i.split(".")[0]))
|
||||||
|
plugins[-1].main(app, ROOT, apikeys)
|
||||||
|
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
|
|
Loading…
Reference in New Issue