This commit is contained in:
yumoqing 2024-07-10 11:17:31 +08:00
parent 4bc140bafe
commit 4e66ec52c3
6 changed files with 32 additions and 32 deletions

View File

@ -16,7 +16,7 @@ from aiohttp_session.redis_storage import RedisStorage
from appPublic.jsonConfig import getConfig from appPublic.jsonConfig import getConfig
from appPublic.rsawrap import RSA from appPublic.rsawrap import RSA
from appPublic.app_logger import AppLogger from appPublic.log import info, debug, warning, error, critical, exception
def get_client_ip(obj, request): def get_client_ip(obj, request):
ip = request.headers.get('X-Forwarded-For') ip = request.headers.get('X-Forwarded-For')
@ -58,9 +58,8 @@ class MyRedisStorage(RedisStorage):
ex=session.max_age, ex=session.max_age,
) )
class AuthAPI(AppLogger): class AuthAPI:
def __init__(self): def __init__(self):
super().__init__()
self.conf = getConfig() self.conf = getConfig()
async def checkUserPermission(self, user, path): async def checkUserPermission(self, user, path):
@ -125,7 +124,7 @@ class AuthAPI(AppLogger):
@web.middleware @web.middleware
async def checkAuth(self,request,handler): async def checkAuth(self,request,handler):
self.info(f'checkAuth() called ... {request.path=}') info(f'checkAuth() called ... {request.path=}')
t1 = time.time() t1 = time.time()
path = request.path path = request.path
user = await auth.get_auth(request) user = await auth.get_auth(request)
@ -136,17 +135,17 @@ class AuthAPI(AppLogger):
try: try:
ret = await handler(request) ret = await handler(request)
t3 = time.time() t3 = time.time()
self.info(f'timecost=client({ip}) {user} access {path} cost {t3-t1}, ({t2-t1})') info(f'timecost=client({ip}) {user} access {path} cost {t3-t1}, ({t2-t1})')
return ret return ret
except Exception as e: except Exception as e:
t3 = time.time() t3 = time.time()
self.info(f'Exception=client({ip}) {user} access {path} cost {t3-t1}, ({t2-t1}), except={e}') info(f'Exception=client({ip}) {user} access {path} cost {t3-t1}, ({t2-t1}), except={e}')
raise e raise e
if user is None: if user is None:
self.info(f'timecost=client({ip}) {user} access need login to access {path} ({t2-t1})') info(f'timecost=client({ip}) {user} access need login to access {path} ({t2-t1})')
raise web.HTTPUnauthorized raise web.HTTPUnauthorized
self.info(f'timecost=client({ip}) {user} access {path} forbidden ({t2-t1})') info(f'timecost=client({ip}) {user} access {path} forbidden ({t2-t1})')
raise web.HTTPForbidden() raise web.HTTPForbidden()
async def needAuth(self,path): async def needAuth(self,path):

View File

@ -9,7 +9,7 @@ from aiohttp.web_response import Response, StreamResponse
from appPublic.jsonConfig import getConfig from appPublic.jsonConfig import getConfig
from appPublic.dictObject import DictObject from appPublic.dictObject import DictObject
from appPublic.folderUtils import listFile from appPublic.folderUtils import listFile
from appPublic.app_logger import AppLogger from appPublic.log import info, debug, warning, error, critical, exception
from .utils import unicode_escape from .utils import unicode_escape
from .serverenv import ServerEnv from .serverenv import ServerEnv
@ -41,13 +41,12 @@ class ObjectCache:
class BaseProcessor(AppLogger): class BaseProcessor:
@classmethod @classmethod
def isMe(self,name): def isMe(self,name):
return name=='base' return name=='base'
def __init__(self,path,resource): def __init__(self,path,resource):
super().__init__()
self.env_set = False self.env_set = False
self.path = path self.path = path
self.resource = resource self.resource = resource
@ -135,7 +134,7 @@ class BaseProcessor(AppLogger):
return resp return resp
async def datahandle(self,request): async def datahandle(self,request):
self.debug('*******Error*************') debug('*******Error*************')
self.content='' self.content=''
def setheaders(self): def setheaders(self):

View File

@ -8,7 +8,7 @@ from aiohttp import web
from appPublic.folderUtils import ProgramPath from appPublic.folderUtils import ProgramPath
from appPublic.dictObject import DictObject from appPublic.dictObject import DictObject
from appPublic.jsonConfig import getConfig from appPublic.jsonConfig import getConfig
from appPublic.app_logger import AppLogger from appPublic.log import info, debug, warning, error, critical, exception
from sqlor.dbpools import DBPools from sqlor.dbpools import DBPools
@ -30,11 +30,10 @@ class AHApp(web.Application):
def get_data(self, k): def get_data(self, k):
return self.data.get(k, DictObject()) return self.data.get(k, DictObject())
class ConfiguredServer(AppLogger): class ConfiguredServer:
def __init__(self, auth_klass=AuthAPI, workdir=None): def __init__(self, auth_klass=AuthAPI, workdir=None):
self.auth_klass = auth_klass self.auth_klass = auth_klass
self.workdir = workdir self.workdir = workdir
super().__init__()
if self.workdir is not None: if self.workdir is not None:
pp = ProgramPath() pp = ProgramPath()
config = getConfig(self.workdir, config = getConfig(self.workdir,

View File

@ -20,7 +20,7 @@ from sqlor.crud import CRUD
from appPublic.dictObject import multiDict2Dict from appPublic.dictObject import multiDict2Dict
from appPublic.jsonConfig import getConfig from appPublic.jsonConfig import getConfig
from appPublic.app_logger import AppLogger from appPublic.log import info, debug, warning, error, critical, exception
from .error import Error,Success from .error import Error,Success
actions = [ actions = [
@ -30,19 +30,19 @@ actions = [
"filter" "filter"
] ]
class DBAdmin(AppLogger): class DBAdmin:
def __init__(self, request,dbname,tablename, action): def __init__(self, request,dbname,tablename, action):
self.dbname = dbname self.dbname = dbname
self.tablename = tablename self.tablename = tablename
self.request = request self.request = request
self.action = action self.action = action
if action not in actions: if action not in actions:
self.debug('action not defined:%s' % action) debug('action not defined:%s' % action)
raise HTTPNotFound raise HTTPNotFound
try: try:
self.crud = CRUD(dbname,tablename) self.crud = CRUD(dbname,tablename)
except Exception as e: except Exception as e:
self.info('e= %s' % e) exception('e= %s' % e)
traceback.print_exc() traceback.print_exc()
raise HTTPNotFound raise HTTPNotFound
@ -51,7 +51,7 @@ class DBAdmin(AppLogger):
d = await self.crud.I() d = await self.crud.I()
return json_response(Success(d)) return json_response(Success(d))
except Exception as e: except Exception as e:
self.debug('except=%s' % e) exception('except=%s' % e)
traceback.print_exc() traceback.print_exc()
return json_response(Error(errno='metaerror',msg='get metadata error')) return json_response(Error(errno='metaerror',msg='get metadata error'))

View File

@ -38,7 +38,7 @@ from appPublic.i18n import getI18N
from appPublic.dictObject import DictObject, multiDict2Dict from appPublic.dictObject import DictObject, multiDict2Dict
from appPublic.timecost import TimeCost from appPublic.timecost import TimeCost
from appPublic.timeUtils import timestampstr from appPublic.timeUtils import timestampstr
from appPublic.app_logger import AppLogger from appPublic.log import info, debug, warning, error, critical, exception
from .baseProcessor import getProcessor, BricksUIProcessor, TemplateProcessor from .baseProcessor import getProcessor, BricksUIProcessor, TemplateProcessor
from .baseProcessor import PythonScriptProcessor, MarkdownProcessor from .baseProcessor import PythonScriptProcessor, MarkdownProcessor
@ -72,7 +72,7 @@ def i18nDICT(request):
return json.dumps(i18n.getLangDict(l)).encode(c.website.coding) return json.dumps(i18n.getLangDict(l)).encode(c.website.coding)
class ProcessorResource(AppLogger, StaticResource,Url2File): class ProcessorResource(StaticResource,Url2File):
def __init__(self, prefix: str, directory: PathLike, def __init__(self, prefix: str, directory: PathLike,
*, name: Optional[str]=None, *, name: Optional[str]=None,
expect_handler: Optional[_ExpectHandler]=None, expect_handler: Optional[_ExpectHandler]=None,
@ -81,7 +81,6 @@ class ProcessorResource(AppLogger, StaticResource,Url2File):
append_version: bool=False, append_version: bool=False,
indexes:list=[], indexes:list=[],
processors:dict={}) -> None: processors:dict={}) -> None:
AppLogger.__init__(self)
StaticResource.__init__(self,prefix, directory, StaticResource.__init__(self,prefix, directory,
name=name, name=name,
expect_handler=expect_handler, expect_handler=expect_handler,
@ -263,9 +262,12 @@ class ProcessorResource(AppLogger, StaticResource,Url2File):
self.y_env.i18n = serveri18n self.y_env.i18n = serveri18n
self.y_env.file_realpath = file_realpath self.y_env.file_realpath = file_realpath
self.y_env.redirect = redirect self.y_env.redirect = redirect
self.y_env.info = self.info self.y_env.info = info
self.y_env.error = self.error self.y_env.error = error
self.y_env.debug = self.debug self.y_env.debug = debug
self.y_env.warning = warning
self.y_env.critical = critical
self.y_env.exception = exception
self.y_env.remember_user = remember_user self.y_env.remember_user = remember_user
self.y_env.forget_user = forget_user self.y_env.forget_user = forget_user
self.y_env.get_user = get_user self.y_env.get_user = get_user
@ -289,7 +291,7 @@ class ProcessorResource(AppLogger, StaticResource,Url2File):
if config.website.dbadm and path.startswith(config.website.dbadm): if config.website.dbadm and path.startswith(config.website.dbadm):
pp = path.split('/')[2:] pp = path.split('/')[2:]
if len(pp)<3: if len(pp)<3:
self.error('%s:not found' % str(request.url)) error('%s:not found' % str(request.url))
raise HTTPNotFound raise HTTPNotFound
dbname = pp[0] dbname = pp[0]
tablename = pp[1] tablename = pp[1]
@ -299,7 +301,7 @@ class ProcessorResource(AppLogger, StaticResource,Url2File):
if config.website.dbrest and path.startswith(config.website.dbrest): if config.website.dbrest and path.startswith(config.website.dbrest):
pp = path.split('/')[2:] pp = path.split('/')[2:]
if len(pp)<2: if len(pp)<2:
self.error('%s:not found' % str(request.url)) error('%s:not found' % str(request.url))
raise HTTPNotFound raise HTTPNotFound
dbname = pp[0] dbname = pp[0]
tablename = pp[1] tablename = pp[1]
@ -311,7 +313,7 @@ class ProcessorResource(AppLogger, StaticResource,Url2File):
if config.website.download and path.startswith(config.website.download): if config.website.download and path.startswith(config.website.download):
pp = path.split('/')[2:] pp = path.split('/')[2:]
if len(pp)<1: if len(pp)<1:
self.error('%s:not found' % str(request.url)) error('%s:not found' % str(request.url))
raise HTTPNotFound raise HTTPNotFound
dp = '/'.join(pp) dp = '/'.join(pp)
path = path_decode(dp) path = path_decode(dp)
@ -328,7 +330,7 @@ class ProcessorResource(AppLogger, StaticResource,Url2File):
if self.request_filename and os.path.isdir(self.request_filename): if self.request_filename and os.path.isdir(self.request_filename):
config = getConfig() config = getConfig()
if not config.website.allowListFolder: if not config.website.allowListFolder:
self.error('%s:not found' % str(request.url)) error('%s:not found' % str(request.url))
raise HTTPNotFound raise HTTPNotFound
# print(f'{self.request_filename=}, {str(request.url)=} handle as a normal file') # print(f'{self.request_filename=}, {str(request.url)=} handle as a normal file')
return await super()._handle(request) return await super()._handle(request)

View File

@ -1,4 +1,5 @@
import aiohttp import aiohttp
from appPublic.log import info, debug, warning, error, critical, exception
from aiohttp import web, BasicAuth from aiohttp import web, BasicAuth
from aiohttp import client from aiohttp import client
from .baseProcessor import * from .baseProcessor import *
@ -17,7 +18,7 @@ class ProxyProcessor(BaseProcessor):
te = self.run_ns['tmpl_engine'] te = self.run_ns['tmpl_engine']
txt = await te.render(url,**ns) txt = await te.render(url,**ns)
data = json.loads(txt) data = json.loads(txt)
self.debug('proxyProcessor: data=%s' % data) debug('proxyProcessor: data=%s' % data)
return data return data
async def datahandle(self,request): async def datahandle(self,request):
@ -44,7 +45,7 @@ class ProxyProcessor(BaseProcessor):
await self.retResponse.prepare(request) await self.retResponse.prepare(request)
async for chunk in res.content.iter_chunked(chunk_size): async for chunk in res.content.iter_chunked(chunk_size):
await self.retResponse.write(chunk) await self.retResponse.write(chunk)
self.debug('proxy: datahandle() finish') debug('proxy: datahandle() finish')
def setheaders(self): def setheaders(self):