Code: Select all
Dockerfile
[*]
Code: Select all
webhook_receiver.py
Code: Select all
webhook_receiver.py
Code: Select all
from flask import Flask,reuqest,jsonify
import psycopg2
import select
import json
import psycopg2.extensions
import requests
import pycopg2.extras as extras
app=Flask(__name__)
@app.route('/webhook',methods=['POST'])
def webhook():
data=request.json
print(f"received webhook data:{data}")
return jsonify({"status":"success"}),200
def webhook_receiver():
conn=psycopg2.connect("host=127.0.0.1 dbname=webhook_db user=postgres password=***** port=5432")
conn.setisolation_level(psycopg2.exrensions.ISOLATION_LEVEL_AUTOCOMMIT)
cur = conn.cursor()
cur.execute("LISTEN my_channel")
print("Waiting")
while True:
if select.select([conn],[],[],5)==([],[],[]):
continue
conn.poll()
while conn.notifies:
notify=conn.notifies.pop(0)
print(f"Received notification {notify.payload} from {notify.channel}")
response = requests.post('http://localhost:5000/webhook',json=notify.payload)
print(f"API response: {response.status_code} - {response.text}")
if __name__="__main__":
webhook_receiver()
from threading import Thread
Thread(target=webhook_receiver).start()
app.run(host='0.0.0.0', port=5000)
< /code>
Dockerfile
Code: Select all
#Dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY . /app
RUN pip install psycopg2-binary requests flask
EXPOSE 5000
CMD ["python","webhook_receiver.py"]
< /code>
When I run the command docker build -t webhook_receiver .
Code: Select all
psycopg2.OperationalError:connection to server at "127.0.0.1", port 5432 failed:Connection refused. Is the server running on that host and accepting TCP/IP connections?
< /code>
In the postgresql.conf
Code: Select all
conn=psycopg2.connect("host=172.0.0.1 ....... port=5432")
< /code>
to this:
conn=pyscopg2.connect("host=host.docker.internal ...... port=5432")
< /code>
sometimes after the internal