Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Improve PG unix sockets configuration (#4202)
ruslandogab988364203fd
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8643fb9ce5..f9264ca7e2 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,8 +13,10 @@ All notable changes to this project will be documented in this file.### Removed- Deprecate `ECTO_IPV6` and `ECTO_CH_IPV6` env vars in CE plausible/analytics#4245- Remove support for importing data from no longer available Universal Analytics+- Soft-deprecate `DATABASE_SOCKET_DIR` plausible/analytics#4202### Changed+- Support Unix sockets in `DATABASE_URL` plausible/analytics#4202- Realtime and hourly graphs now show visits lasting their whole duration instead when specific events occur- Increase hourly request limit for API keys in CE from 600 to 1000000 (practically removing the limit) plausible/analytics#4200diff --git a/config/runtime.exs b/config/runtime.exsindex dcc6b6c896..9a649d1f18 100644--- a/config/runtime.exs+++ b/config/runtime.exs@@ -88,15 +88,6 @@ case secret_key_base donilend-db_url =- get_var_from_path_or_env(- config_dir,- "DATABASE_URL",- "postgres://postgres:postgres@plausible_db:5432/plausible_db"- )--db_socket_dir = get_var_from_path_or_env(config_dir, "DATABASE_SOCKET_DIR")-super_admin_user_ids =get_var_from_path_or_env(config_dir, "ADMIN_USER_IDS", "")|> String.split(",")@@ -333,7 +324,7 @@ config :plausible, PlausibleWeb.Endpoint,websocket_url: websocket_url,secure_cookie: secure_cookie-maybe_ipv6 =+db_maybe_ipv6 =if get_var_from_path_or_env(config_dir, "ECTO_IPV6") doif config_env() in [:ce, :ce_dev, :ce_test] doLogger.warning(@@ -346,12 +337,56 @@ maybe_ipv6 =[]end+db_url =+ get_var_from_path_or_env(+ config_dir,+ "DATABASE_URL",+ "postgres://postgres:postgres@plausible_db:5432/plausible_db"+ )++if db_socket_dir = get_var_from_path_or_env(config_dir, "DATABASE_SOCKET_DIR") do+ Logger.warning("""+ DATABASE_SOCKET_DIR is deprecated, please use DATABASE_URL instead:++ DATABASE_URL=postgresql://postgres:postgres@#{URI.encode_www_form(db_socket_dir)}/plausible_db++ or++ DATABASE_URL=postgresql:///plausible_db?host=#{db_socket_dir}"++ """)+end+db_cacertfile = get_var_from_path_or_env(config_dir, "DATABASE_CACERTFILE", CAStore.file_path())+%URI{host: db_host} = db_uri = URI.parse(db_url)+db_socket_dir? = String.starts_with?(db_host, "%2F") or db_host == ""++if db_socket_dir? do+ [database] = String.split(db_uri.path, "/", trim: true)++ socket_dir =+ if db_host == "" do+ db_host = (db_uri.query || "") |> URI.decode_query() |> Map.get("host")+ db_host || raise ArgumentError, "DATABASE_URL=#{db_url} doesn't include host info"+ else+ URI.decode_www_form(db_host)+ end-if is_nil(db_socket_dir) do+ config :plausible, Plausible.Repo,+ socket_dir: socket_dir,+ database: database++ if userinfo = db_uri.userinfo do+ [username, password] = String.split(userinfo, ":")++ config :plausible, Plausible.Repo,+ username: username,+ password: password+ end+elseconfig :plausible, Plausible.Repo,url: db_url,- socket_options: maybe_ipv6,+ socket_options: db_maybe_ipv6,ssl_opts: [cacertfile: db_cacertfile,verify: :verify_peer,@@ -359,10 +394,6 @@ if is_nil(db_socket_dir) domatch_fun: :public_key.pkix_verify_hostname_match_fun(:https)]]-else- config :plausible, Plausible.Repo,- socket_dir: db_socket_dir,- database: get_var_from_path_or_env(config_dir, "DATABASE_NAME", "plausible")endsentry_app_version = runtime_metadata[:version] || app_versiondiff --git a/test/plausible/config_test.exs b/test/plausible/config_test.exsindex 52a66158f4..1e0a0e11d8 100644--- a/test/plausible/config_test.exs+++ b/test/plausible/config_test.exs@@ -347,6 +347,74 @@ defmodule Plausible.ConfigTest doendend+ describe "postgres" do+ test "default" do+ env = [{"DATABASE_URL", nil}]+ config = runtime_config(env)++ assert get_in(config, [:plausible, Plausible.Repo]) == [+ url: "postgres://postgres:postgres@plausible_db:5432/plausible_db",+ socket_options: [],+ ssl_opts: [+ cacertfile: CAStore.file_path(),+ verify: :verify_peer,+ customize_hostname_check: [+ match_fun: :public_key.pkix_verify_hostname_match_fun(:https)+ ]+ ]+ ]+ end++ test "socket_dir in hostname" do+ env = [{"DATABASE_URL", "postgresql://postgres:postgres@%2Frun%2Fpostgresql/plausible_db"}]+ config = runtime_config(env)++ assert get_in(config, [:plausible, Plausible.Repo]) == [+ socket_dir: "/run/postgresql",+ database: "plausible_db",+ username: "postgres",+ password: "postgres"+ ]+ end++ test "socket_dir in query" do+ env = [{"DATABASE_URL", "postgresql:///plausible_db?host=/run/postgresql"}]+ config = runtime_config(env)++ assert get_in(config, [:plausible, Plausible.Repo]) == [+ socket_dir: "/run/postgresql",+ database: "plausible_db"+ ]+ end++ test "socket_dir missing" do+ env = [{"DATABASE_URL", "postgresql:///plausible_db"}]+ assert_raise ArgumentError, ~r/doesn't include host info/, fn -> runtime_config(env) end+ end++ test "custom URL" do+ env = [+ {"DATABASE_URL",+ "postgresql://your_username:[email protected]:25060/defaultdb"}+ ]++ config = runtime_config(env)++ assert get_in(config, [:plausible, Plausible.Repo]) == [+ url:+ "postgresql://your_username:[email protected]:25060/defaultdb",+ socket_options: [],+ ssl_opts: [+ cacertfile: CAStore.file_path(),+ verify: :verify_peer,+ customize_hostname_check: [+ match_fun: :public_key.pkix_verify_hostname_match_fun(:https)+ ]+ ]+ ]+ end+ end+describe "extra config" dotest "no-op when no extra path is set" doput_system_env_undo({"EXTRA_CONFIG_PATH", nil})