Skip to content

feat(api): enforce per-user hourly rate limits #7

feat(api): enforce per-user hourly rate limits

feat(api): enforce per-user hourly rate limits #7

Workflow file for this run

name: CI/CD
on:
push:
branches:
- main
pull_request:
branches:
- main
permissions:
contents: read
concurrency:
group: ci-cd-${{ github.ref }}
cancel-in-progress: false
jobs:
integration-tests:
name: Run Integration Tests
runs-on: ubuntu-26.04
steps:
- name: Checkout Code
uses: actions/checkout@v7
- name: Set Up Go
uses: actions/setup-go@v7
with:
go-version: stable
cache-dependency-path: |
api/go.sum
tests/go.sum
- name: Run Rate Limit Regression Tests
working-directory: ./api
run: go test -race ./pkg/services ./pkg/middlewares ./pkg/di -run TestRateLimit -count=1
- name: Generate Firebase Credentials
run: |
bash tests/generate-firebase-credentials.sh tests/firebase-credentials.json
echo "FIREBASE_CREDENTIALS=$(jq -c . tests/firebase-credentials.json)" >> $GITHUB_ENV
- name: Generate Adapter Certificates
run: bash tests/generate-adapter-certificates.sh tests/certs
- name: Start Services
working-directory: ./tests
run: docker compose up -d --build
- name: Wait for Healthy Services
working-directory: ./tests
run: |
echo "Waiting for MongoDB to be healthy..."
for i in $(seq 1 20); do
if docker compose exec mongodb mongosh --eval "db.runCommand('ping').ok" --quiet >/dev/null 2>&1; then
echo "MongoDB is healthy!"
break
fi
if [ $i -eq 20 ]; then
echo "MongoDB failed to become healthy"
docker compose logs mongodb
exit 1
fi
echo "MongoDB attempt $i/20 - waiting 3s..."
sleep 3
done
echo "Waiting for API to be healthy..."
for i in $(seq 1 40); do
if docker compose exec api curl -sf http://localhost:8000/health >/dev/null 2>&1; then
echo "API is healthy!"
break
fi
if [ $i -eq 40 ]; then
echo "API failed to become healthy"
docker compose logs api
exit 1
fi
echo "Attempt $i/40 - waiting 5s..."
sleep 5
done
- name: Seed Database
working-directory: ./tests
run: |
echo "Waiting for seed container to finish..."
SEED_CONTAINER_ID=$(docker compose ps --all --quiet seed)
if [ -z "$SEED_CONTAINER_ID" ]; then
echo "::error::Seed container not found"
exit 1
fi
SEED_EXIT_CODE=$(docker wait "$SEED_CONTAINER_ID")
if [ "$SEED_EXIT_CODE" != "0" ]; then
echo "::error::Database seeding failed with exit code $SEED_EXIT_CODE"
exit 1
fi
sleep 2
- name: Run Handler Integration Tests
working-directory: ./api
env:
USER_API_KEY: test-user-api-key
run: go test -tags integration -v -timeout 60s ./pkg/handlers
- name: Run End-to-End Integration Tests
working-directory: ./tests
run: go test -v -timeout 300s ./...
- name: Collect Service Logs on Failure
if: failure()
working-directory: ./tests
run: |
docker compose logs --tail 200
- name: Stop Services
if: always()
working-directory: ./tests
run: docker compose down -v
deploy-api:
name: Deploy API
runs-on: ubuntu-26.04
needs: integration-tests
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
timeout-minutes: 60
permissions:
contents: read
id-token: write
steps:
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v3
with:
workload_identity_provider: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }}
- name: Set Up Google Cloud SDK
uses: google-github-actions/setup-gcloud@v3
- name: Build and Deploy API
run: |
BUILD_ID=$(gcloud builds triggers run api-httpsms-com \
--region=global \
--project=httpsms-86c51 \
--sha=${{ github.sha }} \
--substitutions=_VERSION=${{ github.run_id }} \
--format="value(metadata.build.id)")
echo -e "Cloud Build: \033[34mhttps://console.cloud.google.com/cloud-build/builds/$BUILD_ID?project=httpsms-86c51\033[0m"
echo ""
echo "Polling Cloud Build Status..."
while true; do
STATUS=$(gcloud builds describe "$BUILD_ID" --region=global --project=httpsms-86c51 --format="value(status)")
LOCAL_TIME=$(date -u '+%Y-%m-%d %H:%M:%S UTC')
echo -e " \033[90m${LOCAL_TIME}\033[0m status=\033[36m${STATUS}\033[0m"
case "$STATUS" in
SUCCESS) echo -e "\033[32mBuild succeeded!\033[0m"; exit 0 ;;
FAILURE|TIMEOUT|CANCELLED|EXPIRED|INTERNAL_ERROR) echo -e "\033[31mBuild failed with status: $STATUS\033[0m"; exit 1 ;;
esac
sleep 30
done
build-web:
name: Build Web
needs: integration-tests
runs-on: ubuntu-26.04
defaults:
run:
working-directory: ./web
steps:
- name: Checkout Code
uses: actions/checkout@v7
- name: Set Up Node.js
uses: actions/setup-node@v7
with:
node-version: "24.18"
- name: Set Up pnpm
uses: pnpm/action-setup@v6
with:
version: 11.11.0
- name: Install Web Dependencies
run: pnpm install --trust-lockfile
- name: Lint Web
run: pnpm lint
- name: Run Web Tests
run: pnpm test
- name: Build Web
run: mv .env.production .env && pnpm run generate
- name: Upload Web Build
uses: actions/upload-artifact@v7
with:
name: web-build
path: web/.output/public
include-hidden-files: true
if-no-files-found: error
retention-days: 1
deploy-web-cloudflare:
name: Deploy Web to Cloudflare
needs: [deploy-api, build-web]
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-26.04
permissions:
contents: read
deployments: write
steps:
- name: Checkout Code
uses: actions/checkout@v7
- name: Set Up Node.js
uses: actions/setup-node@v7
with:
node-version: "24.18"
- name: Download Web Build
uses: actions/download-artifact@v8
with:
name: web-build
path: web/.output/public
- name: Deploy Web to Cloudflare
uses: cloudflare/wrangler-action@v4
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: pages deploy web/.output/public --project-name=httpsms
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
deploy-web-firebase:
name: Deploy Web to Firebase
needs: [deploy-api, build-web]
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-26.04
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout Code
uses: actions/checkout@v7
- name: Set Up Node.js
uses: actions/setup-node@v7
with:
node-version: "24.18"
- name: Download Web Build
uses: actions/download-artifact@v8
with:
name: web-build
path: web/.output/public
- name: Deploy Web to Firebase
uses: FirebaseExtended/action-hosting-deploy@500ac625ca2dd40cbd15f7659af953801858032a
with:
repoToken: "${{ secrets.GITHUB_TOKEN }}"
channelId: live
entryPoint: "./web"
firebaseServiceAccount: "${{ secrets.FIREBASE_SERVICE_ACCOUNT_HTTPSMS_86C51 }}"
projectId: httpsms-86c51