From a8be26683a3969657c2b2643c494a9291ebbabca Mon Sep 17 00:00:00 2001 From: pengshiyu <1940607002@qq.com> Date: Sat, 3 Feb 2024 21:52:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E5=A4=87?= =?UTF-8?q?=E6=B3=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 +- Makefile | 4 +- doc/source/manual/install.md | 3 + domain_admin/main.py | 3 +- domain_admin/router/api_map.py | 4 +- domain_admin/utils/datetime_util.py | 8 +-- domain_admin/utils/open_api/aliyun_oss_api.py | 61 +++++++++++++++++++ requirements/production.txt | 2 + 8 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 domain_admin/utils/open_api/aliyun_oss_api.py diff --git a/Dockerfile b/Dockerfile index 133b3b3fcf..e8ef459088 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,4 +11,4 @@ RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && pip install --no-cache-dir -r requirements/production.txt \ && apk del .build-deps -CMD gunicorn --bind '0.0.0.0:8000' 'domain_admin.main:app' --timeout 120 +CMD gunicorn --bind '0.0.0.0:8000' --timeout 120 --worker-class gevent 'domain_admin.main:app' diff --git a/Makefile b/Makefile index 6aa4f30f21..592f13d3a3 100644 --- a/Makefile +++ b/Makefile @@ -3,11 +3,11 @@ # 运行开发环境 dev: - source venv/bin/activate && gunicorn --bind '0.0.0.0:5000' --reload 'domain_admin.main:app' --timeout 120 + source venv/bin/activate && gunicorn --bind '0.0.0.0:5000' --timeout 120 --worker-class gevent --reload 'domain_admin.main:app' # 运行生产环境 pro: - source venv/bin/activate && gunicorn --bind '0.0.0.0:8000' 'domain_admin.main:app' --timeout 120 + source venv/bin/activate && gunicorn --bind '0.0.0.0:8000' --worker-class gevent --timeout 120 'domain_admin.main:app' # 发布 make release .PHONY: release diff --git a/doc/source/manual/install.md b/doc/source/manual/install.md index bb9633f824..4cc6101383 100644 --- a/doc/source/manual/install.md +++ b/doc/source/manual/install.md @@ -42,6 +42,9 @@ $ pip install gunicorn domain-admin # 启动运行 $ gunicorn --bind '127.0.0.1:8000' 'domain_admin.main:app' + +# 开启异步处理,增加并发 +$ gunicorn --bind '127.0.0.1:8000' --worker-class gevent 'domain_admin.main:app' ``` windows 安装 diff --git a/domain_admin/main.py b/domain_admin/main.py index 00d5872a5c..1349b55e89 100644 --- a/domain_admin/main.py +++ b/domain_admin/main.py @@ -1,12 +1,12 @@ # -*- coding: utf-8 -*- from __future__ import print_function, unicode_literals, absolute_import, division - from flask import request, make_response, send_file from flask_cors import CORS from werkzeug.middleware.proxy_fix import ProxyFix from domain_admin import compat +from domain_admin.log import logger from domain_admin.config import TEMP_DIR from domain_admin.model.base_model import db from domain_admin.model.database import init_database @@ -83,6 +83,7 @@ def init_app(flask_app): :param flask_app: :return: """ + logger.info('init_app') # 注册路由 register.register_app_routers(flask_app, api_map.routes) diff --git a/domain_admin/router/api_map.py b/domain_admin/router/api_map.py index ba1d8302af..8db9e85ef8 100644 --- a/domain_admin/router/api_map.py +++ b/domain_admin/router/api_map.py @@ -8,7 +8,9 @@ whois_api, address_api, domain_info_api, prometheus_api, log_operation_api, group_user_api, - log_async_task_api, issue_certificate_api, host_api, monitor_api, log_monitor_api) + log_async_task_api, issue_certificate_api, + host_api, monitor_api, log_monitor_api +) from domain_admin.api import domain_api from domain_admin.api import group_api from domain_admin.api import auth_api diff --git a/domain_admin/utils/datetime_util.py b/domain_admin/utils/datetime_util.py index ad090a82e7..6f57e3d9cc 100644 --- a/domain_admin/utils/datetime_util.py +++ b/domain_admin/utils/datetime_util.py @@ -46,7 +46,7 @@ def get_timestamp_with_microsecond(datetime_obj): ref: https://stackoverflow.com/questions/50650704/attributeerror-datetime-datetime-object-has-no-attribute-timestamp :param datetime_obj: - :return: float + :return: int """ if sys.version_info[0] < 3 or sys.version_info[1] < 4: # python version < 3.3 @@ -207,16 +207,14 @@ def get_diff_time_with_microsecond(start_date, end_date): 获取两个时间对象的时间差秒数 :param start_date: :param end_date: - :return: + :return: int """ - print('get_diff_time_with_microsecond', start_date, end_date) - if start_date and end_date \ and isinstance(start_date, datetime) \ and isinstance(end_date, datetime): return get_timestamp_with_microsecond(end_date) - get_timestamp_with_microsecond(start_date) else: - return 0.0 + return 0 def is_less_than(source_date, target_date): diff --git a/domain_admin/utils/open_api/aliyun_oss_api.py b/domain_admin/utils/open_api/aliyun_oss_api.py new file mode 100644 index 0000000000..fb75667d5e --- /dev/null +++ b/domain_admin/utils/open_api/aliyun_oss_api.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- +""" +@File : aliyun_oss_api.py +@Date : 2024-02-02 +@Author : Peng Shiyu + +OSS Python SDK适用于Python 2.6、2.7、3.3、3.4、3.5、3.6、3.7、3.8及以上版本。 + +https://help.aliyun.com/zh/oss/developer-reference/map-custom-domain-names-4?spm=a2c4g.11186623.0.i6 +""" +import oss2 +from oss2.credentials import EnvironmentVariableCredentialsProvider, StaticCredentialsProvider + + +def create_bucket_cname_token(): + # 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。 + auth = oss2.ProviderAuth(StaticCredentialsProvider(access_key_id="", access_key_secret="", security_token="")) + + # yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。 + # 填写Bucket名称,例如examplebucket。 + bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket') + + # 填写自定义域名。 + test_domain = 'www.example.com' + # 创建CnameToken。 + result = bucket.create_bucket_cname_token(test_domain) + # 打印绑定的Cname名称。 + print(result.cname) + # 打印OSS返回的CnameToken。 + print(result.token) + # 打印CnameToken的过期时间。 + print(result.expire_time) + + +def put_bucket_cname(): + # 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。 + auth = oss2.ProviderAuth(StaticCredentialsProvider(access_key_id="", access_key_secret="", security_token="")) + + # yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。 + # 填写Bucket名称,例如examplebucket。 + bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket') + + # 填写自定义域名。 + test_domain = 'www.example.com' + # 填写旧版证书ID。 + previous_cert_id = '001' + certificate = '''-----BEGIN CERTIFICATE----- + MIIDWzCCAkOgA******KTgnwyOGU9cv+mxA= + -----END CERTIFICATE-----''' + # 设置证书私钥。 + private_key = '''-----BEGIN PRIVATE KEY----- + MIIEvQIBADAN******1i2t41Q/SC3HUGC5mJjpO8= + -----END PRIVATE KEY----- + ''' + + cert = oss2.models.CertInfo(certificate=certificate, private_key=private_key) + # 通过force=True设置强制覆盖旧版证书。 + # 通过delete_certificate选择是否删除证书。设置为delete_certificate=True表示删除证书,设置为delete_certificate=False表示不删除证书。 + # cert = oss2.models.CertInfo(certificate=certificate, private_key=private_key, force=True, delete_certificate=False) + input = oss2.models.PutBucketCnameRequest(test_domain, cert) + bucket.put_bucket_cname(input) diff --git a/requirements/production.txt b/requirements/production.txt index 5e7238c512..1c1c18f6f3 100644 --- a/requirements/production.txt +++ b/requirements/production.txt @@ -19,3 +19,5 @@ acme fabric dnspython openpyxl +psycopg2 +