2023-09-13 18:10:23 +08:00
|
|
|
from flask import Flask, jsonify
|
|
|
|
from pynats2 import NATSClient
|
|
|
|
import json
|
2023-09-13 17:40:06 +08:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
2023-09-13 18:10:23 +08:00
|
|
|
@app.route('/publish', methods=['POST'])
|
|
|
|
def publish_to_nats():
|
|
|
|
data_dict = {
|
|
|
|
"key": "value",
|
|
|
|
"number": 123,
|
|
|
|
"list": [1, 2, 3]
|
|
|
|
}
|
|
|
|
data_str = json.dumps(data_dict)
|
|
|
|
# 将字符串转换为bytes
|
|
|
|
data_bytes = data_str.encode('utf-8')
|
|
|
|
with NATSClient() as client:
|
|
|
|
client.publish("alg.test", payload=data_bytes)
|
|
|
|
return jsonify({'message': 'Message sent successfully!'})
|
2023-09-13 17:40:06 +08:00
|
|
|
|
2023-09-13 18:10:23 +08:00
|
|
|
if __name__ == '__main__':
|
2023-09-13 17:40:06 +08:00
|
|
|
app.run(debug=True)
|