Fetch config options at runtime

This commit is contained in:
Uku Taht 2020-06-05 11:15:26 +03:00
parent 6b8cfbe676
commit 390a4577b5
3 changed files with 19 additions and 12 deletions

View File

@ -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

View File

@ -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

View File

@ -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