monorepo-microservice-rbac/apps/services/alg/app.py

23 lines
592 B
Python
Raw Normal View History

2023-09-13 17:40:06 +08:00
from flask import Flask, request, jsonify
import nats.aio.client
import asyncio
app = Flask(__name__)
nc = nats.aio.client.Client()
@app.route('/send_to_nats', methods=['POST'])
def send_to_nats():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(send_message(request.json))
print("1")
return 111
async def send_message(message):
await nc.connect(servers=["nats://localhost:4222"])
await nc.publish(jsonify({"cmd": "alg.test"}), str(message).encode())
await nc.close()
if __name__ == "__main__":
app.run(debug=True)