flask根据装饰器@application.after_request对错误码进行返回处理
# coding=utf-8 import os import json import connexion from flask import jsonify app = connexion.FlaskApp(__name__, specification_dir='.') application = app.app # 测试配置 # application.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqlconnector://root:apptestxwk@localhost:3306/apptest' # application.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # application.config['REDIS_URL'] = 'redis://:apptestxwk@localhost:6379/0' # application.config['MONGO_URI'] = 'mongodb://root:root@localhost:27017/apptest' # application.config['MONGODB_SETTINGS'] = { # 'db': 'apptest', # 'host': 'mongodb://root:root@localhost:27017' # } # application.config['ES_HOSTS'] = ['localhost:9200/'] # # application.config['CELERY_BROKER_URL'] = 'redis://:apptestxwk@localhost:6379/1' # application.config['CELERY_RESULT_BACKEND'] = 'redis://:apptestxwk@localhost:6379/2' # 生产环境数据库 application.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqlconnector://dba:xxxx@rm-xxxx.mysql.rds.aliyuncs.com:3306/app' # application.config['SQLALCHEMY_ECHO'] = True # application.config['SQLALCHEMY_POOL_RECYCLE'] = 280 # application.config['SQLALCHEMY_POOL_SIZE'] = 20 application.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False application.config['REDIS_URL'] = 'redis://:xxx@xxx.redis.rds.aliyuncs.com:6379/0' application.config['MONGO_URI'] = 'mongodb://root:xxx@dds-xx.mongodb.rds.aliyuncs.com:3717,dds-xx.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-12258621' application.config['MONGODB_SETTINGS'] = { 'db': 'app', 'host': 'mongodb://root:xx@dds-xx.mongodb.rds.aliyuncs.com:3717,dds-xx.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-12258621' } application.config['ES_HOSTS'] = [ 'http://elastic:xx@es-xxx.elasticsearch.aliyuncs.com:9200/' ] # Celery configuration db 8 application.config['CELERY_BROKER_URL'] = 'redis://:xxx@r-xxx.redis.rds.aliyuncs.com:6379/1' application.config['CELERY_RESULT_BACKEND'] = 'redis://:xxx@r-xxxx.redis.rds.aliyuncs.com:6379/2' if os.environ.get('GENERATE_ZOMBIES'): from .generate_zombies import create_zombie_accounts, create_zombie_posts zombies_data_dir = os.environ.get('ZOMBIE_DATA_DIR', '/root/zombies/') zombies_n = int(os.environ.get('ZOMBIE_N', 2000)) # create_zombie_accounts(zombies_data_dir, zombies_n) create_zombie_posts() elif os.environ.get('FEED_PRODUCTS'): from .feed_products import feed_products feed_products() elif os.environ.get('UPDATE_ZOMBIES'): from .update_zombies import update update() elif os.environ.get('UPDATE_PRODUCTS'): from .update_products import update_products update_products() elif os.environ.get('INIT_STARS'): # 可独立运行脚本 pass elif os.environ.get('INIT_SHOW_BRANDS'): # export INIT_SHOW_BRANDS=1 # cd /root/backend/services/app # uwsgi --http :20011 -w app.main # ps -ef | grep 20011 | awk '{print $2}' | xargs kill -9 # from .upsert_name_brand import main_deal # main_deal() pass elif os.environ.get('INIT_LIKE'): # from .generate_show_posts import create_show_posts # create_show_posts() # 更新秀场时间 # from .update_show_tag_published_at import update_published_at # update_published_at() # from .update_brand_batch_objx import update_brand # update_brand() # from .delete_repeat_image_posts import delete_post # delete_post() # from .update_gjcy_new import main_deal # main_deal() # from .init_like import init_like # print("init like") # init_like() # export INIT_LIKE=1 # cd /root/backend/services/app # uwsgi --http :20010 -w app.main # ps -ef | grep 20010 | awk '{print $2}' | xargs kill -9 pass elif os.environ.get('INSERT_POSTS'): # export INSERT_POSTS=1 # uwsgi --http :20009 -w app.main # ps -ef | grep 20009| awk '{print $2}' | xargs kill -9 # from .insert_sp import main_deal # main_deal() # from .the_end import main_deal # print("start exe insert posts task!") # from .insert_zombies_post_0329 import main_deal # main_deal() pass elif os.environ.get('UPSERT_NAME_BRAND'): # export UPSERT_NAME_BRAND=1 # uwsgi --http :20000 -w app.main pass elif os.environ.get('DELETE'): # export DELETE=1 # uwsgi --http :20005 -w app.main # ps -ef | grep 20005 | awk '{print $2}' | xargs kill -9 # from .delete_zombies_posts_0325 import main_deal # main_deal() pass elif os.environ.get('FIND'): # export FIND=1 # cd /root/backend/services/app # uwsgi --http :20010 -w app.main # uwsgi --http :20009 -w app.main # uwsgi --http :20010 -w app.main # ps -ef | grep 20009 | awk '{print $2}' | xargs kill -9 # from .delete_zombies_posts_0325 import main_deal # main_deal() # from .find_cowboy_pants import main_deal # from .insert_show_post import main_deal pass elif os.environ.get('REPAIR'): # export REPAIR=1 # cd /root/backend/services/app # uwsgi --http :20010 -w app.main # uwsgi --http :20009 -w app.main # uwsgi --http :20010 -w app.main # ps -ef | grep 20009 | awk '{print $2}' | xargs kill -9 pass elif os.environ.get('TASK_AI_ANCHOR'): # task.py # export TASK_AI_ANCHOR=1 # cd /root/backend/services/app # uwsgi --http :20011 -w app.main # ps -ef | grep 20011 | awk '{print $2}' | xargs kill -9 pass elif os.environ.get('TASK_AI_POST'): # task_ai_post.py # cd /root/backend/services/app # export TASK_AI_POST=1 # celery -A app.task_ai_post.celery worker -l info pass elif os.environ.get('TASK_DEL_POST'): # task_del_post.py # cd /root/backend/services/app # export TASK_DEL_POST=1 # celery -A app.task_timed_del_post.celery worker -l info pass else: app.add_api(os.environ.get('OPENAPI_CONFIG_PATH', 'openapi.yaml'), validate_responses=True) # flask对某些返回制定返回码,遇到404返回40004 @application.after_request def rewrite_bad_request(response): if response.status_code == 200: pass elif response.status_code == 404: response = jsonify( {'code': 40004, 'message': "资源不存在", 'data': {}}) response.status_code = 200 elif response.status_code == 400: original = json.loads(response.data) response = jsonify( {'code': response.status_code, 'message': original["detail"], 'data': {}}) response.status_code = 200 else: response.data = '' return response if __name__ == "__main__": # Only for debugging while developing app.run(port=5000, debug=True)