Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ target
.idea
.settings
.project
dependency-reduced-pom.xml
dependency-reduced-pom.xml
dist/
29 changes: 29 additions & 0 deletions _headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Cloudflare Pages – HTTP response headers
# https://developers.cloudflare.com/pages/configuration/headers/

# Default security headers for all responses
/*
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: geolocation=(), microphone=(), camera=()

# Long-lived cache for versioned static assets (scripts, styles, images)
/scripts/*
Cache-Control: public, max-age=31536000, immutable

/theme/*
Cache-Control: public, max-age=31536000, immutable

/img/*
Cache-Control: public, max-age=31536000, immutable

/aperture/*
Cache-Control: public, max-age=31536000, immutable

/bootstrap/*
Cache-Control: public, max-age=31536000, immutable

# Never cache the main HTML entry point
/index.html
Cache-Control: no-cache, no-store, must-revalidate
56 changes: 56 additions & 0 deletions build-cloudflare.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash
# build-cloudflare.sh – Build an Influent app WAR and extract its static
# assets into dist/ for deployment to Cloudflare Pages.
#
# Usage:
# ./build-cloudflare.sh [APP]
#
# APP – Maven module to build. One of: influent-app, bitcoin, kiva, walker
# Defaults to influent-app.
#
# After this script completes, deploy with:
# wrangler pages deploy dist/ --project-name influent
#
# Prerequisites: Java 8+, Maven 3.1+, unzip

set -euo pipefail

APP="${1:-influent-app}"

VALID_APPS="influent-app bitcoin kiva walker"
if ! echo "$VALID_APPS" | tr ' ' '\n' | grep -qx "$APP"; then
echo "ERROR: Unknown app '$APP'. Choose one of: $VALID_APPS" >&2
exit 1
fi

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

echo "==> Building $APP (this may take a few minutes)..."
mvn clean package \
--projects "$APP" \
--also-make \
--define skipTests \
--define environment=deployment \
--batch-mode \
--quiet

WAR_FILE=$(find "$APP/target" -maxdepth 1 -name "${APP}-*.war" 2>/dev/null | head -n 1)
if [ -z "$WAR_FILE" ]; then
echo "ERROR: No WAR file found in $APP/target/" >&2
exit 1
fi

echo "==> Extracting $WAR_FILE → dist/"
rm -rf dist
mkdir -p dist
unzip -q "$WAR_FILE" -d dist

# Remove Java-specific server directories – not needed for static hosting.
rm -rf dist/WEB-INF dist/META-INF

echo "==> dist/ is ready for Cloudflare Pages."
echo ""
echo " Deploy with:"
echo " wrangler pages project create influent # first time only"
echo " wrangler pages deploy dist/ --project-name influent"
34 changes: 34 additions & 0 deletions functions/influent/rest/[[path]].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Cloudflare Pages Function – proxy /influent/rest/* to the Java backend.
*
* Set the BACKEND_URL environment variable in the Cloudflare Pages dashboard
* to the base URL of your Java backend (e.g. https://api.example.com).
* Requests are forwarded as-is, preserving method, headers, and body.
*/
export async function onRequest(context) {
const { request, env } = context;

const backendUrl = env.BACKEND_URL;
if (!backendUrl) {
return new Response(
JSON.stringify({ error: 'BACKEND_URL environment variable is not configured' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}

const url = new URL(request.url);
const target = `${backendUrl.replace(/\/$/, '')}${url.pathname}${url.search}`;

const proxyHeaders = new Headers(request.headers);
proxyHeaders.set('X-Forwarded-Host', url.host);
proxyHeaders.set('X-Forwarded-Proto', url.protocol.replace(':', ''));

// Clone the request before reading its body to avoid consuming the stream.
const cloned = request.clone();
return fetch(target, {
method: cloned.method,
headers: proxyHeaders,
body: ['GET', 'HEAD'].includes(cloned.method) ? undefined : cloned.body,
redirect: 'follow',
});
}
34 changes: 34 additions & 0 deletions functions/influent/rpc/[[path]].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Cloudflare Pages Function – proxy /influent/rpc/* to the Java backend.
*
* Set the BACKEND_URL environment variable in the Cloudflare Pages dashboard
* to the base URL of your Java backend (e.g. https://api.example.com).
* Requests are forwarded as-is, preserving method, headers, and body.
*/
export async function onRequest(context) {
const { request, env } = context;

const backendUrl = env.BACKEND_URL;
if (!backendUrl) {
return new Response(
JSON.stringify({ error: 'BACKEND_URL environment variable is not configured' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}

const url = new URL(request.url);
const target = `${backendUrl.replace(/\/$/, '')}${url.pathname}${url.search}`;

const proxyHeaders = new Headers(request.headers);
proxyHeaders.set('X-Forwarded-Host', url.host);
proxyHeaders.set('X-Forwarded-Proto', url.protocol.replace(':', ''));

// Clone the request before reading its body to avoid consuming the stream.
const cloned = request.clone();
return fetch(target, {
method: cloned.method,
headers: proxyHeaders,
body: ['GET', 'HEAD'].includes(cloned.method) ? undefined : cloned.body,
redirect: 'follow',
});
}
29 changes: 29 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Cloudflare Pages configuration for Influent
# https://developers.cloudflare.com/pages/
#
# Deployment steps:
# 1. Build static assets locally:
# ./build-cloudflare.sh influent-app
# 2. Deploy to Cloudflare Pages (first time):
# wrangler pages project create influent
# 3. Deploy build output:
# wrangler pages deploy dist/ --project-name influent
#
# Required environment variable (set in Cloudflare Pages dashboard):
# BACKEND_URL – base URL of the running Java backend, e.g.
# https://api.example.com
# The backend must be reachable from Cloudflare's network.
#
# Tip: for CI/CD, add the build command and output directory in the
# Cloudflare Pages dashboard or in a GitHub Actions workflow. The build
# environment must have Java 8+ and Maven 3.1+ available; install them
# via the dashboard's "Install command" if needed.

name = "influent"
pages_build_output_dir = "dist"
compatibility_date = "2024-01-01"

[vars]
# Default placeholder – override this in the Cloudflare Pages dashboard
# under Settings > Environment variables.
BACKEND_URL = "http://localhost:8080"
Loading