Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Webhook] Add custom domain support #1076

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 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)-> ngrok.NgrokTunnel:
"""
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 Down Expand Up @@ -203,6 +206,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 +229,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