ahserver_g/ahserver/configuredServer.py

89 lines
2.4 KiB
Python
Raw Permalink Normal View History

2019-07-10 17:34:45 +08:00
import os,sys
2024-01-27 17:22:56 +08:00
from sys import platform
2021-06-24 11:01:37 +08:00
import time
2019-07-10 17:34:45 +08:00
import ssl
2021-05-17 10:58:51 +08:00
from socket import *
2019-07-10 17:34:45 +08:00
from aiohttp import web
from appPublic.folderUtils import ProgramPath
2024-04-09 18:10:52 +08:00
from appPublic.dictObject import DictObject
2019-07-10 17:34:45 +08:00
from appPublic.jsonConfig import getConfig
2022-06-23 14:58:03 +08:00
from appPublic.app_logger import AppLogger
2019-07-10 17:34:45 +08:00
from sqlor.dbpools import DBPools
from .processorResource import ProcessorResource
from .auth_api import AuthAPI
from .myTE import setupTemplateEngine
from .globalEnv import initEnv
2023-02-22 23:37:09 +08:00
from .filestorage import TmpFileRecord
2023-08-07 16:40:38 +08:00
from .loadplugins import load_plugins
2019-07-10 17:34:45 +08:00
2024-04-09 18:10:52 +08:00
class AHApp(web.Application):
def __init__(self, *args, **kw):
super().__init__(*args, **kw)
self.data = DictObject()
def set_data(self, k, v):
self.data[k] = v
def get_data(self, k):
return self.data.get(k, DictObject())
2022-06-23 14:58:03 +08:00
class ConfiguredServer(AppLogger):
2022-05-20 17:53:06 +08:00
def __init__(self, auth_klass=AuthAPI, workdir=None):
2023-07-18 18:25:59 +08:00
self.auth_klass = auth_klass
self.workdir = workdir
2022-06-23 14:58:03 +08:00
super().__init__()
2023-07-18 18:25:59 +08:00
if self.workdir is not None:
2022-06-23 15:33:34 +08:00
pp = ProgramPath()
2023-07-18 18:25:59 +08:00
config = getConfig(self.workdir,
{'workdir':self.workdir,'ProgramPath':pp})
2022-06-23 15:33:34 +08:00
else:
config = getConfig()
2019-07-10 17:34:45 +08:00
if config.databases:
DBPools(config.databases)
2023-07-18 18:30:09 +08:00
self.config = config
2019-07-10 17:34:45 +08:00
initEnv()
setupTemplateEngine()
2023-05-29 15:35:03 +08:00
client_max_size = 1024 * 10240
2023-05-29 20:26:06 +08:00
if config.website.client_max_size:
client_max_size = config.website.client_max_size
2023-05-29 20:35:23 +08:00
2023-08-07 16:40:38 +08:00
load_plugins(self.workdir)
2024-04-09 18:10:52 +08:00
self.app = AHApp(client_max_size=client_max_size)
2023-07-19 11:38:09 +08:00
async def init_auth(self):
2023-07-18 18:25:59 +08:00
auth = self.auth_klass()
2023-07-19 11:38:09 +08:00
await auth.setupAuth(self.app)
return self.app
2023-09-22 13:53:32 +08:00
def run(self, port=None):
2023-07-18 18:30:09 +08:00
config = getConfig()
2023-07-18 18:25:59 +08:00
self.configPath(config)
2023-02-22 23:37:09 +08:00
a = TmpFileRecord()
2019-07-10 17:34:45 +08:00
ssl_context = None
2023-09-22 13:53:32 +08:00
if port is None:
port = config.website.port or 8080
2019-07-10 17:34:45 +08:00
if config.website.ssl:
ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_context.load_cert_chain(config.website.ssl.crtfile,
config.website.ssl.keyfile)
2024-01-27 17:22:56 +08:00
reuse_port = None
if platform != 'win32':
2024-01-28 11:14:28 +08:00
reuse_port = True
2024-01-31 22:15:55 +08:00
print('reuse_port=', reuse_port)
2023-07-19 11:38:09 +08:00
web.run_app(self.init_auth(),host=config.website.host or '0.0.0.0',
2023-09-22 13:53:32 +08:00
port=port,
2024-01-27 17:22:56 +08:00
reuse_port=reuse_port,
2023-07-21 11:55:11 +08:00
ssl_context=ssl_context)
2019-07-10 17:34:45 +08:00
def configPath(self,config):
for p,prefix in config.website.paths:
res = ProcessorResource(prefix,p,show_index=True,
2019-12-03 11:30:15 +08:00
follow_symlinks=True,
2019-12-05 06:04:38 +08:00
indexes=config.website.indexes,
processors=config.website.processors)
2019-07-10 17:34:45 +08:00
self.app.router.register_resource(res)