Skip to content

Commit f2c5aa4

Browse files
authored
Merge pull request #259 from jaypatrick/master
Reverse merge from master to dev
2 parents 55ad01b + d1c416b commit f2c5aa4

18 files changed

Lines changed: 994 additions & 53 deletions

.dockerignore

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Git
2+
.git
3+
.gitignore
4+
.gitattributes
5+
6+
# Environment files (secrets should be passed via docker-compose or -e flag)
7+
.env.local
8+
.env.*.local
9+
.envrc
10+
11+
# Node
12+
node_modules
13+
npm-debug.log
14+
yarn-error.log
15+
16+
# Deno
17+
.deno/
18+
19+
# Build artifacts
20+
dist/
21+
*.db
22+
*.db-journal
23+
*.db-wal
24+
*.db-shm
25+
data/
26+
27+
# IDE
28+
.vscode
29+
.idea
30+
*.swp
31+
*.swo
32+
33+
# OS
34+
.DS_Store
35+
Thumbs.db
36+
37+
# Test coverage
38+
coverage/
39+
coverage.lcov
40+
41+
# Wrangler
42+
.wrangler/
43+
worker-configuration.d.ts
44+
.deployment-version.json
45+
46+
# Documentation
47+
*.md
48+
!README.md
49+
50+
# CI/CD
51+
.github/
52+
53+
# Examples
54+
examples/
55+
56+
# Output
57+
output/
58+
59+
# Logs
60+
*.log

.env

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Base environment variables shared across all environments
2+
# Loaded first by .envrc, then overlaid with .env.$ENV and .env.local
3+
# DO NOT put secrets here - use .env.local instead
4+
5+
# Compiler version
6+
COMPILER_VERSION=0.11.4
7+
8+
# Server port (default: 8787)
9+
PORT=8787
10+
11+
# NOTE: Do not set DENO_DIR globally here, as this file is loaded in CI/GitHub Actions
12+
# and /app/.deno may not exist or be writable. Configure DENO_DIR only in
13+
# environment-specific files (e.g. .env.docker) or in container configs.
14+
# DENO_DIR=/app/.deno

.env.README.md

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# Environment Configuration
2+
3+
This project uses a layered environment configuration system powered by `.envrc` and `direnv`.
4+
5+
## How It Works
6+
7+
Environment variables are loaded in the following order (later files override earlier ones):
8+
9+
1. **`.env`** - Base configuration shared across all environments (committed to git)
10+
2. **`.env.$ENV`** - Environment-specific configuration (committed to git)
11+
3. **`.env.local`** - Local overrides and secrets (NOT committed to git)
12+
13+
The `$ENV` variable is automatically determined by your current git branch:
14+
15+
| Git Branch | Environment | Loaded File |
16+
| ----------------------- | ------------- | ------------------- |
17+
| `main` or `master` | `production` | `.env.production` |
18+
| `dev` or `develop` | `development` | `.env.development` |
19+
| Other branches | `local` | `.env.local` |
20+
| Custom branch with file | Custom | `.env.$BRANCH_NAME` |
21+
22+
## File Structure
23+
24+
```
25+
.env # Base config (PORT, COMPILER_VERSION, etc.)
26+
.env.development # Development-specific (test API keys, local DB)
27+
.env.production # Production-specific (placeholder values)
28+
.env.local # Your personal secrets (NEVER commit this!)
29+
.env.example # Template showing all available variables
30+
```
31+
32+
## Setup Instructions
33+
34+
### 1. Enable direnv (if not already installed)
35+
36+
```bash
37+
# macOS
38+
brew install direnv
39+
40+
# Add to your shell config (~/.zshrc)
41+
eval "$(direnv hook zsh)"
42+
```
43+
44+
### 2. Allow the .envrc file
45+
46+
```bash
47+
direnv allow
48+
```
49+
50+
You should see: `✅ Loaded environment: development (branch: dev)`
51+
52+
### 3. Create your .env.local file
53+
54+
```bash
55+
cp .env.example .env.local
56+
```
57+
58+
Then edit `.env.local` with your actual secrets and API keys.
59+
60+
## What Goes Where?
61+
62+
### `.env` (Committed)
63+
64+
- Non-sensitive defaults
65+
- Port numbers
66+
- Version numbers
67+
- Public configuration
68+
69+
### `.env.development` / `.env.production` (Committed)
70+
71+
- Environment-specific defaults
72+
- Test API keys (development only)
73+
- Environment-specific feature flags
74+
- Non-secret configuration
75+
76+
### `.env.local` (NOT Committed)
77+
78+
- **ALL secrets and API keys**
79+
- Database connection strings
80+
- Authentication tokens
81+
- Personal overrides
82+
83+
## Wrangler Integration
84+
85+
The `wrangler.toml` configuration supports environment-based deployments:
86+
87+
```bash
88+
# Development deployment
89+
wrangler deploy --env development
90+
91+
# Production deployment
92+
wrangler deploy --env production
93+
```
94+
95+
Environment variables from `.env.local` are automatically available during local development (`wrangler dev`).
96+
97+
For production deployments, secrets should be set using:
98+
99+
```bash
100+
wrangler secret put ADMIN_KEY --env production
101+
wrangler secret put TURNSTILE_SECRET_KEY --env production
102+
```
103+
104+
## Troubleshooting
105+
106+
### Environment not loading?
107+
108+
```bash
109+
# Re-allow the .envrc
110+
direnv allow
111+
112+
# Check what's loaded
113+
direnv exec . env | grep DATABASE_URL
114+
```
115+
116+
### Wrong environment?
117+
118+
Check your git branch:
119+
120+
```bash
121+
git branch --show-current
122+
```
123+
124+
The `.envrc` automatically maps your branch to an environment.
125+
126+
### Variables not available?
127+
128+
Make sure:
129+
130+
1. You've created `.env.local` from `.env.example`
131+
2. You've run `direnv allow`
132+
3. The variable exists in one of the .env files
133+
134+
## Security Best Practices
135+
136+
-**DO** commit `.env`, `.env.development`, `.env.production`
137+
-**DO** use test/dummy values in committed files
138+
-**DO** put all secrets in `.env.local`
139+
-**DON'T** commit `.env.local`
140+
- ⚠️ **BE CAREFUL** with `.envrc` — it is committed as part of the env-loading system, so never put secrets or credentials in it
141+
-**DON'T** put real secrets in any committed file
142+
-**DON'T** commit production credentials
143+
144+
## GitHub Actions Integration
145+
146+
This environment system works seamlessly in GitHub Actions workflows. See [.github/ENV_SETUP.md](.github/ENV_SETUP.md) for detailed documentation.
147+
148+
### Quick Start
149+
150+
```yaml
151+
steps:
152+
- uses: actions/checkout@v4
153+
154+
- name: Load environment variables
155+
uses: ./.github/actions/setup-env
156+
157+
- name: Use environment variables
158+
run: echo "Version: $COMPILER_VERSION"
159+
```
160+
161+
The action automatically:
162+
163+
- Detects environment from branch name
164+
- Loads `.env` and `.env.$ENV` files
165+
- Exports variables to workflow
166+
167+
## Environment Variables Reference
168+
169+
See `.env.example` for a complete list of available variables and their purposes.

.env.development

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Development environment configuration
2+
# Loaded when on dev/develop branch
3+
# Secrets should go in .env.local instead
4+
5+
# Database URL for local development (SQLite)
6+
DATABASE_URL="file:./data/adblock.db"
7+
8+
# Cloudflare Turnstile test keys (always passes in testing)
9+
# Get production keys from https://dash.cloudflare.com/?to=/:account/turnstile
10+
TURNSTILE_SITE_KEY=1x00000000000000000000AA
11+
TURNSTILE_SECRET_KEY=1x0000000000000000000000000000000AA

.env.example

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,42 @@
1-
# Environment variables for Docker deployment
1+
# ============================================================================
2+
# Environment Variables Template
3+
# ============================================================================
4+
# Copy this file to .env.local and fill in your actual values
5+
#
6+
# Environment Loading Order (via .envrc):
7+
# 1. .env - Base configuration (committed)
8+
# 2. .env.$ENV - Environment-specific (.env.development or .env.production)
9+
# 3. .env.local - Local overrides and secrets (NOT committed)
10+
#
11+
# The $ENV variable is determined by your git branch:
12+
# - main/master → production
13+
# - dev/develop → development
14+
# - other branches → local (or branch-specific if .env.$BRANCH exists)
15+
# ============================================================================
216

3-
# Database URL for Prisma/SQLite storage
4-
# This is used by PrismaStorageAdapter for local caching
5-
# Default: file:./data/adblock.db
6-
DATABASE_URL=file:./data/adblock.db
17+
# ===== Database Configuration =====
18+
# Local development (SQLite)
19+
DATABASE_URL="file:./data/adblock.db"
720

8-
# Compiler version
9-
COMPILER_VERSION=0.8.3
21+
# Direct connection string for migrations and introspection
22+
DIRECT_DATABASE_URL="postgres://your_user:your_password@db.prisma.io:5432/postgres?sslmode=require&pool=true"
1023

11-
# Server port (default: 8787)
12-
PORT=8787
24+
# Prisma Accelerate connection string
25+
PRISMA_DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/?api_key=your_api_key_here"
1326

14-
# Deno cache directory (default: /app/.deno)
15-
DENO_DIR=/app/.deno
27+
# Wrangler Hyperdrive local connection
28+
WRANGLER_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE=postgresql://user:password@host:5432/postgres?sslmode=verify-full&sslrootcert=system
1629

17-
# Cloudflare Turnstile Configuration
30+
# ===== Cloudflare Turnstile =====
1831
# Get your keys from https://dash.cloudflare.com/?to=/:account/turnstile
1932
# Site key (public - used in the frontend widget)
20-
TURNSTILE_SITE_KEY=
33+
TURNSTILE_SITE_KEY=your_site_key_here
2134
# Secret key (private - used for server-side validation)
22-
TURNSTILE_SECRET_KEY=
35+
TURNSTILE_SECRET_KEY=your_secret_key_here
2336

37+
# ===== API Keys =====
2438
# Admin API key for storage management endpoints
25-
# Used to authenticate requests to /api/admin/* endpoints
26-
ADMIN_KEY=
27-
OPTIMIZE_API_KEY=
39+
ADMIN_KEY="your_admin_key_here"
40+
41+
# Optimize API key
42+
OPTIMIZE_API_KEY="your_optimize_api_key_here"

.env.production

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Production environment configuration
2+
# Loaded when on main/master branch
3+
#
4+
# This file is committed to git and must only contain non-secret defaults/placeholders.
5+
# Real production secrets must be provided via your deployment environment's secret management
6+
# (e.g. Cloudflare Wrangler `wrangler secret put` / dashboard), not via this file or other committed env files.
7+
8+
# Production database default - override via environment variables or .env.local for local development.
9+
# Do not store real production secrets in this file.
10+
DATABASE_URL=file:./data/adblock.db
11+
12+
# Cloudflare Turnstile - placeholder values only.
13+
# Get your keys from https://dash.cloudflare.com/?to=/:account/turnstile
14+
# In production (e.g. Cloudflare Workers), configure real keys via secrets / environment variables
15+
# such as `wrangler secret put`, not in .env.production or .env.local.
16+
# Leave empty to disable Turnstile verification (or set to test keys for development).
17+
TURNSTILE_SITE_KEY=
18+
TURNSTILE_SECRET_KEY=

.envrc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# ==============================
2+
# Universal direnv dotenv loader (Zsh layered merge)
3+
# ==============================
4+
5+
# Watch for changes in env files
6+
watch_file .env .env.local .env.development .env.production
7+
8+
# If inside a Git repo, also watch branch changes
9+
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
10+
watch_file "$(git rev-parse --show-toplevel)/.git/HEAD"
11+
12+
# Detect current branch
13+
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "default")
14+
15+
# Map branch to ENV
16+
case "$GIT_BRANCH" in
17+
main|master)
18+
ENV="production"
19+
;;
20+
develop|dev)
21+
ENV="development"
22+
;;
23+
*)
24+
if [[ -f ".env.$GIT_BRANCH" ]]; then
25+
ENV="$GIT_BRANCH"
26+
else
27+
ENV="local"
28+
fi
29+
;;
30+
esac
31+
else
32+
ENV="development"
33+
GIT_BRANCH="(no-git)"
34+
fi
35+
36+
# Load env vars using direnv's built-in dotenv loader (preserves proper quoting/escaping)
37+
# Files are loaded in a layered order without clearing existing variables.
38+
39+
# Load files in layered order
40+
dotenv_if_exists ".env"
41+
dotenv_if_exists ".env.$ENV"
42+
dotenv_if_exists ".env.local"
43+
44+
# Status message
45+
echo "✅ Loaded environment: $ENV (branch: $GIT_BRANCH)"

0 commit comments

Comments
 (0)