2022-04-28 12:24:29 +03:00
|
|
|
defimpl FunWithFlags.Actor, for: BitString do
|
|
|
|
def id(str) do
|
|
|
|
str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
defmodule PlausibleWeb.Api.ExternalController do
|
|
|
|
use PlausibleWeb, :controller
|
|
|
|
require Logger
|
|
|
|
|
2019-10-31 06:49:46 +03:00
|
|
|
def event(conn, _params) do
|
2022-08-10 10:36:40 +03:00
|
|
|
with {:ok, ingestion_request} <- Plausible.Ingestion.Request.build(conn),
|
2022-08-16 14:43:10 +03:00
|
|
|
_ <- Sentry.Context.set_extra_context(%{request: ingestion_request}),
|
|
|
|
:ok <- Plausible.Ingestion.Event.build_and_buffer(ingestion_request) do
|
2021-11-18 11:36:16 +03:00
|
|
|
conn |> put_status(202) |> text("ok")
|
|
|
|
else
|
2022-08-16 14:43:10 +03:00
|
|
|
:skip ->
|
|
|
|
conn |> put_status(202) |> text("ok")
|
|
|
|
|
2021-11-18 11:36:16 +03:00
|
|
|
{:error, :invalid_json} ->
|
|
|
|
conn
|
|
|
|
|> put_status(400)
|
|
|
|
|> json(%{errors: %{request: "Unable to parse request body as json"}})
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2022-08-16 14:43:10 +03:00
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
|
|
errors =
|
|
|
|
Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} ->
|
|
|
|
Regex.replace(~r"%{(\w+)}", msg, fn _, key ->
|
|
|
|
opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2021-09-24 10:38:23 +03:00
|
|
|
conn |> put_status(400) |> json(%{errors: errors})
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def error(conn, _params) do
|
2021-03-31 11:38:14 +03:00
|
|
|
Sentry.capture_message("JS snippet error")
|
2019-09-02 14:29:19 +03:00
|
|
|
send_resp(conn, 200, "")
|
|
|
|
end
|
|
|
|
|
2020-07-20 10:34:35 +03:00
|
|
|
def health(conn, _params) do
|
2020-07-21 09:58:00 +03:00
|
|
|
postgres_health =
|
|
|
|
case Ecto.Adapters.SQL.query(Plausible.Repo, "SELECT 1", []) do
|
|
|
|
{:ok, _} -> "ok"
|
|
|
|
e -> "error: #{inspect(e)}"
|
|
|
|
end
|
2020-07-20 10:34:35 +03:00
|
|
|
|
2020-07-21 09:58:00 +03:00
|
|
|
clickhouse_health =
|
2020-09-17 16:36:01 +03:00
|
|
|
case Ecto.Adapters.SQL.query(Plausible.ClickhouseRepo, "SELECT 1", []) do
|
2020-07-21 09:58:00 +03:00
|
|
|
{:ok, _} -> "ok"
|
|
|
|
e -> "error: #{inspect(e)}"
|
|
|
|
end
|
2020-07-20 10:34:35 +03:00
|
|
|
|
2020-07-21 09:58:00 +03:00
|
|
|
status =
|
|
|
|
case {postgres_health, clickhouse_health} do
|
|
|
|
{"ok", "ok"} -> 200
|
|
|
|
_ -> 500
|
|
|
|
end
|
2020-07-20 10:34:35 +03:00
|
|
|
|
|
|
|
put_status(conn, status)
|
|
|
|
|> json(%{
|
|
|
|
postgres: postgres_health,
|
2020-07-21 09:58:00 +03:00
|
|
|
clickhouse: clickhouse_health
|
2020-07-20 10:34:35 +03:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2022-05-27 10:49:04 +03:00
|
|
|
def info(conn, _params) do
|
2022-05-27 15:24:11 +03:00
|
|
|
build_metadata = System.get_env("BUILD_METADATA", "{}") |> Jason.decode!()
|
2022-05-27 14:30:21 +03:00
|
|
|
|
2022-05-27 10:49:04 +03:00
|
|
|
geo_database =
|
2022-07-12 11:39:04 +03:00
|
|
|
case Geolix.metadata(where: :geolocation) do
|
|
|
|
%{database_type: type} ->
|
2022-05-27 10:49:04 +03:00
|
|
|
type
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
"(not configured)"
|
|
|
|
end
|
|
|
|
|
2022-05-27 15:51:24 +03:00
|
|
|
info = %{
|
|
|
|
geo_database: geo_database,
|
|
|
|
build: %{
|
|
|
|
version: get_in(build_metadata, ["labels", "org.opencontainers.image.version"]),
|
|
|
|
commit: get_in(build_metadata, ["labels", "org.opencontainers.image.revision"]),
|
|
|
|
created: get_in(build_metadata, ["labels", "org.opencontainers.image.created"]),
|
|
|
|
tags: get_in(build_metadata, ["tags"])
|
|
|
|
}
|
|
|
|
}
|
2022-05-27 10:49:04 +03:00
|
|
|
|
|
|
|
json(conn, info)
|
|
|
|
end
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|