From 390a4577b5b7f86ba3cdd5f2d09a44ce23eff5e0 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Fri, 5 Jun 2020 11:15:26 +0300 Subject: [PATCH] Fetch config options at runtime --- lib/mix/tasks/hydrate_clickhouse.ex | 4 ++-- lib/plausible/billing/paddle_api.ex | 23 ++++++++++++------- .../controllers/api/external_controller.ex | 4 ++-- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/mix/tasks/hydrate_clickhouse.ex b/lib/mix/tasks/hydrate_clickhouse.ex index 0a0f03c29..c07259269 100644 --- a/lib/mix/tasks/hydrate_clickhouse.ex +++ b/lib/mix/tasks/hydrate_clickhouse.ex @@ -2,7 +2,6 @@ defmodule Mix.Tasks.HydrateClickhouse do use Mix.Task use Plausible.Repo require Logger - @hash_key Keyword.fetch!(Application.get_env(:plausible, PlausibleWeb.Endpoint), :secret_key_base) |> binary_part(0, 16) # coveralls-ignore-start @@ -101,6 +100,7 @@ defmodule Mix.Tasks.HydrateClickhouse do end def hydrate_events(repo, _args \\ []) do + hash_key = Keyword.fetch!(Application.get_env(:plausible, PlausibleWeb.Endpoint), :secret_key_base) |> binary_part(0, 16) end_time = ~N[2020-05-21 10:46:51] total = Repo.aggregate(from(e in Plausible.Event, where: e.timestamp < ^end_time), :count, :id) @@ -114,7 +114,7 @@ defmodule Mix.Tasks.HydrateClickhouse do {session_cache, sessions, events} = Enum.reduce(events, {session_cache, [], []}, fn event, {session_cache, sessions, new_events} -> found_session = session_cache[event.fingerprint] active = is_active?(found_session, event) - user_id = SipHash.hash!(@hash_key, event.fingerprint) + user_id = SipHash.hash!(hash_key, event.fingerprint) clickhouse_event = struct(Plausible.ClickhouseEvent, Map.from_struct(event) |> Map.put(:user_id, user_id)) cond do diff --git a/lib/plausible/billing/paddle_api.ex b/lib/plausible/billing/paddle_api.ex index 941c3f16e..38c39a38e 100644 --- a/lib/plausible/billing/paddle_api.ex +++ b/lib/plausible/billing/paddle_api.ex @@ -2,17 +2,17 @@ defmodule Plausible.Billing.PaddleApi do @update_preview_endpoint "https://vendors.paddle.com/api/2.0/subscription/preview_update" @update_endpoint "https://vendors.paddle.com/api/2.0/subscription/users/update" @get_endpoint "https://vendors.paddle.com/api/2.0/subscription/users" - @vendor_id Keyword.fetch!(Application.get_env(:plausible, :paddle), :vendor_id) - @vendor_auth_code Keyword.fetch!(Application.get_env(:plausible, :paddle), :vendor_auth_code) @headers [ {"Content-type", "application/json"}, {"Accept", "application/json"} ] def update_subscription_preview(paddle_subscription_id, new_plan_id) do + config = get_config() + params = %{ - vendor_id: @vendor_id, - vendor_auth_code: @vendor_auth_code, + vendor_id: config[:vendor_id], + vendor_auth_code: config[:vendor_auth_code], subscription_id: paddle_subscription_id, plan_id: new_plan_id, prorate: true, @@ -32,9 +32,11 @@ defmodule Plausible.Billing.PaddleApi do end def update_subscription(paddle_subscription_id, params) do + config = get_config() + params = Map.merge(params, %{ - vendor_id: @vendor_id, - vendor_auth_code: @vendor_auth_code, + vendor_id: config[:vendor_id], + vendor_auth_code: config[:vendor_auth_code], subscription_id: paddle_subscription_id, quantity: 1 }) @@ -50,9 +52,10 @@ defmodule Plausible.Billing.PaddleApi do end def get_subscription(paddle_subscription_id) do + config = get_config() params = %{ - vendor_id: @vendor_id, - vendor_auth_code: @vendor_auth_code, + vendor_id: config[:vendor_id], + vendor_auth_code: config[:vendor_auth_code], subscription_id: paddle_subscription_id } @@ -66,4 +69,8 @@ defmodule Plausible.Billing.PaddleApi do {:error, body["error"]} end end + + defp get_config() do + Application.get_env(:plausible, :paddle) + end end diff --git a/lib/plausible_web/controllers/api/external_controller.ex b/lib/plausible_web/controllers/api/external_controller.ex index b78dc1c4f..b71d4a920 100644 --- a/lib/plausible_web/controllers/api/external_controller.ex +++ b/lib/plausible_web/controllers/api/external_controller.ex @@ -1,7 +1,6 @@ defmodule PlausibleWeb.Api.ExternalController do use PlausibleWeb, :controller require Logger - @hash_key Keyword.fetch!(Application.get_env(:plausible, PlausibleWeb.Endpoint), :secret_key_base) |> binary_part(0, 16) def event(conn, _params) do params = parse_body(conn) @@ -98,11 +97,12 @@ defmodule PlausibleWeb.Api.ExternalController do end defp generate_user_id(conn, params) do + hash_key = Keyword.fetch!(Application.get_env(:plausible, PlausibleWeb.Endpoint), :secret_key_base) |> binary_part(0, 16) user_agent = List.first(Plug.Conn.get_req_header(conn, "user-agent")) || "" ip_address = List.first(Plug.Conn.get_req_header(conn, "x-bb-ip")) || "" # Netlify sets this header as the remote client IP domain = strip_www(params["domain"]) || "" - SipHash.hash!(@hash_key, user_agent <> ip_address <> domain) + SipHash.hash!(hash_key, user_agent <> ip_address <> domain) end defp calculate_screen_size(nil) , do: nil