all worked api with nginx

This commit is contained in:
acidmikk 2023-11-24 22:22:08 +04:00
parent 83528356a1
commit 06eba2e084
2 changed files with 45 additions and 25 deletions

View File

@ -11,47 +11,65 @@ time.sleep(20)
connection = pika.BlockingConnection(pika.ConnectionParameters('rabbitmq')) connection = pika.BlockingConnection(pika.ConnectionParameters('rabbitmq'))
channel = connection.channel() channel = connection.channel()
channel.queue_declare(queue='order_queue', durable=True) channel.queue_declare(queue='order_queue', durable=True)
orders = []
# READ ALL
@app.route('/orders', methods=['GET'])
def show_orders():
return orders
# CREATE # CREATE
@app.route('/orders', methods=['POST']) @app.route('/add_order/<int:user_id>_<string:product>_<string:action>', methods=['POST'])
def create_order(): def create_order(user_id, product, action):
data = request.get_json() order = {'id': len(orders) + 1,
order = {'user_id': data['user_id'], 'product': data['product'], 'action': 'create'} 'user_id': user_id,
'product': product,
'action': action}
orders.append(order)
# Отправка сообщения о создании заказа # Отправка сообщения о создании заказа
channel.basic_publish(exchange='microservices', routing_key='order', body=json.dumps(order)) channel.basic_publish(exchange='microservices', routing_key='order', body=bytes(json.dumps(order)))
return order, 201 return jsonify(order)
# READ # READ
@app.route('/orders/<int:order_id>', methods=['GET']) @app.route('/order/<int:order_id>', methods=['GET'])
def get_order(order_id): def get_order(order_id):
# Реализация READ операции по желанию order = next((order for order in orders if order['id'] == order_id), None)
return jsonify({'error': 'Read operation not implemented in order service'}), 501 if order:
read_order = {'id': order['id'],
'user_id': order['user_id'],
'product': order['product']}
return jsonify(read_order)
else:
return jsonify({'error': 'Order not found'})
# UPDATE # UPDATE
@app.route('/orders/<int:order_id>', methods=['PUT']) @app.route('/order/<int:order_id>/<int:user_id>_<string:product>_<string:action>', methods=['PUT'])
def update_order(order_id): def update_order(order_id, user_id, product, action):
data = request.get_json() order = next((order for order in orders if order['id'] == order_id), None)
order = {'user_id': data['user_id'], 'product': data['product'], 'order_id': order_id, 'action': 'update'} if order:
order['user_id'] = user_id
# Отправка сообщения об обновлении заказа order['product'] = product
channel.basic_publish(exchange='microservices', routing_key='order', body=json.dumps(order)) order['action'] = action# Отправка сообщения об обновлении заказа
channel.basic_publish(exchange='microservices', routing_key='order', body=bytes(json.dumps(order)))
return jsonify(order) return jsonify(order)
else:
return jsonify({'error': 'Order not found'})
# DELETE # DELETE
@app.route('/orders/<int:order_id>', methods=['DELETE']) @app.route('/orders/<int:order_id>', methods=['DELETE'])
def delete_order(order_id): def delete_order(order_id):
user_id = request.args.get('user_id') global orders
order = {'user_id': user_id, 'order_id': order_id, 'action': 'delete'} order = next((order for order in orders if order['id'] == order_id), None)
orders = [order for order in orders if order['id'] != order]
# Отправка сообщения об удалении заказа # Отправка сообщения об удалении заказа
channel.basic_publish(exchange='microservices', routing_key='order', body=json.dumps(order)) channel.basic_publish(exchange='microservices', routing_key='order', body=bytes(json.dumps(order)))
return jsonify({'result': True}) return jsonify({'result': True})

View File

@ -16,8 +16,9 @@ def connect_to_rabbitmq():
try: try:
connection = pika.BlockingConnection(pika.ConnectionParameters('rabbitmq')) connection = pika.BlockingConnection(pika.ConnectionParameters('rabbitmq'))
channel = connection.channel() channel = connection.channel()
channel.exchange_declare(exchange='microservices', exchange_type='direct')
channel.queue_declare(queue='order_service', durable=True) channel.queue_declare(queue='order_service', durable=True)
#channel.queue_bind(queue='order_service', routing_key='order') channel.queue_bind(exchange='microservices', queue='order_service', routing_key='order')
channel.basic_consume(queue='order_service', on_message_callback=process_order_message, auto_ack=True) channel.basic_consume(queue='order_service', on_message_callback=process_order_message, auto_ack=True)
print('Waiting for messages. To exit press CTRL+C') print('Waiting for messages. To exit press CTRL+C')
channel.start_consuming() channel.start_consuming()
@ -69,13 +70,14 @@ def process_order_message(ch, method, properties, body):
# CRUD операции для пользователей # CRUD операции для пользователей
# CREATE # READ ALL
@app.route('/users', methods=['GET']) @app.route('/users', methods=['GET'])
def show_users(): def show_users():
return users return users
@app.route('/add_users/<string:name>', methods=['POST']) # CREATE
@app.route('/add_user/<string:name>', methods=['POST'])
def create_user(name): def create_user(name):
if request.method == 'POST': if request.method == 'POST':
user = {'id': len(users) + 1, 'name': name} user = {'id': len(users) + 1, 'name': name}