-
Notifications
You must be signed in to change notification settings - Fork 213
/
setup_plausible.sh
executable file
·61 lines (52 loc) · 1.96 KB
/
setup_plausible.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
set -e
PLAUSIBLE_SERVICE_NAME="${PLAUSIBLE_SERVICE_NAME:-plausible}"
PLAUSIBLE_DB_SERVICE_NAME="${PLAUSIBLE_DB_SERVICE_NAME:-plausible_db}"
# Create Plausible user
docker compose exec -T "$PLAUSIBLE_SERVICE_NAME" \
/app/bin/plausible rpc \
'Plausible.Auth.User.new(%{name: "Deploy", email: "[email protected]", password: "deploy", password_confirmation: "deploy"}) |> Plausible.Repo.insert'
# load necessary environment variables into shell session
# Note: API_KEY with '{sites:provision:*}' scope and SECRET_KEY_BASE
# are both gotten from env file.
# shellcheck disable=SC1091
source docker/plausible/env.docker
docker compose exec -T "$PLAUSIBLE_DB_SERVICE_NAME" /bin/bash -c "psql -U deploy -d plausible <<-EOF
CREATE EXTENSION IF NOT EXISTS pgcrypto;
INSERT INTO api_keys
(id, user_id, name, key_prefix, key_hash, inserted_at, updated_at, scopes, hourly_request_limit)
VALUES
(1, 1, 'Development', 'aaaaaa', encode(digest('$SECRET_KEY_BASE$API_KEY', 'sha256'), 'hex'), now(), now(), '{sites:provision:*}', 1000)
ON CONFLICT (id) DO NOTHING
EOF"
authorization_header="Authorization: Bearer $API_KEY"
local_plausible="http://localhost:50288"
# Create site using API key
RES=$(curl \
-X POST \
-H "$authorization_header" \
-F 'domain="localhost"' \
-F 'timezone="UTC"' \
"$local_plausible/api/v1/sites")
if [[ $RES == *"This domain cannot be registered. Perhaps one of your colleagues registered it"* ]]; then
echo "Domain already exists."
elif [[ $RES == *"\"domain\":\"localhost\""* ]]; then
echo "Domain created."
else
echo "Error: $RES"
exit 1
fi
# Setup custom events
custom_events=$(node ./frontend/bin/get-custom-event-names.js)
echo "Verifying custom events:"
for eventName in $custom_events; do
echo "$eventName"
curl \
-X PUT \
-s --output /dev/null \
-H "$authorization_header" \
-F 'site_id=localhost' \
-F 'goal_type=event' \
-F "event_name=$eventName" \
"$local_plausible/api/v1/sites/goals"
done