2019-07-10 17:34:45 +08:00
|
|
|
import os,sys
|
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
|
2021-05-17 10:58:51 +08:00
|
|
|
from appPublic.background import Background
|
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
|
|
|
|
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)
|
2023-05-29 15:35:03 +08:00
|
|
|
self.app = web.Application(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)
|
2021-05-17 10:58:51 +08:00
|
|
|
|
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,
|
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)
|
|
|
|
|