Skip to content

Commit

Permalink
fix: update templates and dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryanthelol committed Feb 4, 2024
1 parent aa5df64 commit bcdfe21
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 38 deletions.
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pychassiscli"
version = "0.3.9"
version = "0.4.0"
description = "A lightweight Python distributed microservice command line tool"
authors = [
{ name = "BryantHe", email = "bryantsisu@qq.com" },
Expand All @@ -14,11 +14,11 @@ classifiers = [
"Topic :: Software Development :: Libraries :: Python Modules",
]
dependencies = [
"python-on-whales>=0.67.0",
"python-on-whales>=0.68.0",
"rich>=13.7.0",
"click>=8.1.7",
"mako>=1.3.0",
"pytest==7.4.3",
"mako>=1.3.2",
"pytest==8.0.0",
]
requires-python = ">=3.8"
readme = "README.md"
Expand Down
60 changes: 46 additions & 14 deletions src/pychassiscli/templates/apiflask/app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
import os

# from dotenv import load_dotenv
from apiflask import APIFlask
from chassis.flask_nameko import FlaskPooledClusterRpcProxy
from pychassislib.namekoproxy_pool import FlaskPooledServiceRpcProxy
from werkzeug.middleware.proxy_fix import ProxyFix

from app.config.config import Config
from app.util.common import basedir


rpc = FlaskPooledClusterRpcProxy()
rpc = FlaskPooledServiceRpcProxy()


def register_blueprints(apiflask_app):
from app.api.v1 import create_v1

apiflask_app.register_blueprint(create_v1(), url_prefix='/v1')
apiflask_app.register_blueprint(create_v1(), url_prefix='/xxx/v1')


def load_app_config(app):
"""
加载环境变量和配置类到app config
加载配置类到app config
"""
# 读取 .env
# load_dotenv(os.path.join(basedir, '.apiflask.env'))
# 读取配置类
app.config.from_object('app.config.config.Config')


Expand All @@ -32,21 +27,58 @@ def load_rpc_client(apiflask_app):
NAMEKO_AMQP_URI=str(Config.RABBITMQ_URI)
))
rpc.init_app(apiflask_app, extra_config={
'INITIAL_CONNECTIONS': 4,
'MAX_CONNECTIONS': 30,
'INITIAL_CONNECTIONS': 2,
'MAX_CONNECTIONS': 10,
'POOL_RECYCLE': 1800 # 30 分钟后过期所有已有链接
})


def set_security_schemes(app):
app.security_schemes = {
'AccessTokenAuth': {
'type': 'AccessToken',
'in': 'body',
'name': 'access token',
},
'SessionAuth': {
'type': 'Session',
'in': 'header',
'name': 'session',
}
}


def set_contact_info(app):
app.contact = {
'name': 'API Support',
'url': '',
'email': 'bryant@7-speed.com'
}


def set_doc_ui_cdn(app):
app.config['SWAGGER_UI_BUNDLE_JS'] = 'https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.11.1/swagger-ui-bundle.min.js'
app.config['SWAGGER_UI_CSS'] = 'https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.11.1/swagger-ui.min.css'
app.config['SWAGGER_UI_STANDALONE_PRESET_JS'] = 'https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.11.1/swagger-ui-standalone-preset.min.js'
app.config['REDOC_STANDALONE_JS'] = 'https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js'


def create_app():
# http wsgi server 托管启动需指定读取环境配置
# load_dotenv(os.path.join(basedir, '.apiflaskenv'))
app = APIFlask(__name__, title='Body Record API', version='1.0.0', docs_ui='redoc')
app.servers = [
{
'name': 'Production Server',
'url': 'https://www.bearcatlog.com/pzx/'
}
]
app = APIFlask(__name__, title='XXX API', version='1.0.0', docs_ui='redoc', docs_path='/xxx/docs',
spec_path='/xxx/openapi.json')
app.wsgi_app = ProxyFix(
app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1
)
set_doc_ui_cdn(app)
set_security_schemes(app)
set_contact_info(app)
load_app_config(app)
register_blueprints(app)
load_rpc_client(app)
Expand Down
20 changes: 16 additions & 4 deletions src/pychassiscli/templates/apiflask/docker-compose-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,30 @@ services:
- .apiflaskenv
- .apiflask.env
ports:
- "5000:5000"
- target: 5000
published: 6100
mode: host
deploy:
mode: replicated
replicas: 6
replicas: 2
resources:
reservations:
cpus: '0.05'
memory: 100M
limits:
cpus: '0.40'
memory: 300M
update_config:
parallelism: 3
parallelism: 1
failure_action: rollback
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
max_attempts: 1
window: 120s
placement:
constraints:
- 'node.role == worker'
networks:
- apiflask-api-network

Expand Down
2 changes: 1 addition & 1 deletion src/pychassiscli/templates/nameko/all_demo/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AMQP_URI: pyamqp://${RABBIT_USER:admin}:${RABBIT_PASSWORD:admin}@${RABBIT_HOST:localhost}:${RABBIT_PORT:5672}/
WEB_SERVER_ADDRESS: '0.0.0.0:8000'
# WEB_SERVER_ADDRESS: '0.0.0.0:8000'
RPC_EXCHANGE: 'nameko-rpc'

max_workers: 20
Expand Down
16 changes: 13 additions & 3 deletions src/pychassiscli/templates/nameko/docker-compose-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@ services:
- .nameko.env
deploy:
mode: replicated
replicas: 6
replicas: 2
resources:
reservations:
cpus: '0.05'
memory: 100M
limits:
cpus: '0.40'
memory: 500M
update_config:
parallelism: 3
parallelism: 1
failure_action: rollback
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
max_attempts: 1
window: 120s
placement:
constraints:
- 'node.role == worker'
networks:
- nameko-service

Expand Down
11 changes: 0 additions & 11 deletions src/pychassiscli/templates/nameko/pyproject.toml

This file was deleted.

1 change: 1 addition & 0 deletions src/pychassiscli/templates/nameko/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pychassislib[mongodb,nameko,ob,schema,metric]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3"
version: "3.8"

services:
rabbitmq:
Expand Down

0 comments on commit bcdfe21

Please sign in to comment.