From 8f6f860bd29f4705751623e93bdb25b8ca2e74fa Mon Sep 17 00:00:00 2001 From: Vinicius Brasil Date: Tue, 11 Oct 2022 07:13:55 -0300 Subject: [PATCH] Add --host option to send_event task (#2320) This commit adds an option to override `http://localhost:8000` in `mix send_event`. This is useful when running the server in another port or with remote servers such as staging. --- lib/mix/tasks/send_pageview.ex | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/mix/tasks/send_pageview.ex b/lib/mix/tasks/send_pageview.ex index 71a7ce45d..dcbcaf092 100644 --- a/lib/mix/tasks/send_pageview.ex +++ b/lib/mix/tasks/send_pageview.ex @@ -9,6 +9,7 @@ defmodule Mix.Tasks.SendPageview do use Mix.Task require Logger + @default_host "http://localhost:8000" @default_ip_address "127.0.0.1" @default_user_agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.284" @default_domain "dummy.site" @@ -19,7 +20,8 @@ defmodule Mix.Tasks.SendPageview do user_agent: :string, domain: :string, page: :string, - referrer: :string + referrer: :string, + host: :string ] def run(opts) do @@ -41,8 +43,9 @@ defmodule Mix.Tasks.SendPageview do defp do_send_pageview(parsed_opts) do ip = Keyword.get(parsed_opts, :ip, @default_ip_address) user_agent = Keyword.get(parsed_opts, :user_agent, @default_user_agent) + host = Keyword.get(parsed_opts, :host, @default_host) - url = get_url() + url = get_url(host) headers = get_headers(ip, user_agent) body = get_body(parsed_opts) @@ -57,8 +60,8 @@ defmodule Mix.Tasks.SendPageview do end end - defp get_url() do - "http://localhost:8000/api/event" + defp get_url(host) do + Path.join(host, "/api/event") end defp get_headers(ip, user_agent) do