12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- from flask import Flask, jsonify, request
- from xmpp import cli
- from flask_jwt_extended import JWTManager, jwt_required, get_jwt_identity, create_access_token
- from datetime import timedelta
- from config import Config
- from MyQuery import MyQuery
- #from OpenSSL import SSL
- #context = SSL.Context(SSL.PROTOCOL_TLSv1_2)
- #context.use_privatekey_file('server.key')
- #context.use_certificate_file('server.crt')
- ### ssl_context=context in run
- db_query = MyQuery()
- app = Flask(__name__)
- app.config.from_object(Config)
- jwt = JWTManager(app)
- #Генерация токена
- def gen_token(id, minut):
- token = {"access_token" : create_access_token(identity=id, expires_delta=timedelta(minutes=minut))}
- return jsonify(token)
- #Авторизация пользователя
- @app.route('/auth', methods=['POST'])
- def post_auth():
- try:
- result = db_query.auth_query(request.get_json())
- return gen_token(result['id'], 5), 200
- except Exception as ex:
- return jsonify({"msg":str(ex)}), 500
- #Добавление пользователя и проверка на сходства
- @app.route('/adduser', methods=['POST'])
- def get_add():
- try:
- result = db_query.add_query(request.get_json())
- return jsonify(result[0]), result[1]
- except Exception as ex:
- return jsonify({"msg":str(ex)}), 500
- #Запрос на получение данных об одном пользователе
- @app.route('/user/<int:id>', methods=['GET'])
- def get_user(id):
- try:
- result = db_query.get_user_query(id)
- return jsonify(result[0]), result[1]
- except Exception as ex:
- return jsonify({"msg":str(ex)}), 500
- #Запрос на получение данных всех пользователей
- @app.route('/users', methods=['GET'])
- def get_show_user():
- try:
- result = db_query.get_all_user_query()
- return jsonify(result[0]), result[1]
- except Exception as ex:
- return jsonify({"msg":str(ex)}), 500
- #Удаление пользователя
- @app.route('/deleteuser', methods=['POST'])
- def delete_user():
- try:
- result = db_query.delete_user_query(request.get_json())
- return jsonify(result[0]), result[1]
- except Exception as ex:
- return jsonify({"msg":str(ex)}), 500
- #Обновление данных пользователя
- @app.route('/updateuser', methods=['POST'])
- @jwt_required()
- def test_api():
- try:
- result = db_query.update_user_query(request.get_json(), get_jwt_identity())
- return jsonify(result[0]), result[1]
- except Exception as ex:
- return jsonify({"msg":str(ex)}), 500
- #Отправка сообщения через XMPP
- @app.route('/testmessage', methods=['POST'])
- def send_mess():
- try:
- request_data = request.get_json()
- for to in request_data['to']:
- cli.send_message(request_data['from'], request_data['password'], to, request_data['message'])
- return jsonify({"msg":"Succes send!"}), 200
- except:
- return jsonify({"msg":"Server error!"}), 500
- if __name__ == '__main__':
- #from waitress import serve
- #serve(app, host='127.0.0.2', port='8080')
- app.run(host='127.0.0.2', port='8080', debug=True, load_dotenv=True )
|