Skip to content

Commit

Permalink
[Webhook] Add custom domain support
Browse files Browse the repository at this point in the history
  • Loading branch information
Herklos committed Oct 11, 2023
1 parent dda460a commit 8220cb0
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions Services/Services_bases/webhook_service/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def get_fields_description(self):
return {
services_constants.CONFIG_ENABLE_NGROK: "Use Ngrok",
services_constants.CONFIG_NGROK_TOKEN: "The ngrok token used to expose the webhook to the internet.",
services_constants.CONFIG_NGROK_DOMAIN: "[Optional] The ngrok subdomain.",
services_constants.CONFIG_WEBHOOK_SERVER_IP: "WebHook bind IP: used for webhook when ngrok is not enabled.",
services_constants.CONFIG_WEBHOOK_SERVER_PORT: "WebHook port: used for webhook when ngrok is not enabled."
}
Expand All @@ -52,6 +53,7 @@ def get_default_value(self):
return {
services_constants.CONFIG_ENABLE_NGROK: True,
services_constants.CONFIG_NGROK_TOKEN: "",
services_constants.CONFIG_NGROK_DOMAIN: "",
services_constants.CONFIG_WEBHOOK_SERVER_IP: services_constants.DEFAULT_WEBHOOK_SERVER_IP,
services_constants.CONFIG_WEBHOOK_SERVER_PORT: services_constants.DEFAULT_WEBHOOK_SERVER_PORT
}
Expand All @@ -62,6 +64,7 @@ def __init__(self):
self.ngrok_tunnel = None
self.webhook_public_url = ""
self.ngrok_enabled = True
self.ngrok_domain = None

self.service_feed_webhooks = {}
self.service_feed_auth_callbacks = {}
Expand Down Expand Up @@ -128,14 +131,14 @@ def is_subscribed(self, feed_name):
return feed_name in self.service_feed_webhooks

@staticmethod
def connect(port, protocol="http") -> ngrok.NgrokTunnel:
def connect(port, protocol="http", domain=None) -> str:
"""
Create a new ngrok tunnel
:param port: the tunnel local port
:param protocol: the protocol to use
:return: the ngrok url
"""
return ngrok.connect(port, protocol)
return ngrok.connect(port, protocol, domain=domain)

def subscribe_feed(self, service_feed_name, service_feed_callback, auth_callback) -> None:
"""
Expand All @@ -155,17 +158,14 @@ def get_subscribe_url(self, service_feed_name):

def _prepare_webhook_server(self):
try:
self.logger.debug(f"Starting local webhook server at {self.webhook_host}:{self.webhook_port}")
self.webhook_server = gevent.pywsgi.WSGIServer(
(self.webhook_host, self.webhook_port),
self.webhook_app,
log=None
)
self.webhook_server = gevent.pywsgi.WSGIServer((self.webhook_host, self.webhook_port),
self.webhook_app,
log=None)
self.webhook_server_context = self.webhook_app.app_context()
self.webhook_server_context.push()
except OSError as e:
self.webhook_server = None
self.logger.exception(e, False, f"Fail to start webhook : {e}")
self.get_logger().exception(e, False, f"Fail to start webhook : {e}")

def _register_webhook_routes(self, blueprint) -> None:
@blueprint.route('/')
Expand Down Expand Up @@ -203,6 +203,8 @@ async def prepare(self) -> None:
ngrok.set_auth_token(
self.config[services_constants.CONFIG_CATEGORY_SERVICES][services_constants.CONFIG_WEBHOOK][
services_constants.CONFIG_NGROK_TOKEN])
self.ngrok_domain = self.config[services_constants.CONFIG_CATEGORY_SERVICES][services_constants.CONFIG_WEBHOOK].get(
services_constants.CONFIG_NGROK_DOMAIN, None)
try:
self.webhook_host = os.getenv(services_constants.ENV_WEBHOOK_ADDRESS,
self.config[services_constants.CONFIG_CATEGORY_SERVICES]
Expand All @@ -224,7 +226,7 @@ def _start_server(self):
self._register_webhook_routes(self.webhook_app)
self.webhook_public_url = f"http://{self.webhook_host}:{self.webhook_port}/webhook"
if self.ngrok_enabled:
self.ngrok_tunnel = self.connect(self.webhook_port, protocol="http")
self.ngrok_tunnel = self.connect(self.webhook_port, protocol="http", domain=self.ngrok_domain)
self.webhook_public_url = f"{self.ngrok_tunnel.public_url}/webhook"
if self.webhook_server:
self.connected = True
Expand Down

0 comments on commit 8220cb0

Please sign in to comment.