23 lines
592 B
Python
23 lines
592 B
Python
|
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)
|