~nyg/notes

8f01cb0079c1d223259b80801c5c95b03ab38ff6 — nyg 1 year, 18 days ago d6ea1d4
Enable GoatCounter analytics
A docs/assets/count.py => docs/assets/count.py +41 -0
@@ 0,0 1,41 @@
#!/usr/bin/env python3

from json import dumps
from os import environ
from urllib.error import URLError, HTTPError
from urllib.parse import parse_qsl
from urllib.request import Request, urlopen

params = dict(parse_qsl(environ['QUERY_STRING']))

data = {
    'hits': [{
        'bot': int(params.get('b', 0)),
        'event': bool(params.get('e', False)),
        'ip': environ['REMOTE_ADDR'],
        'path': params.get('p'),
        'query': params.get('q'),
        'ref': params.get('r'),
        'size': params.get('s'),
        'title': params.get('t'),
        'user_agent': environ['HTTP_USER_AGENT']
    }],
    'no_sessions': True
}

headers = {
    'Content-type': 'application/json',
    'Authorization': 'Bearer {}'.format(environ['GOAT_KEY'])
}

try:
    res = urlopen(Request(environ['GOAT_HOST'], dumps(data).encode(), headers))
    status, reason = res.status, res.reason
except HTTPError as e:
    status, reason = e.code, e.reason
except URLError:
    status, reason = 500, 'Internal Server Error'

print('Content-Type: text/html')
print('Status: {} {}'.format(status, reason))
print()

M mkdocs.yml => mkdocs.yml +4 -5
@@ 8,11 8,10 @@ repo_url: https://git.sr.ht/~nyg/notes

extra_css:
  - assets/custom.css
  - assets/asciinema-player.css

# extra:
#   analytics:
#     provider: goatcounter
extra:
  analytics:
    provider: goatcounter

markdown_extensions:
  - pymdownx.highlight


@@ 36,7 35,7 @@ theme:
  custom_dir: overrides
  language: custom
  features:
    - navigation.instant
    #- navigation.instant
    - navigation.tracking
    - navigation.tabs
    - navigation.sections

M overrides/partials/integrations/analytics/goatcounter.html => overrides/partials/integrations/analytics/goatcounter.html +1 -3
@@ 1,3 1,1 @@
<!-- not possible due to CSP restrictions…
<script data-goatcounter="https://andstuff.club/goatcounter" async src="/assets/goatcounter.js"></script>
-->
<script data-goatcounter="/assets/count.py" async src="/assets/goatcounter.js"></script>

M publish.sh => publish.sh +17 -8
@@ 2,20 2,29 @@

set -eu

echo Building website…
echo_section() {
    command echo "\n"$(tput bold)$@$(tput sgr0)
}

echo_section Building website…
mkdocs build

echo Creating archive…
( cd site && tar -cvz * > ../site.tar.gz )
echo_section Creating archive…
( cd site && tar -cz * > ../site.tar.gz )
ls -lh site.tar.gz

echo Copying archive to host
source .sshenv
ssh $SSH_USER@$SSH_HOST 'rm -rf /home/public/notes.andstuff.club/* || echo Folder already empty'
echo "\n"Removing existing site files on host…
ssh $SSH_USER@$SSH_HOST "find $SSH_DIR ! -name .well-known ! -name .htaccess -mindepth 1 -maxdepth 1 -exec rm -r {} +"

echo_section Copying archive to host…
scp site.tar.gz $SSH_USER@$SSH_HOST:$SSH_DIR
ssh $SSH_USER@$SSH_HOST "cd $SSH_DIR && tar -xvf site.tar.gz && rm site.tar.gz"

echo Uploaded archive:
ls -lh site.tar.gz
echo "\n"Extracting archive to site folder…
ssh $SSH_USER@$SSH_HOST "cd $SSH_DIR && tar -xf site.tar.gz && rm site.tar.gz"

echo Setting executable permission for CGI script…
ssh $SSH_USER@$SSH_HOST "chmod +x $SSH_DIR/assets/count.py"

echo Removing archive…
rm site.tar.gz

A test-cgi.sh => test-cgi.sh +8 -0
@@ 0,0 1,8 @@
#!/usr/bin/env sh

QUERY_STRING='p=%2F&t=Welcome%20-%20Notes&s=3440%2C1440%2C1&b=0&rnd=wc5ow' \
REMOTE_ADDR='1.1.1.1' \
HTTP_USER_AGENT='vscode' \
GOAT_KEY=abc \
GOAT_HOST=https://eo675tndws631wx.m.pipedream.net \
python docs/assets/count.py