Compare commits
3 Commits
666854a198
...
eb10f28d95
| Author | SHA1 | Date | |
|---|---|---|---|
| eb10f28d95 | |||
| 375831185b | |||
| 2881257d09 |
@ -9,7 +9,7 @@ from appPublic.folderUtils import ProgramPath
|
||||
from appPublic.dictObject import DictObject
|
||||
from appPublic.jsonConfig import getConfig
|
||||
from appPublic.log import info, debug, warning, error, critical, exception
|
||||
from appPublic.registerfunction import RegisterCoroutine
|
||||
from appPublic.registerfunction import RegisterFunction
|
||||
|
||||
from sqlor.dbpools import DBPools
|
||||
|
||||
@ -24,7 +24,8 @@ from .real_ip import real_ip_middleware
|
||||
|
||||
class AHApp(web.Application):
|
||||
def __init__(self, *args, **kw):
|
||||
kw['client_max_size'] = 1024000000
|
||||
if not kw.get('client_max_size'):
|
||||
kw['client_max_size'] = 1024000000
|
||||
super().__init__(*args, **kw)
|
||||
self.user_data = DictObject()
|
||||
self.middlewares.insert(0, real_ip_middleware())
|
||||
@ -36,7 +37,7 @@ class AHApp(web.Application):
|
||||
return self.user_data.get(k)
|
||||
|
||||
class ConfiguredServer:
|
||||
def __init__(self, auth_klass=AuthAPI, workdir=None):
|
||||
def __init__(self, auth_klass=AuthAPI, workdir=None, app=None):
|
||||
self.auth_klass = auth_klass
|
||||
self.workdir = workdir
|
||||
if self.workdir is not None:
|
||||
@ -54,13 +55,16 @@ class ConfiguredServer:
|
||||
if config.website.client_max_size:
|
||||
client_max_size = config.website.client_max_size
|
||||
|
||||
self.app = AHApp(client_max_size=client_max_size)
|
||||
if app:
|
||||
self.app = app
|
||||
else:
|
||||
self.app = AHApp(client_max_size=client_max_size)
|
||||
load_plugins(self.workdir)
|
||||
g = ServerEnv()
|
||||
g.workdir = workdir
|
||||
|
||||
async def build_app(self):
|
||||
rf = RegisterCoroutine()
|
||||
rf = RegisterFunction()
|
||||
await rf.exe('ahapp_built', self.app)
|
||||
auth = self.auth_klass()
|
||||
await auth.setupAuth(self.app)
|
||||
@ -88,7 +92,7 @@ class ConfiguredServer:
|
||||
|
||||
def configPath(self,config):
|
||||
for p,prefix in config.website.paths:
|
||||
res = ProcessorResource(prefix,p,show_index=True,
|
||||
res = ProcessorResource(prefix, p, show_index=True,
|
||||
follow_symlinks=True,
|
||||
indexes=config.website.indexes,
|
||||
processors=config.website.processors)
|
||||
|
||||
@ -75,7 +75,7 @@ def i18nDICT(request):
|
||||
return json.dumps(i18n.getLangDict(l)).encode(c.website.coding)
|
||||
|
||||
class ProcessorResource(StaticResource,Url2File):
|
||||
def __init__(self, prefix: str, directory: PathLike,
|
||||
def __init__(self, prefix: str, directory: str,
|
||||
*, name: Optional[str]=None,
|
||||
expect_handler: Optional[_ExpectHandler]=None,
|
||||
chunk_size: int=256 * 1024,
|
||||
@ -83,14 +83,14 @@ class ProcessorResource(StaticResource,Url2File):
|
||||
append_version: bool=False,
|
||||
indexes:list=[],
|
||||
processors:dict={}) -> None:
|
||||
StaticResource.__init__(self,prefix, directory,
|
||||
StaticResource.__init__(self, prefix, directory,
|
||||
name=name,
|
||||
expect_handler=expect_handler,
|
||||
chunk_size=chunk_size,
|
||||
show_index=show_index,
|
||||
follow_symlinks=follow_symlinks,
|
||||
append_version=append_version)
|
||||
Url2File.__init__(self,directory,prefix,indexes,inherit=True)
|
||||
Url2File.__init__(self, directory, prefix, indexes, inherit=True)
|
||||
gr = self._routes.get('GET')
|
||||
self._routes.update({'POST':gr})
|
||||
self._routes.update({'PUT':gr})
|
||||
|
||||
@ -21,6 +21,19 @@ class Url2File:
|
||||
break
|
||||
return '/'.join(items)
|
||||
|
||||
def get_ospath_by_webpath(self, path:str) -> str:
|
||||
paths = url.split('/')
|
||||
f = os.path.join(self.rootpath, *paths)
|
||||
if not os.path.exists(f):
|
||||
return None
|
||||
if os.path.isdir(f):
|
||||
for idx in self.indexes:
|
||||
p = os.path.join(real_path,idx)
|
||||
if os.path.isfile(p):
|
||||
return p
|
||||
return None
|
||||
return f
|
||||
|
||||
def url2ospath(self, url: str) -> str:
|
||||
url = url.split('?')[0]
|
||||
if len(url) > 0 and url[-1] == '/':
|
||||
|
||||
@ -7,16 +7,16 @@ from ahserver.configuredServer import ConfiguredServer
|
||||
from ahserver.serverenv import ServerEnv
|
||||
from appPublic.jsonConfig import getConfig
|
||||
|
||||
def webapp(init_func):
|
||||
def webapp(init_func, app=None):
|
||||
parser = argparse.ArgumentParser(prog="Sage")
|
||||
parser.add_argument('-w', '--workdir')
|
||||
parser.add_argument('-p', '--port')
|
||||
args = parser.parse_args()
|
||||
workdir = args.workdir or os.getcwd()
|
||||
port = args.port
|
||||
webserver(init_func, workdir, port)
|
||||
webserver(init_func, workdir, port=port, app=app)
|
||||
|
||||
def webserver(init_func, workdir, port=None):
|
||||
def webserver(init_func, workdir, port=None, app=None):
|
||||
p = ProgramPath()
|
||||
config = getConfig(workdir, NS={'workdir':workdir, 'ProgramPath':p})
|
||||
if config.logger:
|
||||
@ -29,7 +29,7 @@ def webserver(init_func, workdir, port=None):
|
||||
se = ServerEnv()
|
||||
se.workdir = workdir
|
||||
se.port = port
|
||||
server = ConfiguredServer(workdir=workdir)
|
||||
server = ConfiguredServer(workdir=workdir, app=app)
|
||||
port = port or config.website.port or 8080
|
||||
port = int(port)
|
||||
server.run(port=port)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user