Skip to content

Commit

Permalink
Merge branch 'main' of github.com:HermanMartinus/bearlytics
Browse files Browse the repository at this point in the history
  • Loading branch information
HermanMartinus committed Dec 10, 2024
2 parents a0b9117 + eee3e20 commit b775bcb
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 7 deletions.
12 changes: 12 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.dockerignore
.env
.gitattributes
.gitignore
analytics
docker-compose.yaml
Dockerfile
Makefile
Procfile
README.md
ROADMAP.md
screenshot.png
45 changes: 45 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Create and publish Docker images

on:
push:
branches: ['main']

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout respository
uses: actions/checkout@v4
- name: Log in to the Container registry
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@369eb591f429131d6889c46b94e711f089e6ca96
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# TODO: figure out versioning / releases
# For now, every push to main will generate an image tagged with 'latest'
# and the commit short hash.
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=sha
- name: Build and push Docker image
id: push
uses: docker/build-push-action@48aba3b46d1b1fec4febb7c5d0c644b249a11355
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,4 @@ venv.bak/
.mypy_cache/
.jsbeautifyrc

analytics/
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM python:3-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
curl=7.88.1-10+deb12u8 && \
rm -rf /var/lib/apt/lists/* && \
groupadd -g 1000 bear && \
useradd -u 1000 -g 1000 -m -s /bin/bash bear && \
mkdir -p /app/data && \
chown -R bear:bear /app

WORKDIR /app

COPY --chown=bear:bear . .

RUN pip install --no-cache-dir -r requirements.txt && \
chmod +x docker/entrypoint.sh docker/run.sh

EXPOSE 8000
HEALTHCHECK --interval=60s --timeout=30s --start-period=15s --retries=3 \
CMD curl -f http://localhost:8000/script.js || exit 1
ENTRYPOINT ["/app/docker/entrypoint.sh"]
CMD ["/app/docker/run.sh"]
60 changes: 54 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,20 @@ A: The database is stored in a SQLite database file on your server. You can back
**Q: Can I pay you to host my analytics?**
A: No, but if it's something you're interested in, let me know by commenting on [this issue](https://github.com/HermanMartinus/bearlytics/issues/1), and I may add it to [the roadmap](ROADMAP.md).

## Self-hosting Guide
## Self-hosting Guides

This is a basic Django app that can be deployed to any platform that supports Django. I've chosen Dokku for this guide because it's easy to set up and manage, and I'm familiar with it.
This is a basic Django app that can be deployed to any platform that supports Django.

### Prerequisites
### Dokku
I've chosen Dokku for this guide because it's easy to set up and manage, and I'm familiar with it.

#### Prerequisites

- A Dokku server
- Basic knowledge of terminal commands
- A cup of coffee

### Step 1: Dokku Setup
#### Step 1: Dokku Setup

```bash
# Create the app
Expand All @@ -111,15 +114,15 @@ dokku config:set SALT_SECRET=your_very_secret_salt_here
dokku config:set DEBUG=False
```

### Step 2: Deploy
#### Step 2: Deploy

Add Dokku as a remote and commit and push your code
```bash
git remote add dokku dokku@your-server:analytics
git push dokku main
```

### Step 3: Add Your First Website
#### Step 3: Add Your First Website

1. Visit `https://your-analytics-domain.com`
2. Create an account (if no user exists, the first user will be automatically promoted to admin)
Expand All @@ -128,6 +131,51 @@ git push dokku main
5. Add it to your website's `<head>` section
6. Watch those sweet, sweet analytics roll in

### Docker
For hosting with Docker Compose:

1. Create a directory to hold the configuration .
```shell
mkdir -p /opt/bearlytics
```
2. Create a subfolder `analytics/` to store the data.
3. Create a `.env` file with (at least):
```shell
SECRET_KEY=your_django_secret_key
SALT_SECRET=your_secret_salt
```
4. Review [docker-compose.yaml](docker-compose.yaml) for any other needed changes, such as adjusting the `UID`/`GID` to match the ownership of the `analytics/` directory:
```yaml
services:
bearlytics:
image: ghcr.io/HermanMartinus/bearlytics:latest
ports:
- 8000:8000
volumes:
- ./analytics:/app/data
environment:
CSRF_TRUSTED_ORIGINS: ${CSRF_TRUSTED_ORIGINS:-http://localhost}
DB_PATH: ${DB_PATH:-/app/data/analytics.db}
DEBUG: False
SALT_SECRET: ${SALT_SECRET:?err}
SECRET_KEY: ${SECRET_KEY:?err}
UID: 1000
GID: 1000
restart: unless-stopped
```
5. Deploy the app:
```shell
docker compose up -d
```

For production deployments, serve Bearlytics behind a reverse proxy like [Caddy](https://caddyserver.com/docs/quick-starts/reverse-proxy) or [nginx](https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/). A Caddy configuration could be as simple as:

```
bearlytics.example.com {
reverse_proxy http://localhost:8000
}
```
## Tracking Script
```html
Expand Down
17 changes: 17 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
services:
bearlytics:
image: ghcr.io/HermanMartinus/bearlytics:latest
ports:
- 8000:8000
volumes:
- ./analytics:/app/data
environment:
CSRF_TRUSTED_ORIGINS: ${CSRF_TRUSTED_ORIGINS:-http://localhost}
DB_PATH: ${DB_PATH:-/app/data/analytics.db}
DEBUG: False
SALT_SECRET: ${SALT_SECRET:?err}
SECRET_KEY: ${SECRET_KEY:?err}
UID: 1000
GID: 1000
restart: unless-stopped

12 changes: 12 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
set -e

export USER=bear
UID=${UID:-1000}
GID=${GID:-1000}
groupmod -o -g "$GID" $USER
usermod -o -u "$UID" $USER
chown -R $USER:$USER /app

exec su $USER -c "$@"

9 changes: 9 additions & 0 deletions docker/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -e

# Run migrations first
python manage.py migrate

# Start gunicorn
exec gunicorn conf.wsgi --log-file - --bind 0.0.0.0:8000

2 changes: 1 addition & 1 deletion project/templates/websites.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h2 class="text-xl text-gray-700 mb-6">Your Websites</h2>
<input
type="text"
readonly
value='<script data-site="{{ website.id }}" src="//{{ request.get_host }}/script.js" defer></script>'
value='<script data-site="{{ website.id }}" src="{{ request.scheme }}://{{ request.get_host }}/script.js" defer></script>'
class="w-full h-10 bg-gray-100 px-3 rounded border border-gray-200 text-gray-600 text-sm focus:outline-none focus:ring-2 focus:ring-blue-300"
>
</div>
Expand Down

0 comments on commit b775bcb

Please sign in to comment.