Skip to content

Commit

Permalink
2025年01月03日 优化 Application
Browse files Browse the repository at this point in the history
  • Loading branch information
ss1917 committed Jan 3, 2025
1 parent b6ce756 commit 19bdc7e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 90 deletions.
5 changes: 3 additions & 2 deletions db_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Desc : 数据表生成
"""

import logging
from sqlalchemy import create_engine
from sqlalchemy.engine.url import URL
from websdk2.consts import const
Expand Down Expand Up @@ -34,9 +35,9 @@ def create():
try:
AuBase.metadata.create_all(engine)
AppsBase.metadata.create_all(engine)
print('[Success] 表结构创建成功!')
logging.info('[Success] 表结构创建成功!')
except Exception as err:
print(f'[Error] {err}!')
logging.error(f'[Error] {err}!')


def drop():
Expand Down
44 changes: 5 additions & 39 deletions get_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
date : 2017年11月15日
role : 权限同步和鉴定
"""

import datetime
import hashlib
import json
import logging
import time

import requests
from urllib3 import disable_warnings
from urllib3.exceptions import InsecureRequestWarning
from websdk2.db_context import DBContextV2 as DBContext
from libs.feature_model_utils import insert_or_update

from models.authority import Users
from settings import settings

try:
requests.packages.urllib3.disable_warnings()
except:
pass
disable_warnings(InsecureRequestWarning)


def get_all_user():
Expand All @@ -45,7 +45,7 @@ def md5hex(sign):
url = uc_conf['endpoint'] + "/api/all-users-4-outer"
response = requests.get(url=url, params=params)
res = response.json()
print(res.get('message'))
logging.info(res.get('message'))
return res.get('data')


Expand Down Expand Up @@ -88,37 +88,3 @@ def index():


sync_user_from_ucenter()

# dict1 = api_permissions()
# dict2 = api_permissions_v2()
# # 对比两字典的每一对键值对
# for key in dict1.keys():
# if key in dict2:
# if dict1[key] != dict2[key]:
# print(f"不一致Key: {key}, Dict1 Value: {dict1[key]}, Dict2 Value: {dict2[key]}")
# else:
# print(f"Key: {key}, Dict1 Value: {dict1[key]}, Dict2 Value: <not present>")
# #
# if dict1 == dict2:
# print("The JSON objects are equal")
# else:
# print("The JSON objects are different")
#
#
# def get_md5_hash(data):
# # 将字典转换为排序后的 JSON 字符串
# json_str = json.dumps(data, sort_keys=True)
# # 计算 JSON 字符串的 MD5 哈希值
# return hashlib.md5(json_str.encode()).hexdigest()
#
#
# hash1 = get_md5_hash(dict1)
# hash2 = get_md5_hash(dict2)
#
# print(f"Hash of dict1: {hash1}")
# print(f"Hash of dict2: {hash2}")
#
# if hash1 == hash2:
# print("dict1 and dict2 are equal")
# else:
# print("dict1 and dict2 are different")
103 changes: 56 additions & 47 deletions libs/feature_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,75 +8,84 @@

import asyncio
import logging
from abc import ABC
from shortuuid import uuid
from tornado import httpserver, ioloop
from tornado import options as tnd_options
from tornado.options import options, define
from tornado.web import Application as tornadoApp
from tornado.web import RequestHandler
from tornado import httpserver
from tornado.options import options, define, parse_command_line
from tornado.web import Application as TornadoApp, RequestHandler
from websdk2.configs import configs
from websdk2.logger import init_logging

define("addr", default='0.0.0.0', help="run on the given ip address", type=str)
define("port", default=8000, help="run on the given port", type=int)
define("progid", default=str(uuid()), help="tornado progress id", type=str)

init_logging()
urls_meta_list = []


class Application(tornadoApp):
class Application(TornadoApp):
""" 定制 Tornado Application 集成日志、sqlalchemy 等功能 """

def __init__(self, handlers=None, default_host="", transforms=None, **settings):
tnd_options.parse_command_line()
if configs.can_import: configs.import_dict(**settings)
handlers.extend([(r"/v1/probe/meta/urls/", MetaProbe), ])
self.urls_meta_handle(handlers)
max_buffer_size = configs.get('max_buffer_size')
max_body_size = configs.get('max_body_size')
super(Application, self).__init__(handlers, default_host, transforms, **configs)
self.http_server = httpserver.HTTPServer(self, max_buffer_size=max_buffer_size, max_body_size=max_body_size)
self.__options = options
# self.http_server.listen(options.port, address=options.addr)
# self.io_loop = ioloop.IOLoop.instance()
def __init__(self, handlers=None, **settings):
parse_command_line()
handlers = handlers or []
handlers.append((r"/v1/probe/meta/urls/", MetaProbe))

if configs.can_import:
configs.import_dict(**settings)

self._generate_url_metadata(handlers)

max_buffer_size = configs.get("max_buffer_size")
max_body_size = configs.get("max_body_size")
super().__init__(handlers, **configs)

self.http_server = httpserver.HTTPServer(
self, max_buffer_size=max_buffer_size, max_body_size=max_body_size
)

async def http_server_main(self):
self.http_server.listen(self.__options.port, address=self.__options.addr)
self.http_server.listen(options.port, address=options.addr)
logging.info(f"Server started on {options.addr}:{options.port} with process ID {options.progid}")
await asyncio.Event().wait()

def start_server(self):
"""
启动 tornado 服务
:return:
"""
"""Start Tornado server."""
try:
# init_logging()
logging.info('progressid: %(progid)s' % dict(progid=options.progid))
logging.info('server address: %(addr)s:%(port)d' % dict(addr=options.addr, port=options.port))
logging.info('web server start sucessfuled.')
# self.io_loop.start()
logging.info(f"Process ID: {options.progid}")
asyncio.run(self.http_server_main())
except KeyboardInterrupt:
pass
# self.io_loop.stop()
except:
import traceback
logging.error('%(tra)s' % dict(tra=traceback.format_exc()))

def urls_meta_handle(self, urls):
# 数据写入内存,启动的时候上报至权限管理
urls_meta_list.extend([{"url": u[0], "name": u[2].get('handle_name')[0:30] if u[2].get('handle_name') else "",
"method": u[2].get('method') if u[2].get('method') and len(
u[2].get('method')) < 100 else [],
"status": u[2].get('handle_status')[0:2] if u[2].get('handle_status') else "y"} if len(
u) > 2 else {"url": u[0], "name": "暂无", "status": "y"} for u in urls])


class MetaProbe(RequestHandler):
logging.info("Server shut down gracefully.")
except Exception as e:
logging.error(f"Unexpected error: {e}", exc_info=True)

@staticmethod
def _generate_url_metadata(urls):
"""Generate metadata for registered URLs."""
for url in urls:
meta = {
"url": url[0],
"name": url[2].get("handle_name", "暂无")[:30] if len(url) > 2 else "暂无",
"method": url[2].get("method", []) if len(url) > 2 else [],
"status": url[2].get("handle_status", "y")[:2] if len(url) > 2 else "y",
}
urls_meta_list.append(meta)


class MetaProbe(ABC, RequestHandler):
def head(self, *args, **kwargs):
self.write(dict(code=0, msg="Get success", count=len(urls_meta_list), data=urls_meta_list))
self._write_response()

def get(self, *args, **kwargs):
self.write(dict(code=0, msg="Get success", count=len(urls_meta_list), data=urls_meta_list))
self._write_response()

def _write_response(self):
self.write({
"code": 0,
"msg": "Get success",
"count": len(urls_meta_list),
"data": urls_meta_list,
})


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions mg/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

from abc import ABC
from tornado.ioloop import PeriodicCallback
from websdk2.application import Application as myApplication
# from libs.feature_application import Application as myApplication
# from websdk2.application import Application as myApplication
from libs.feature_application import Application as myApplication
from libs.sync_user_verift_v4 import async_api_permission_v4, async_user_center
from mg.handlers import urls
from mg.subscribe import RedisSubscriber as SubApp
Expand Down

0 comments on commit 19bdc7e

Please sign in to comment.