from flask_apscheduler import APScheduler import requests import time def req(): requests.get('http://localhost:4001/{}'.format(time.time())) scheduler = APScheduler(app=app) # scheduler.add_job('statistics_visitor', VisitorLogDate.statistics_visitor, # trigger='interval', seconds=5) scheduler.add_job('statistics_article_data', ArticleData.statistics_article_data, trigger='interval', seconds=66) scheduler.add_job('statistics_pv', Article.statistics_pv, trigger='interval', seconds=360) scheduler.add_job('statistics_article', Article.statistics_article, trigger='interval', seconds=350) # scheduler.add_job('test', req, # trigger='interval', seconds=1) scheduler.start() if __name__ == "__main__": app.run(debug=True, port=8003)
except IntegrityError: return {'error': 'This task already exists'} raise InvalidParameter @app.route('/tasks/<int:id>/', methods=['GET', 'DELETE']) def manage_tasks(id): task = Task.query.filter_by(id=id).first() if not task: raise TaskNotFound if request.method == 'GET': response = task.to_json() return response elif request.method == 'DELETE': db.session.delete(task) db.session.commit() return { 'message': 'task deleted' } if __name__ == '__main__': db.create_all() logging.basicConfig( filename='error.log', level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) app.run(debug=True) app.run(debug=True)
from app.config import app if __name__ == '__main__': app.run()
) # SERVICE HEALTH and ERROR HANDLERS # ----------------------------------------------------------------------------# @app.route("/health") def health(): return jsonify(status="OK") @app.errorhandler(500) def internal_error(error): # db_session.rollback() return render_template("errors/500.html"), 500 @app.errorhandler(404) def not_found_error(error): return render_template("errors/404.html"), 404 @app.errorhandler(401) def unauthorized(error): return redirect(url_for("home")) # Launch. # ----------------------------------------------------------------------------# if __name__ == "__main__": app.run(host="0.0.0.0")
logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) logger.addHandler(logging.StreamHandler(sys.stdout)) DEBUG = os.environ.get('DEBUG') @app.middleware('request') async def add_start_time(request): request['start_time'] = time() @app.middleware('response') async def add_spent_time(request, response): spend_time = round((time() - request['start_time']) * 1000) if request.method == 'POST': logger.info('%s %s %s %s %s %s', response.status, request.method, request.path, request.query_string, request.json, spend_time) @app.route('/', methods=['GET', 'OPTIONS']) async def index(request): if request.method == 'OPTIONS': return text('', 204) return text('Hello World!', 200) if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=DEBUG)
from app.config import app from app.rutas import * if __name__ == '__main__': app.run(port=5000, debug=True)
import os from flask import send_from_directory from app.config import app # router from controllers.authentication import auth_api from controllers.ticket import ticket_api from controllers.users import user_api from controllers.comment import comment_api # register router app.register_blueprint(auth_api, url_prefix='/api/auth') app.register_blueprint(user_api, url_prefix='/api/users') app.register_blueprint(ticket_api, url_prefix='/api/ticket') app.register_blueprint(comment_api, url_prefix='/api/comment') @app.route('/', defaults={'path': ''}) @app.route('/<path:path>') def serve(path): if path != "" and os.path.exists(app.static_folder + '/' + path): return send_from_directory(app.static_folder, path) else: return send_from_directory(app.static_folder, 'index.html') if __name__ == '__main__': app.run()