Spaces:
Running
Running
refactor: replace UptimeRobot with Cloudflare Workers for service keep-alive mechanism
Browse files- .env.example +5 -7
- Dockerfile +2 -2
- README.md +8 -10
- cloudflare-keepalive-setup.py +212 -0
- health-server.js +202 -236
- setup-uptimerobot.sh +0 -91
- start.sh +3 -3
.env.example
CHANGED
|
@@ -56,18 +56,16 @@ STORAGE_PROVIDER=local
|
|
| 56 |
# CLOUDFLARE_REGION=auto
|
| 57 |
|
| 58 |
|
| 59 |
-
# ── Cloudflare
|
|
|
|
|
|
|
|
|
|
| 60 |
# CLOUDFLARE_WORKERS_TOKEN=
|
|
|
|
| 61 |
# Extra domains to proxy, merged with built-in defaults (Telegram, Discord, WhatsApp,
|
| 62 |
# Facebook, Google). Comma-separated. Set to "*" to proxy ALL external traffic.
|
| 63 |
-
# Leave unset to proxy only the built-in default domains.
|
| 64 |
# CLOUDFLARE_PROXY_DOMAINS=api.sendgrid.com,smtp.resend.com,slack.com
|
| 65 |
|
| 66 |
-
# ── UptimeRobot keep-alive ───────────────────────────────────────────────────
|
| 67 |
-
# Add your Main API key (NOT Read-only or Monitor-specific).
|
| 68 |
-
# Monitor is created automatically at boot. Status shown on the dashboard.
|
| 69 |
-
# UPTIMEROBOT_API_KEY=ur_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
| 70 |
-
|
| 71 |
|
| 72 |
# ── Email (Optional — controls signup activation flow) ──────────────────────
|
| 73 |
# Without RESEND_API_KEY, new signups are auto-activated.
|
|
|
|
| 56 |
# CLOUDFLARE_REGION=auto
|
| 57 |
|
| 58 |
|
| 59 |
+
# ── Cloudflare Keep-Alive & Proxy ───────────────────────────────────────────
|
| 60 |
+
# Create an API token at https://dash.cloudflare.com/profile/api-tokens
|
| 61 |
+
# with "Workers Scripts: Edit" permission.
|
| 62 |
+
# Enables both transparent outbound proxy and Keep-Alive cron monitor.
|
| 63 |
# CLOUDFLARE_WORKERS_TOKEN=
|
| 64 |
+
|
| 65 |
# Extra domains to proxy, merged with built-in defaults (Telegram, Discord, WhatsApp,
|
| 66 |
# Facebook, Google). Comma-separated. Set to "*" to proxy ALL external traffic.
|
|
|
|
| 67 |
# CLOUDFLARE_PROXY_DOMAINS=api.sendgrid.com,smtp.resend.com,slack.com
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# ── Email (Optional — controls signup activation flow) ──────────────────────
|
| 71 |
# Without RESEND_API_KEY, new signups are auto-activated.
|
Dockerfile
CHANGED
|
@@ -182,7 +182,7 @@ COPY postiz-sync.py /opt/postiz-sync.py
|
|
| 182 |
COPY cloudflare-proxy.js /opt/cloudflare-proxy.js
|
| 183 |
COPY cloudflare-proxy-setup.py /opt/cloudflare-proxy-setup.py
|
| 184 |
COPY cloudflare-worker.js /opt/cloudflare-worker.js
|
| 185 |
-
COPY
|
| 186 |
|
| 187 |
# Vendor fonts + patch script available at runtime.
|
| 188 |
# Stage 1 may be cached from before the font patch was added; start.sh applies
|
|
@@ -194,7 +194,7 @@ COPY vendor/fonts/PlusJakartaSans-500-normal.woff2 \
|
|
| 194 |
/opt/vendor/fonts/
|
| 195 |
COPY vendor/patch-jakarta-font.js /opt/vendor/patch-jakarta-font.js
|
| 196 |
|
| 197 |
-
RUN chmod +x /opt/start.sh /opt/
|
| 198 |
|
| 199 |
EXPOSE 7860
|
| 200 |
|
|
|
|
| 182 |
COPY cloudflare-proxy.js /opt/cloudflare-proxy.js
|
| 183 |
COPY cloudflare-proxy-setup.py /opt/cloudflare-proxy-setup.py
|
| 184 |
COPY cloudflare-worker.js /opt/cloudflare-worker.js
|
| 185 |
+
COPY cloudflare-keepalive-setup.py /opt/cloudflare-keepalive-setup.py
|
| 186 |
|
| 187 |
# Vendor fonts + patch script available at runtime.
|
| 188 |
# Stage 1 may be cached from before the font patch was added; start.sh applies
|
|
|
|
| 194 |
/opt/vendor/fonts/
|
| 195 |
COPY vendor/patch-jakarta-font.js /opt/vendor/patch-jakarta-font.js
|
| 196 |
|
| 197 |
+
RUN chmod +x /opt/start.sh /opt/cloudflare-keepalive-setup.py
|
| 198 |
|
| 199 |
EXPOSE 7860
|
| 200 |
|
README.md
CHANGED
|
@@ -11,9 +11,7 @@ secrets:
|
|
| 11 |
- name: HF_TOKEN
|
| 12 |
description: HF token with WRITE access — enables DB+uploads backup persistence to a private HF Dataset.
|
| 13 |
- name: CLOUDFLARE_WORKERS_TOKEN
|
| 14 |
-
description: Cloudflare API token to auto-provision an outbound proxy.
|
| 15 |
-
- name: UPTIMEROBOT_API_KEY
|
| 16 |
-
description: UptimeRobot API key for automatic monitor setup.
|
| 17 |
---
|
| 18 |
|
| 19 |
[](https://github.com/somratpro/huggingpost)
|
|
@@ -41,7 +39,7 @@ secrets:
|
|
| 41 |
- 📅 **30+ Social Platforms** — schedule posts to X, LinkedIn, Facebook, Threads, TikTok, YouTube, Reddit, Mastodon, Discord, Slack, Pinterest, etc.
|
| 42 |
- ⚡ **One-click deploy** — duplicate the Space, add `HF_TOKEN`, you're done.
|
| 43 |
- 💾 **Persistent across restarts** — PostgreSQL + uploaded media auto-backed up to a private HF Dataset every 5 min and restored on boot.
|
| 44 |
-
- 💓 **Keep-Alive** — add `
|
| 45 |
- 🌐 **Outbound firewall workaround** — optional Cloudflare Worker proxy auto-provisioned for blocked platform APIs.
|
| 46 |
- 🔒 **Secrets generated** — `JWT_SECRET` auto-generated on first boot and persisted, no manual setup.
|
| 47 |
- 🏠 **100% HF-Native** — no external Postgres/Redis/storage accounts needed for the default path.
|
|
@@ -74,7 +72,7 @@ Land on the HuggingPost dashboard. Click **Open Postiz →** to reach the login
|
|
| 74 |
|
| 75 |
### Step 5: Set Up Keep-Alive (1 min)
|
| 76 |
|
| 77 |
-
Add your
|
| 78 |
|
| 79 |
## 🔑 Configuration
|
| 80 |
|
|
@@ -137,7 +135,7 @@ huggingface-cli download --repo-type dataset <your-username>/huggingpost-backup
|
|
| 137 |
|
| 138 |
## 💓 Keep It Awake
|
| 139 |
|
| 140 |
-
Free HF Spaces sleep after ~48h of no traffic. A sleeping Space cannot fire scheduled posts. Add your
|
| 141 |
|
| 142 |
## 🌐 Cloudflare Proxy *(Optional)*
|
| 143 |
|
|
@@ -177,7 +175,7 @@ HuggingPost/
|
|
| 177 |
├── cloudflare-proxy.js # Transparent outbound proxy injected via NODE_OPTIONS
|
| 178 |
├── cloudflare-proxy-setup.py
|
| 179 |
├── cloudflare-worker.js
|
| 180 |
-
├──
|
| 181 |
├── docker-compose.yml # Local dev convenience
|
| 182 |
├── .env.example # Configuration reference
|
| 183 |
└── README.md
|
|
@@ -187,7 +185,7 @@ HuggingPost/
|
|
| 187 |
|
| 188 |
| Path | Target | Notes |
|
| 189 |
| :--- | :--- | :--- |
|
| 190 |
-
| `/` | HuggingPost dashboard (local) | Status +
|
| 191 |
| `/health`, `/status` | local | JSON handlers |
|
| 192 |
| `/app` or `/app/*` | Postiz nginx `:5000` | `/app` stripped — Next.js built with `basePath="/app"` |
|
| 193 |
| `/_next/*`, `/static/*` | 301 → `/app/<path>` | Catches absolute-URL leaks |
|
|
@@ -224,7 +222,7 @@ On restarts after the first boot, wait 30–90 s for PM2 processes to come up. C
|
|
| 224 |
Either move media to Cloudflare R2 (`STORAGE_PROVIDER=cloudflare`) or raise `SYNC_MAX_FILE_BYTES`. The HF Dataset itself supports much larger files, but huge backups slow restart.
|
| 225 |
|
| 226 |
**Scheduled posts didn't fire while I was away**
|
| 227 |
-
The Space slept. Add `
|
| 228 |
|
| 229 |
**OAuth callback fails for X/Facebook/LinkedIn**
|
| 230 |
Some platforms reject `*.hf.space` subdomains as redirect URIs. You may need to put a custom domain in front (Cloudflare → HF Space CNAME).
|
|
@@ -240,7 +238,7 @@ Usually means Postgres didn't finish starting. Container will exit and HF will a
|
|
| 240 |
- [Postiz on GitHub](https://github.com/gitroomhq/postiz-app)
|
| 241 |
- [Postiz docs](https://docs.postiz.com)
|
| 242 |
- [HuggingFace Spaces docs](https://huggingface.co/docs/hub/spaces)
|
| 243 |
-
-
|
| 244 |
|
| 245 |
## 📄 License
|
| 246 |
|
|
|
|
| 11 |
- name: HF_TOKEN
|
| 12 |
description: HF token with WRITE access — enables DB+uploads backup persistence to a private HF Dataset.
|
| 13 |
- name: CLOUDFLARE_WORKERS_TOKEN
|
| 14 |
+
description: Cloudflare API token to auto-provision an outbound proxy and the Keep-Alive monitor.
|
|
|
|
|
|
|
| 15 |
---
|
| 16 |
|
| 17 |
[](https://github.com/somratpro/huggingpost)
|
|
|
|
| 39 |
- 📅 **30+ Social Platforms** — schedule posts to X, LinkedIn, Facebook, Threads, TikTok, YouTube, Reddit, Mastodon, Discord, Slack, Pinterest, etc.
|
| 40 |
- ⚡ **One-click deploy** — duplicate the Space, add `HF_TOKEN`, you're done.
|
| 41 |
- 💾 **Persistent across restarts** — PostgreSQL + uploaded media auto-backed up to a private HF Dataset every 5 min and restored on boot.
|
| 42 |
+
- 💓 **Keep-Alive** — add `CLOUDFLARE_WORKERS_TOKEN` as a Space secret and a cron monitor is created automatically via Cloudflare Workers.
|
| 43 |
- 🌐 **Outbound firewall workaround** — optional Cloudflare Worker proxy auto-provisioned for blocked platform APIs.
|
| 44 |
- 🔒 **Secrets generated** — `JWT_SECRET` auto-generated on first boot and persisted, no manual setup.
|
| 45 |
- 🏠 **100% HF-Native** — no external Postgres/Redis/storage accounts needed for the default path.
|
|
|
|
| 72 |
|
| 73 |
### Step 5: Set Up Keep-Alive (1 min)
|
| 74 |
|
| 75 |
+
Add your **Cloudflare Workers Token** as a Space secret named `CLOUDFLARE_WORKERS_TOKEN`. HuggingPost will automatically create a Worker cron that pings `/health` every 10 min. Without this, the Space will sleep and scheduled posts won't fire.
|
| 76 |
|
| 77 |
## 🔑 Configuration
|
| 78 |
|
|
|
|
| 135 |
|
| 136 |
## 💓 Keep It Awake
|
| 137 |
|
| 138 |
+
Free HF Spaces sleep after ~48h of no traffic. A sleeping Space cannot fire scheduled posts. Add your `CLOUDFLARE_WORKERS_TOKEN` as a Space secret. HuggingPost will automatically create a Cloudflare Worker cron that pings the Space every 10 minutes to keep it active. The dashboard shows the current "Keep Awake" status.
|
| 139 |
|
| 140 |
## 🌐 Cloudflare Proxy *(Optional)*
|
| 141 |
|
|
|
|
| 175 |
├── cloudflare-proxy.js # Transparent outbound proxy injected via NODE_OPTIONS
|
| 176 |
├── cloudflare-proxy-setup.py
|
| 177 |
├── cloudflare-worker.js
|
| 178 |
+
├── cloudflare-keepalive-setup.py
|
| 179 |
├── docker-compose.yml # Local dev convenience
|
| 180 |
├── .env.example # Configuration reference
|
| 181 |
└── README.md
|
|
|
|
| 185 |
|
| 186 |
| Path | Target | Notes |
|
| 187 |
| :--- | :--- | :--- |
|
| 188 |
+
| `/` | HuggingPost dashboard (local) | Status + Keep Awake tile |
|
| 189 |
| `/health`, `/status` | local | JSON handlers |
|
| 190 |
| `/app` or `/app/*` | Postiz nginx `:5000` | `/app` stripped — Next.js built with `basePath="/app"` |
|
| 191 |
| `/_next/*`, `/static/*` | 301 → `/app/<path>` | Catches absolute-URL leaks |
|
|
|
|
| 222 |
Either move media to Cloudflare R2 (`STORAGE_PROVIDER=cloudflare`) or raise `SYNC_MAX_FILE_BYTES`. The HF Dataset itself supports much larger files, but huge backups slow restart.
|
| 223 |
|
| 224 |
**Scheduled posts didn't fire while I was away**
|
| 225 |
+
The Space slept. Add `CLOUDFLARE_WORKERS_TOKEN` as a Space secret to enable automatic keep-awake monitoring.
|
| 226 |
|
| 227 |
**OAuth callback fails for X/Facebook/LinkedIn**
|
| 228 |
Some platforms reject `*.hf.space` subdomains as redirect URIs. You may need to put a custom domain in front (Cloudflare → HF Space CNAME).
|
|
|
|
| 238 |
- [Postiz on GitHub](https://github.com/gitroomhq/postiz-app)
|
| 239 |
- [Postiz docs](https://docs.postiz.com)
|
| 240 |
- [HuggingFace Spaces docs](https://huggingface.co/docs/hub/spaces)
|
| 241 |
+
- More projects: [Hugging8n](https://huggingface.co/spaces/somratpro/Hugging8n) (n8n), [HuggingClip](https://huggingface.co/spaces/somratpro/HuggingClip) (Paperclip), [HuggingClaw](https://huggingface.co/spaces/somratpro/HuggingClaw) (OpenClaw), [HuggingMess](https://huggingface.co/spaces/somratpro/HuggingMess) (Messari)
|
| 242 |
|
| 243 |
## 📄 License
|
| 244 |
|
cloudflare-keepalive-setup.py
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
"""Create or reuse a Cloudflare Worker for Space keep-awake."""
|
| 5 |
+
|
| 6 |
+
import json
|
| 7 |
+
import os
|
| 8 |
+
import re
|
| 9 |
+
import sys
|
| 10 |
+
import time
|
| 11 |
+
import urllib.request
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
|
| 14 |
+
API_BASE = "https://api.cloudflare.com/client/v4"
|
| 15 |
+
KEEPALIVE_STATUS_FILE = Path("/tmp/huggingpost-cloudflare-keepalive-status.json")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def cf_request(method: str, path: str, token: str, body: bytes | None = None, content_type: str = "application/json"):
|
| 19 |
+
req = urllib.request.Request(
|
| 20 |
+
f"{API_BASE}{path}",
|
| 21 |
+
data=body,
|
| 22 |
+
method=method,
|
| 23 |
+
headers={"Authorization": f"Bearer {token}", "Content-Type": content_type},
|
| 24 |
+
)
|
| 25 |
+
with urllib.request.urlopen(req, timeout=30) as response:
|
| 26 |
+
payload = json.loads(response.read().decode("utf-8"))
|
| 27 |
+
if not payload.get("success"):
|
| 28 |
+
errors = payload.get("errors") or [{"message": "Unknown Cloudflare API error"}]
|
| 29 |
+
raise RuntimeError(errors[0].get("message", "Unknown Cloudflare API error"))
|
| 30 |
+
return payload["result"]
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def slugify(value: str) -> str:
|
| 34 |
+
cleaned = re.sub(r"[^a-z0-9-]+", "-", value.lower()).strip("-")
|
| 35 |
+
cleaned = re.sub(r"-{2,}", "-", cleaned)
|
| 36 |
+
return (cleaned or "huggingpost-proxy")[:63].rstrip("-")
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def get_space_host() -> str:
|
| 40 |
+
space_host = os.environ.get("SPACE_HOST", "").strip()
|
| 41 |
+
if space_host:
|
| 42 |
+
return space_host
|
| 43 |
+
|
| 44 |
+
author = os.environ.get("SPACE_AUTHOR_NAME", "").strip()
|
| 45 |
+
repo = os.environ.get("SPACE_REPO_NAME", "").strip()
|
| 46 |
+
if author and repo:
|
| 47 |
+
return f"{author}-{repo}.hf.space".lower()
|
| 48 |
+
|
| 49 |
+
return ""
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def derive_keepalive_worker_name() -> str:
|
| 53 |
+
explicit = os.environ.get("CLOUDFLARE_KEEPALIVE_WORKER_NAME", "").strip()
|
| 54 |
+
if explicit:
|
| 55 |
+
return slugify(explicit)
|
| 56 |
+
space_host = get_space_host()
|
| 57 |
+
if space_host:
|
| 58 |
+
return slugify(f"{space_host.replace('.hf.space', '')}-keepalive")
|
| 59 |
+
return "huggingpost-keepalive"
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def render_keepalive_worker(target_url: str) -> str:
|
| 63 |
+
return f"""addEventListener("fetch", (event) => {{
|
| 64 |
+
event.respondWith(handleRequest(event.request));
|
| 65 |
+
}});
|
| 66 |
+
|
| 67 |
+
addEventListener("scheduled", (event) => {{
|
| 68 |
+
event.waitUntil(ping("cron"));
|
| 69 |
+
}});
|
| 70 |
+
|
| 71 |
+
const TARGET_URL = {json.dumps(target_url)};
|
| 72 |
+
|
| 73 |
+
async function ping(source) {{
|
| 74 |
+
const startedAt = new Date().toISOString();
|
| 75 |
+
try {{
|
| 76 |
+
const response = await fetch(TARGET_URL, {{
|
| 77 |
+
method: "GET",
|
| 78 |
+
headers: {{
|
| 79 |
+
"user-agent": "HuggingPost Cloudflare KeepAlive",
|
| 80 |
+
"cache-control": "no-cache"
|
| 81 |
+
}},
|
| 82 |
+
cf: {{ cacheTtl: 0, cacheEverything: false }}
|
| 83 |
+
}});
|
| 84 |
+
return {{
|
| 85 |
+
ok: response.ok,
|
| 86 |
+
status: response.status,
|
| 87 |
+
source,
|
| 88 |
+
target: TARGET_URL,
|
| 89 |
+
timestamp: startedAt
|
| 90 |
+
}};
|
| 91 |
+
}} catch (error) {{
|
| 92 |
+
return {{
|
| 93 |
+
ok: false,
|
| 94 |
+
status: 0,
|
| 95 |
+
source,
|
| 96 |
+
target: TARGET_URL,
|
| 97 |
+
timestamp: startedAt,
|
| 98 |
+
error: error.message
|
| 99 |
+
}};
|
| 100 |
+
}}
|
| 101 |
+
}}
|
| 102 |
+
|
| 103 |
+
async function handleRequest(request) {{
|
| 104 |
+
const url = new URL(request.url);
|
| 105 |
+
if (url.pathname === "/" || url.pathname === "/health" || url.pathname === "/ping") {{
|
| 106 |
+
const result = await ping("manual");
|
| 107 |
+
return new Response(JSON.stringify(result, null, 2), {{
|
| 108 |
+
status: result.ok ? 200 : 502,
|
| 109 |
+
headers: {{ "content-type": "application/json; charset=utf-8" }}
|
| 110 |
+
}});
|
| 111 |
+
}}
|
| 112 |
+
return new Response("Not found", {{ status: 404 }});
|
| 113 |
+
}}
|
| 114 |
+
"""
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def write_keepalive_status(payload: dict) -> None:
|
| 118 |
+
payload = {
|
| 119 |
+
**payload,
|
| 120 |
+
"timestamp": payload.get("timestamp") or time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
| 121 |
+
}
|
| 122 |
+
KEEPALIVE_STATUS_FILE.write_text(json.dumps(payload), encoding="utf-8")
|
| 123 |
+
try:
|
| 124 |
+
KEEPALIVE_STATUS_FILE.chmod(0o600)
|
| 125 |
+
except OSError:
|
| 126 |
+
pass
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def resolve_account_and_subdomain(api_token: str) -> tuple[str, str]:
|
| 130 |
+
account_id = os.environ.get("CLOUDFLARE_ACCOUNT_ID", "").strip()
|
| 131 |
+
if not account_id:
|
| 132 |
+
accounts = cf_request("GET", "/accounts", api_token)
|
| 133 |
+
if not accounts:
|
| 134 |
+
raise RuntimeError("No Cloudflare account is available for this token.")
|
| 135 |
+
account_id = accounts[0]["id"]
|
| 136 |
+
|
| 137 |
+
subdomain_info = cf_request("GET", f"/accounts/{account_id}/workers/subdomain", api_token)
|
| 138 |
+
subdomain = (subdomain_info or {}).get("subdomain", "").strip()
|
| 139 |
+
if not subdomain:
|
| 140 |
+
raise RuntimeError("Cloudflare Workers subdomain is not configured. Enable workers.dev first.")
|
| 141 |
+
return account_id, subdomain
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def setup_keepalive_worker(api_token: str, account_id: str, subdomain: str) -> None:
|
| 145 |
+
enabled = os.environ.get("CLOUDFLARE_KEEPALIVE_ENABLED", "true").strip().lower()
|
| 146 |
+
if enabled in {"0", "false", "no", "off"}:
|
| 147 |
+
write_keepalive_status({"configured": False, "status": "disabled", "message": "Cloudflare keep-awake is disabled."})
|
| 148 |
+
return
|
| 149 |
+
|
| 150 |
+
space_host = get_space_host()
|
| 151 |
+
if not space_host:
|
| 152 |
+
write_keepalive_status({"configured": False, "status": "skipped", "message": "SPACE_HOST could not be determined."})
|
| 153 |
+
return
|
| 154 |
+
|
| 155 |
+
cron = os.environ.get("CLOUDFLARE_KEEPALIVE_CRON", "*/10 * * * *").strip()
|
| 156 |
+
space_host = space_host.removeprefix("https://").removeprefix("http://").split("/")[0]
|
| 157 |
+
target_url = os.environ.get("CLOUDFLARE_KEEPALIVE_URL", f"https://{space_host}/health").strip()
|
| 158 |
+
worker_name = derive_keepalive_worker_name()
|
| 159 |
+
worker_source = render_keepalive_worker(target_url)
|
| 160 |
+
|
| 161 |
+
cf_request(
|
| 162 |
+
"PUT",
|
| 163 |
+
f"/accounts/{account_id}/workers/scripts/{worker_name}",
|
| 164 |
+
api_token,
|
| 165 |
+
body=worker_source.encode("utf-8"),
|
| 166 |
+
content_type="application/javascript",
|
| 167 |
+
)
|
| 168 |
+
cf_request(
|
| 169 |
+
"POST",
|
| 170 |
+
f"/accounts/{account_id}/workers/scripts/{worker_name}/subdomain",
|
| 171 |
+
api_token,
|
| 172 |
+
body=json.dumps({"enabled": True, "previews_enabled": True}).encode("utf-8"),
|
| 173 |
+
)
|
| 174 |
+
cf_request(
|
| 175 |
+
"PUT",
|
| 176 |
+
f"/accounts/{account_id}/workers/scripts/{worker_name}/schedules",
|
| 177 |
+
api_token,
|
| 178 |
+
body=json.dumps([{"cron": cron}]).encode("utf-8"),
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
worker_url = f"https://{worker_name}.{subdomain}.workers.dev"
|
| 182 |
+
write_keepalive_status(
|
| 183 |
+
{
|
| 184 |
+
"configured": True,
|
| 185 |
+
"status": "configured",
|
| 186 |
+
"workerName": worker_name,
|
| 187 |
+
"workerUrl": worker_url,
|
| 188 |
+
"targetUrl": target_url,
|
| 189 |
+
"cron": cron,
|
| 190 |
+
"message": f"Cloudflare Worker cron pings {target_url} on {cron}.",
|
| 191 |
+
}
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
def main() -> int:
|
| 196 |
+
api_token = os.environ.get("CLOUDFLARE_WORKERS_TOKEN", "").strip()
|
| 197 |
+
|
| 198 |
+
if not api_token:
|
| 199 |
+
return 0
|
| 200 |
+
|
| 201 |
+
try:
|
| 202 |
+
account_id, subdomain = resolve_account_and_subdomain(api_token)
|
| 203 |
+
setup_keepalive_worker(api_token, account_id, subdomain)
|
| 204 |
+
return 0
|
| 205 |
+
except Exception as exc:
|
| 206 |
+
print(f"Cloudflare keepalive setup failed: {exc}", file=sys.stderr)
|
| 207 |
+
write_keepalive_status({"configured": False, "status": "error", "message": str(exc)})
|
| 208 |
+
return 1
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
if __name__ == "__main__":
|
| 212 |
+
raise SystemExit(main())
|
health-server.js
CHANGED
|
@@ -43,8 +43,7 @@ const POSTIZ_PORT = 5000;
|
|
| 43 |
const startTime = Date.now();
|
| 44 |
const HF_BACKUP_ENABLED = !!process.env.HF_TOKEN;
|
| 45 |
const SYNC_INTERVAL = process.env.SYNC_INTERVAL || "300";
|
| 46 |
-
const
|
| 47 |
-
const UPTIMEROBOT_API_KEY_SET = !!process.env.UPTIMEROBOT_API_KEY;
|
| 48 |
|
| 49 |
// Social platform env-var presence check (for dashboard status grid).
|
| 50 |
// Each entry: { name, emoji, ready: bool, setupUrl, envVars, noOAuth }
|
|
@@ -573,15 +572,39 @@ function isLocalRoute(pathname) {
|
|
| 573 |
// UptimeRobot helpers
|
| 574 |
// ============================================================================
|
| 575 |
|
| 576 |
-
function
|
| 577 |
try {
|
| 578 |
-
if (fs.existsSync(
|
| 579 |
-
return JSON.parse(fs.readFileSync(
|
| 580 |
}
|
| 581 |
} catch {}
|
| 582 |
return null;
|
| 583 |
}
|
| 584 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 585 |
// ============================================================================
|
| 586 |
// Status helpers
|
| 587 |
// ============================================================================
|
|
@@ -626,34 +649,42 @@ function formatUptime(seconds) {
|
|
| 626 |
// Dashboard HTML
|
| 627 |
// ============================================================================
|
| 628 |
|
| 629 |
-
function renderDashboard(
|
| 630 |
-
const syncStatus =
|
| 631 |
-
const
|
| 632 |
-
|
| 633 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 634 |
const platforms = getSocialPlatforms();
|
| 635 |
const readyNow = platforms.filter(p => p.noOAuth);
|
| 636 |
const needsSetup = platforms.filter(p => !p.noOAuth);
|
| 637 |
const configuredCount = needsSetup.filter(p => p.ready).length;
|
| 638 |
|
| 639 |
-
const syncBadge = !hasBackup
|
| 640 |
-
? `<span class="badge badge-off">Disabled</span>`
|
| 641 |
-
: syncError
|
| 642 |
-
? `<span class="badge badge-err">Error</span>`
|
| 643 |
-
: syncStatus.last_sync_time
|
| 644 |
-
? `<span class="badge badge-on"><i class="dot"></i>Syncing</span>`
|
| 645 |
-
: `<span class="badge badge-wait"><i class="dot" style="background:#3b82f6"></i>Pending</span>`;
|
| 646 |
-
|
| 647 |
-
const postizBadge = initialData.postizRunning
|
| 648 |
-
? `<span class="badge badge-on"><i class="dot"></i>Running</span>`
|
| 649 |
-
: `<span class="badge badge-off">Booting…</span>`;
|
| 650 |
-
|
| 651 |
const needsSetupRows = needsSetup.map(p => {
|
| 652 |
if (p.ready) {
|
| 653 |
return `<div class="plat-row ready">
|
| 654 |
<span class="plat-icon">${p.emoji}</span>
|
| 655 |
<span class="plat-name">${p.name}</span>
|
| 656 |
-
<span class="badge
|
| 657 |
</div>`;
|
| 658 |
}
|
| 659 |
return `<div class="plat-row">
|
|
@@ -670,231 +701,165 @@ function renderDashboard(initialData) {
|
|
| 670 |
<span style="font-size:0.75rem;color:var(--dim)">${p.note || ""}</span>
|
| 671 |
</div>`).join("");
|
| 672 |
|
| 673 |
-
const
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 684 |
<html lang="en">
|
| 685 |
<head>
|
| 686 |
-
<meta charset="
|
| 687 |
-
<meta name="viewport" content="width=device-width, initial-scale=1
|
| 688 |
-
<title>HuggingPost
|
| 689 |
-
<
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
.
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
.
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
.
|
| 707 |
-
.
|
| 708 |
-
.
|
| 709 |
-
.
|
| 710 |
-
|
| 711 |
-
.
|
| 712 |
-
.
|
| 713 |
-
.
|
| 714 |
-
.
|
| 715 |
-
.
|
| 716 |
-
|
| 717 |
-
.
|
| 718 |
-
.
|
| 719 |
-
|
| 720 |
-
.steps{
|
| 721 |
-
.steps li{
|
| 722 |
-
.steps li:
|
| 723 |
-
.steps
|
| 724 |
-
|
| 725 |
-
.
|
| 726 |
-
|
| 727 |
-
.
|
| 728 |
-
.
|
| 729 |
-
.
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
.
|
| 733 |
-
|
| 734 |
-
.
|
| 735 |
-
.
|
| 736 |
-
.
|
| 737 |
-
.
|
| 738 |
-
.
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
.
|
| 744 |
-
|
| 745 |
-
</style>
|
| 746 |
</head>
|
| 747 |
<body>
|
| 748 |
-
<
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
<div class="
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
<
|
| 772 |
-
<div
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
<
|
| 778 |
-
<div
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
<div class="s-title">Enable LinkedIn, X, YouTube… (optional)</div>
|
| 785 |
-
<div class="s-note">These require a free API key from each platform. Use the <a href="/setup" style="color:#f472b6">Setup Guide →</a> for step-by-step instructions per platform, then add the keys as <a href="https://huggingface.co/spaces/${process.env.SPACE_ID || "your-space"}/settings" target="_blank">Space secrets</a>.</div>
|
| 786 |
-
</div>
|
| 787 |
-
</li>
|
| 788 |
-
<li>
|
| 789 |
-
<div>
|
| 790 |
-
<div class="s-title">Keep your Space awake (optional)</div>
|
| 791 |
-
<div class="s-note">HF Spaces sleep after inactivity — scheduled posts won't fire while sleeping. Add <code>UPTIMEROBOT_API_KEY</code> to auto-create a free uptime monitor, or upgrade to a paid HF Space.</div>
|
| 792 |
-
</div>
|
| 793 |
-
</li>
|
| 794 |
-
</ol>
|
| 795 |
-
</div>
|
| 796 |
-
|
| 797 |
-
<!-- Platforms ready now -->
|
| 798 |
-
<div class="card">
|
| 799 |
-
<button class="section-toggle open" onclick="toggle(this,'ready-list')">
|
| 800 |
-
✅ Works immediately — no API keys needed (${readyNow.length} platforms)
|
| 801 |
-
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M6 9l6 6 6-6"/></svg>
|
| 802 |
-
</button>
|
| 803 |
-
<div id="ready-list" class="collapse open">
|
| 804 |
-
${readyNowRows}
|
| 805 |
-
<div class="sync-note" style="margin-top:10px">Connect these inside Postiz → <strong>Add Channel</strong> after signing in.</div>
|
| 806 |
</div>
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
🔑 Needs API keys — ${configuredCount}/${needsSetup.length} configured
|
| 813 |
-
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M6 9l6 6 6-6"/></svg>
|
| 814 |
-
</button>
|
| 815 |
-
<div id="oauth-list" class="collapse open">
|
| 816 |
-
${needsSetupRows}
|
| 817 |
-
<div class="sync-note" style="margin-top:10px">
|
| 818 |
-
After getting API keys: go to your <a href="https://huggingface.co/spaces/${process.env.SPACE_ID || "your-space"}/settings" target="_blank" style="color:#f472b6">Space Settings → Variables & Secrets</a>, add the keys, then restart the Space.
|
| 819 |
</div>
|
| 820 |
-
<a href="/setup" style="display:inline-block;margin-top:12px;background:linear-gradient(135deg,#ec4899,#8b5cf6);color:#fff;text-decoration:none;padding:9px 18px;border-radius:10px;font-size:.84rem;font-weight:600">📖 Full Setup Guide →</a>
|
| 821 |
</div>
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
⚙️ System & Backup
|
| 828 |
-
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M6 9l6 6 6-6"/></svg>
|
| 829 |
-
</button>
|
| 830 |
-
<div id="sys-detail" class="collapse">
|
| 831 |
-
<div class="plat-row">
|
| 832 |
-
<span style="flex:1;font-size:.85rem">Database backup to HF Dataset</span>
|
| 833 |
-
<span id="sync-badge-detail">${syncBadge}</span>
|
| 834 |
-
</div>
|
| 835 |
-
<div class="plat-row">
|
| 836 |
-
<span style="flex:1;font-size:.85rem">Last sync</span>
|
| 837 |
-
<span style="font-size:.82rem;color:var(--dim)" id="sync-time-detail">${lastSync}</span>
|
| 838 |
</div>
|
| 839 |
-
<div
|
| 840 |
-
<
|
| 841 |
-
${keepAwakeNote}
|
| 842 |
</div>
|
| 843 |
-
<div class="sync-note" id="sync-msg">${syncError ? "Backup error: " + syncError : syncStatus.last_sync_time ? "Last backup successful" : hasBackup ? "Waiting for first sync…" : "Add HF_TOKEN secret to enable automatic DB backups"}</div>
|
| 844 |
</div>
|
| 845 |
-
</div>
|
| 846 |
-
|
| 847 |
-
<div class="footer">Auto-refreshes every 30s · <a href="/health" style="color:var(--dim);text-decoration:none">Health endpoint</a></div>
|
| 848 |
-
</div>
|
| 849 |
|
| 850 |
-
<
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
return '<span class="badge badge-wait"><i class="dot" style="background:#3b82f6"></i>Pending</span>';
|
| 861 |
-
}
|
| 862 |
-
|
| 863 |
-
async function refresh() {
|
| 864 |
-
try {
|
| 865 |
-
const d = await fetch('/status').then(r => r.json());
|
| 866 |
-
document.getElementById('uptime').textContent = d.uptime;
|
| 867 |
-
|
| 868 |
-
const running = d.postizRunning;
|
| 869 |
-
document.getElementById('postiz-badge').innerHTML = running
|
| 870 |
-
? '<span class="badge badge-on"><i class="dot"></i>Running</span>'
|
| 871 |
-
: '<span class="badge badge-off">Booting…</span>';
|
| 872 |
-
|
| 873 |
-
const btn = document.querySelector('.open-btn');
|
| 874 |
-
if (btn && running && btn.classList.contains('booting')) {
|
| 875 |
-
btn.classList.remove('booting');
|
| 876 |
-
btn.textContent = 'Open Postiz →';
|
| 877 |
-
btn.href = '/app/auth';
|
| 878 |
-
btn.onclick = null;
|
| 879 |
}
|
| 880 |
-
|
| 881 |
-
|
| 882 |
-
['sync-badge','sync-badge-detail'].forEach(id => {
|
| 883 |
-
const el = document.getElementById(id);
|
| 884 |
-
if (el) el.innerHTML = badge;
|
| 885 |
-
});
|
| 886 |
-
const ls = d.sync.last_sync_time ? new Date(d.sync.last_sync_time).toLocaleString() : 'Never';
|
| 887 |
-
const el = document.getElementById('sync-time-detail');
|
| 888 |
-
if (el) el.textContent = ls;
|
| 889 |
-
const msg = document.getElementById('sync-msg');
|
| 890 |
-
if (msg) msg.textContent = d.sync.last_error ? 'Backup error: ' + d.sync.last_error
|
| 891 |
-
: d.sync.last_sync_time ? 'Last backup successful'
|
| 892 |
-
: ${hasBackup} ? 'Waiting for first sync…' : 'Add HF_TOKEN secret to enable automatic DB backups';
|
| 893 |
-
} catch(e) {}
|
| 894 |
-
}
|
| 895 |
-
refresh();
|
| 896 |
-
setInterval(refresh, 30000);
|
| 897 |
-
</script>
|
| 898 |
</body>
|
| 899 |
</html>`;
|
| 900 |
}
|
|
@@ -1051,6 +1016,7 @@ const server = http.createServer((req, res) => {
|
|
| 1051 |
uptime: formatUptime(uptime),
|
| 1052 |
postizRunning: postiz.status === "running",
|
| 1053 |
sync: readSyncStatus(),
|
|
|
|
| 1054 |
}));
|
| 1055 |
})();
|
| 1056 |
return;
|
|
|
|
| 43 |
const startTime = Date.now();
|
| 44 |
const HF_BACKUP_ENABLED = !!process.env.HF_TOKEN;
|
| 45 |
const SYNC_INTERVAL = process.env.SYNC_INTERVAL || "300";
|
| 46 |
+
const CLOUDFLARE_KEEPALIVE_STATUS_FILE = "/tmp/huggingpost-cloudflare-keepalive-status.json";
|
|
|
|
| 47 |
|
| 48 |
// Social platform env-var presence check (for dashboard status grid).
|
| 49 |
// Each entry: { name, emoji, ready: bool, setupUrl, envVars, noOAuth }
|
|
|
|
| 572 |
// UptimeRobot helpers
|
| 573 |
// ============================================================================
|
| 574 |
|
| 575 |
+
function getKeepaliveStatus() {
|
| 576 |
try {
|
| 577 |
+
if (fs.existsSync(CLOUDFLARE_KEEPALIVE_STATUS_FILE)) {
|
| 578 |
+
return JSON.parse(fs.readFileSync(CLOUDFLARE_KEEPALIVE_STATUS_FILE, "utf8"));
|
| 579 |
}
|
| 580 |
} catch {}
|
| 581 |
return null;
|
| 582 |
}
|
| 583 |
|
| 584 |
+
function escapeHtml(value) {
|
| 585 |
+
return String(value)
|
| 586 |
+
.replace(/&/g, "&")
|
| 587 |
+
.replace(/</g, "<")
|
| 588 |
+
.replace(/>/g, ">")
|
| 589 |
+
.replace(/"/g, """);
|
| 590 |
+
}
|
| 591 |
+
|
| 592 |
+
function toneBadge(label, tone = "neutral") {
|
| 593 |
+
return `<span class="badge ${tone}">${escapeHtml(label)}</span>`;
|
| 594 |
+
}
|
| 595 |
+
|
| 596 |
+
function renderTile({ title, value, detail = "", tone = "neutral", meta = "" }) {
|
| 597 |
+
return `<article class="tile ${tone}">
|
| 598 |
+
<div class="tile-head">
|
| 599 |
+
<span class="tile-title">${escapeHtml(title)}</span>
|
| 600 |
+
<span class="tile-dot"></span>
|
| 601 |
+
</div>
|
| 602 |
+
<div class="tile-value">${value}</div>
|
| 603 |
+
${detail ? `<div class="tile-detail">${detail}</div>` : ""}
|
| 604 |
+
${meta ? `<div class="tile-meta">${meta}</div>` : ""}
|
| 605 |
+
</article>`;
|
| 606 |
+
}
|
| 607 |
+
|
| 608 |
// ============================================================================
|
| 609 |
// Status helpers
|
| 610 |
// ============================================================================
|
|
|
|
| 649 |
// Dashboard HTML
|
| 650 |
// ============================================================================
|
| 651 |
|
| 652 |
+
function renderDashboard(data) {
|
| 653 |
+
const syncStatus = String(data.sync?.status || "unknown");
|
| 654 |
+
const syncTone = ["success", "restored", "synced", "configured"].includes(syncStatus)
|
| 655 |
+
? "ok"
|
| 656 |
+
: syncStatus === "disabled"
|
| 657 |
+
? "warn"
|
| 658 |
+
: "neutral";
|
| 659 |
+
const backupDetail = data.sync?.message ? escapeHtml(data.sync.message) : "No status yet";
|
| 660 |
+
|
| 661 |
+
const keepaliveConfigured = data.keepalive?.configured === true;
|
| 662 |
+
const keepaliveStatus = String(
|
| 663 |
+
data.keepalive?.status ||
|
| 664 |
+
(process.env.CLOUDFLARE_WORKERS_TOKEN ? "pending" : "not configured"),
|
| 665 |
+
);
|
| 666 |
+
const keepAliveTone = keepaliveConfigured
|
| 667 |
+
? "ok"
|
| 668 |
+
: process.env.CLOUDFLARE_WORKERS_TOKEN
|
| 669 |
+
? "warn"
|
| 670 |
+
: "neutral";
|
| 671 |
+
const keepAliveDetail = keepaliveConfigured
|
| 672 |
+
? `Pinging <code>${escapeHtml(data.keepalive.targetUrl || "/health")}</code>`
|
| 673 |
+
: process.env.CLOUDFLARE_WORKERS_TOKEN
|
| 674 |
+
? "Worker pending or failed"
|
| 675 |
+
: "Not configured";
|
| 676 |
+
|
| 677 |
const platforms = getSocialPlatforms();
|
| 678 |
const readyNow = platforms.filter(p => p.noOAuth);
|
| 679 |
const needsSetup = platforms.filter(p => !p.noOAuth);
|
| 680 |
const configuredCount = needsSetup.filter(p => p.ready).length;
|
| 681 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 682 |
const needsSetupRows = needsSetup.map(p => {
|
| 683 |
if (p.ready) {
|
| 684 |
return `<div class="plat-row ready">
|
| 685 |
<span class="plat-icon">${p.emoji}</span>
|
| 686 |
<span class="plat-name">${p.name}</span>
|
| 687 |
+
<span class="badge ok" style="font-size:0.72rem">Configured</span>
|
| 688 |
</div>`;
|
| 689 |
}
|
| 690 |
return `<div class="plat-row">
|
|
|
|
| 701 |
<span style="font-size:0.75rem;color:var(--dim)">${p.note || ""}</span>
|
| 702 |
</div>`).join("");
|
| 703 |
|
| 704 |
+
const tiles = [
|
| 705 |
+
renderTile({
|
| 706 |
+
title: "Postiz Core",
|
| 707 |
+
value: toneBadge(data.postizRunning ? "Online" : "Booting", data.postizRunning ? "ok" : "warn"),
|
| 708 |
+
detail: `Backend Port ${POSTIZ_PORT}`,
|
| 709 |
+
tone: data.postizRunning ? "ok" : "warn",
|
| 710 |
+
}),
|
| 711 |
+
renderTile({
|
| 712 |
+
title: "Uptime",
|
| 713 |
+
value: escapeHtml(data.uptimeHuman),
|
| 714 |
+
detail: `Exposed on port ${PORT}`,
|
| 715 |
+
tone: "neutral",
|
| 716 |
+
}),
|
| 717 |
+
renderTile({
|
| 718 |
+
title: "Backup",
|
| 719 |
+
value: toneBadge(syncStatus.toUpperCase(), syncTone),
|
| 720 |
+
detail: backupDetail,
|
| 721 |
+
tone: syncTone,
|
| 722 |
+
}),
|
| 723 |
+
renderTile({
|
| 724 |
+
title: "Keep Awake",
|
| 725 |
+
value: toneBadge(keepaliveConfigured ? "CF Cron" : keepaliveStatus.toUpperCase(), keepAliveTone),
|
| 726 |
+
detail: keepAliveDetail,
|
| 727 |
+
tone: keepAliveTone,
|
| 728 |
+
}),
|
| 729 |
+
].join("");
|
| 730 |
+
|
| 731 |
+
return `<!doctype html>
|
| 732 |
<html lang="en">
|
| 733 |
<head>
|
| 734 |
+
<meta charset="utf-8" />
|
| 735 |
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
| 736 |
+
<title>HuggingPost</title>
|
| 737 |
+
<style>
|
| 738 |
+
:root { color-scheme: dark; --bg:#08080f; --panel:#12111b; --panel2:#151421; --line:#26243a; --text:#f6f4ff; --muted:#7f7a9e; --soft:#b8b3d7; --good:#22c55e; --warn:#f5c542; --bad:#fb7185; --accent:#3b82f6; --accent2:#8b5cf6; --dim:#94a3b8; }
|
| 739 |
+
* { box-sizing:border-box; }
|
| 740 |
+
body { margin:0; min-height:100vh; font-family:Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; background:var(--bg); color:var(--text); font-size:13px; }
|
| 741 |
+
main { width:min(720px, calc(100% - 32px)); margin:0 auto; padding:36px 0 44px; }
|
| 742 |
+
header { text-align:center; margin-bottom:22px; }
|
| 743 |
+
h1 { margin:0; font-size:1.65rem; line-height:1; letter-spacing:0; }
|
| 744 |
+
.subtitle { margin-top:12px; color:var(--muted); font-size:.72rem; text-transform:uppercase; letter-spacing:.14em; font-weight:800; }
|
| 745 |
+
|
| 746 |
+
.hero-action { display:flex; width:100%; min-height:46px; align-items:center; justify-content:center; border-radius:8px; background:linear-gradient(135deg, var(--accent), var(--accent2)); color:#ffffff; text-decoration:none; font-weight:850; font-size:.98rem; margin:24px 0 20px; transition: opacity 0.15s ease; }
|
| 747 |
+
.hero-action:hover { opacity: 0.9; }
|
| 748 |
+
.hero-action.booting { background:var(--panel2); color:var(--muted); cursor:wait; border:1px solid var(--line); }
|
| 749 |
+
|
| 750 |
+
.overview { display:grid; grid-template-columns:repeat(2, minmax(0, 1fr)); gap:10px; margin-bottom:24px; }
|
| 751 |
+
.tile { border:1px solid var(--line); background:var(--panel); border-radius:11px; padding:18px; min-height:124px; display:flex; flex-direction:column; gap:10px; position:relative; }
|
| 752 |
+
.tile.ok { border-color:rgba(34,197,94,.22); }
|
| 753 |
+
.tile.warn { border-color:rgba(245,197,66,.24); }
|
| 754 |
+
.tile.off { border-color:rgba(251,113,133,.28); }
|
| 755 |
+
.tile-head { display:flex; align-items:center; justify-content:space-between; gap:12px; }
|
| 756 |
+
.tile-title { color:var(--muted); font-size:.67rem; letter-spacing:.18em; text-transform:uppercase; font-weight:850; }
|
| 757 |
+
.tile-dot { width:7px; height:7px; border-radius:50%; background:var(--line); }
|
| 758 |
+
.tile.ok .tile-dot { background:var(--good); }
|
| 759 |
+
.tile.warn .tile-dot { background:var(--warn); }
|
| 760 |
+
.tile.off .tile-dot { background:var(--bad); }
|
| 761 |
+
.tile-value { font-size:1.12rem; font-weight:850; overflow-wrap:anywhere; }
|
| 762 |
+
.tile-detail { color:var(--soft); line-height:1.45; font-size:.83rem; }
|
| 763 |
+
.tile-meta { color:var(--muted); line-height:1.4; font-size:.75rem; margin-top:auto; overflow-wrap:anywhere; }
|
| 764 |
+
|
| 765 |
+
.card { background:var(--panel); border:1px solid var(--line); border-radius:11px; padding:20px; margin-bottom:12px; }
|
| 766 |
+
.card h2 { font-size:.7rem; text-transform:uppercase; color:var(--muted); letter-spacing:.1em; margin:0 0 16px; }
|
| 767 |
+
.steps { list-style:none; padding:0; margin:0; }
|
| 768 |
+
.steps li { display:flex; gap:12px; padding:12px 0; border-top:1px solid var(--line); }
|
| 769 |
+
.steps li:first-child { border-top:none; padding-top:0; }
|
| 770 |
+
.steps li::before { content: counter(step-counter); counter-increment: step-counter; width:22px; height:22px; border-radius:50%; background:var(--panel2); border:1px solid var(--line); color:var(--text); font-size:10px; font-weight:800; display:flex; align-items:center; justify-content:center; flex-shrink:0; }
|
| 771 |
+
.steps { counter-reset: step-counter; }
|
| 772 |
+
.s-title { font-weight:800; margin-bottom:4px; font-size:14px; }
|
| 773 |
+
.s-note { color:var(--muted); line-height:1.5; font-size:12px; }
|
| 774 |
+
|
| 775 |
+
.plat-row { display:flex; align-items:center; gap:10px; padding:10px 0; border-top:1px solid var(--line); font-size:13px; }
|
| 776 |
+
.plat-row:first-child { border-top:none; }
|
| 777 |
+
.plat-icon { font-size:16px; width:22px; text-align:center; flex-shrink:0; }
|
| 778 |
+
.plat-name { flex:1; font-weight:700; }
|
| 779 |
+
.setup-link { color:var(--accent2); font-size:11px; text-decoration:none; font-weight:800; }
|
| 780 |
+
.setup-link:hover { text-decoration:underline; }
|
| 781 |
+
|
| 782 |
+
.badge { display:inline-flex; align-items:center; width:max-content; border:1px solid var(--line); border-radius:999px; padding:5px 10px; font-size:.72rem; font-weight:850; line-height:1; text-transform:uppercase; }
|
| 783 |
+
.badge.ok { color:var(--good); border-color:rgba(34,197,94,.34); background:rgba(34,197,94,.11); }
|
| 784 |
+
.badge.warn { color:var(--warn); border-color:rgba(245,197,66,.34); background:rgba(245,197,66,.11); }
|
| 785 |
+
.badge.off { color:var(--bad); border-color:rgba(251,113,133,.34); background:rgba(251,113,133,.11); }
|
| 786 |
+
.badge.neutral { color:var(--soft); }
|
| 787 |
+
|
| 788 |
+
code { background:var(--panel2); border:1px solid var(--line); border-radius:6px; padding:2px 6px; color:var(--text); font-size:.9em; }
|
| 789 |
+
footer { color:var(--muted); text-align:center; font-size:.74rem; margin-top:18px; }
|
| 790 |
+
footer .live { color:var(--good); }
|
| 791 |
+
@media (max-width: 700px) { .overview { grid-template-columns:1fr; } main { width:min(100% - 22px, 720px); padding-top:28px; } }
|
| 792 |
+
</style>
|
|
|
|
| 793 |
</head>
|
| 794 |
<body>
|
| 795 |
+
<main>
|
| 796 |
+
<header>
|
| 797 |
+
<h1>📮 HuggingPost</h1>
|
| 798 |
+
<div class="subtitle">Self-hosted Postiz Dashboard</div>
|
| 799 |
+
</header>
|
| 800 |
+
|
| 801 |
+
${data.postizRunning
|
| 802 |
+
? `<a href="/app/auth" class="hero-action" target="_blank" rel="noopener">Open Postiz -></a>`
|
| 803 |
+
: `<a href="#" class="hero-action booting" onclick="return false">Postiz is starting up (first boot ~5 min)...</a>`}
|
| 804 |
+
|
| 805 |
+
<section class="overview">
|
| 806 |
+
${tiles}
|
| 807 |
+
</section>
|
| 808 |
+
|
| 809 |
+
<div class="card">
|
| 810 |
+
<h2>🚀 Getting Started</h2>
|
| 811 |
+
<ol class="steps">
|
| 812 |
+
<li>
|
| 813 |
+
<div>
|
| 814 |
+
<div class="s-title">Create your account</div>
|
| 815 |
+
<div class="s-note">Click <strong>Open Postiz</strong> above. The first signup becomes the admin account.</div>
|
| 816 |
+
</div>
|
| 817 |
+
</li>
|
| 818 |
+
<li>
|
| 819 |
+
<div>
|
| 820 |
+
<div class="s-title">Connect direct channels</div>
|
| 821 |
+
<div class="s-note">Bluesky, Mastodon, Telegram, Dev.to, and Hashnode connect with just your credentials.</div>
|
| 822 |
+
</div>
|
| 823 |
+
</li>
|
| 824 |
+
<li>
|
| 825 |
+
<div>
|
| 826 |
+
<div class="s-title">Enable OAuth platforms</div>
|
| 827 |
+
<div class="s-note">LinkedIn, X, YouTube... require API keys. Use the <a href="/setup" class="setup-link">Setup Guide -></a> for instructions.</div>
|
| 828 |
+
</div>
|
| 829 |
+
</li>
|
| 830 |
+
</ol>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 831 |
</div>
|
| 832 |
+
|
| 833 |
+
<div class="card">
|
| 834 |
+
<h2>✅ Works immediately (${readyNow.length} platforms)</h2>
|
| 835 |
+
<div class="plat-list">
|
| 836 |
+
${readyNowRows}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 837 |
</div>
|
|
|
|
| 838 |
</div>
|
| 839 |
+
|
| 840 |
+
<div class="card">
|
| 841 |
+
<h2>🔑 Needs API keys (${configuredCount}/${needsSetup.length} configured)</h2>
|
| 842 |
+
<div class="plat-list">
|
| 843 |
+
${needsSetupRows}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 844 |
</div>
|
| 845 |
+
<div style="margin-top:16px">
|
| 846 |
+
<a href="/setup" class="hero-action" style="background:var(--panel2); border:1px solid var(--line); margin:0;">📖 View Full Setup Guide</a>
|
|
|
|
| 847 |
</div>
|
|
|
|
| 848 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 849 |
|
| 850 |
+
<footer><span class="live">Live</span> status - Health endpoint: <code>/health</code></footer>
|
| 851 |
+
</main>
|
| 852 |
+
<script>
|
| 853 |
+
async function refresh() {
|
| 854 |
+
try {
|
| 855 |
+
const d = await fetch('/status').then(r => r.json());
|
| 856 |
+
if (d.postizRunning && document.querySelector('.hero-action.booting')) {
|
| 857 |
+
location.reload();
|
| 858 |
+
}
|
| 859 |
+
} catch(e) {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 860 |
}
|
| 861 |
+
setInterval(refresh, 15000);
|
| 862 |
+
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 863 |
</body>
|
| 864 |
</html>`;
|
| 865 |
}
|
|
|
|
| 1016 |
uptime: formatUptime(uptime),
|
| 1017 |
postizRunning: postiz.status === "running",
|
| 1018 |
sync: readSyncStatus(),
|
| 1019 |
+
keepalive: getKeepaliveStatus(),
|
| 1020 |
}));
|
| 1021 |
})();
|
| 1022 |
return;
|
setup-uptimerobot.sh
DELETED
|
@@ -1,91 +0,0 @@
|
|
| 1 |
-
#!/bin/bash
|
| 2 |
-
set -euo pipefail
|
| 3 |
-
|
| 4 |
-
# Create or update a UptimeRobot monitor for this Hugging Face Space.
|
| 5 |
-
#
|
| 6 |
-
# Requirements:
|
| 7 |
-
# - UPTIMEROBOT_API_KEY: Main API key from UptimeRobot
|
| 8 |
-
# - SPACE_HOST or first CLI arg: your HF Space host, e.g. "user-space.hf.space"
|
| 9 |
-
#
|
| 10 |
-
# Optional:
|
| 11 |
-
# - UPTIMEROBOT_MONITOR_NAME: friendly name for the monitor
|
| 12 |
-
# - UPTIMEROBOT_ALERT_CONTACTS: dash-separated alert contact IDs, e.g. "123456-789012"
|
| 13 |
-
# - UPTIMEROBOT_INTERVAL: monitoring interval in seconds (default: 300 = 5 min; min: 30)
|
| 14 |
-
|
| 15 |
-
API_URL="https://api.uptimerobot.com/v2"
|
| 16 |
-
API_KEY="${UPTIMEROBOT_API_KEY:-}"
|
| 17 |
-
SPACE_HOST_INPUT="${1:-${SPACE_HOST:-}}"
|
| 18 |
-
STATUS_FILE="/tmp/huggingpost-uptimerobot-status.json"
|
| 19 |
-
|
| 20 |
-
if [ -z "$API_KEY" ]; then
|
| 21 |
-
echo "Missing UPTIMEROBOT_API_KEY."
|
| 22 |
-
echo "Use the Main API key from UptimeRobot -> Integrations."
|
| 23 |
-
echo "Do not use the Read-only API key or a Monitor-specific API key."
|
| 24 |
-
exit 1
|
| 25 |
-
fi
|
| 26 |
-
|
| 27 |
-
if [ -z "$SPACE_HOST_INPUT" ]; then
|
| 28 |
-
echo "Missing Space host."
|
| 29 |
-
echo "Usage: UPTIMEROBOT_API_KEY=... ./setup-uptimerobot.sh your-space.hf.space"
|
| 30 |
-
exit 1
|
| 31 |
-
fi
|
| 32 |
-
|
| 33 |
-
SPACE_HOST_CLEAN="${SPACE_HOST_INPUT#https://}"
|
| 34 |
-
SPACE_HOST_CLEAN="${SPACE_HOST_CLEAN#http://}"
|
| 35 |
-
SPACE_HOST_CLEAN="${SPACE_HOST_CLEAN%%/*}"
|
| 36 |
-
|
| 37 |
-
MONITOR_URL="https://${SPACE_HOST_CLEAN}/health"
|
| 38 |
-
MONITOR_NAME="${UPTIMEROBOT_MONITOR_NAME:-HuggingPost ${SPACE_HOST_CLEAN}}"
|
| 39 |
-
INTERVAL="${UPTIMEROBOT_INTERVAL:-300}"
|
| 40 |
-
|
| 41 |
-
echo "Checking existing UptimeRobot monitors for ${MONITOR_URL}..."
|
| 42 |
-
MONITORS_RESPONSE=$(curl -sS -X POST "${API_URL}/getMonitors" \
|
| 43 |
-
-d "api_key=${API_KEY}" \
|
| 44 |
-
-d "format=json" \
|
| 45 |
-
-d "logs=0" \
|
| 46 |
-
-d "response_times=0" \
|
| 47 |
-
-d "response_times_limit=1")
|
| 48 |
-
|
| 49 |
-
MONITOR_ID=$(printf '%s' "$MONITORS_RESPONSE" | jq -r --arg url "$MONITOR_URL" '
|
| 50 |
-
(.monitors // []) | map(select(.url == $url)) | first | .id // empty
|
| 51 |
-
')
|
| 52 |
-
|
| 53 |
-
if [ -n "$MONITOR_ID" ]; then
|
| 54 |
-
printf '{"configured":true,"monitorId":"%s","url":"%s","alreadyExisted":true,"timestamp":"%s"}\n' \
|
| 55 |
-
"$MONITOR_ID" "$MONITOR_URL" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" > "$STATUS_FILE"
|
| 56 |
-
echo "Monitor already exists (id=${MONITOR_ID}) for ${MONITOR_URL}"
|
| 57 |
-
exit 0
|
| 58 |
-
fi
|
| 59 |
-
|
| 60 |
-
echo "Creating new UptimeRobot monitor for ${MONITOR_URL}..."
|
| 61 |
-
|
| 62 |
-
CURL_ARGS=(
|
| 63 |
-
-sS
|
| 64 |
-
-X POST "${API_URL}/newMonitor"
|
| 65 |
-
-d "api_key=${API_KEY}"
|
| 66 |
-
-d "format=json"
|
| 67 |
-
-d "type=1"
|
| 68 |
-
-d "friendly_name=${MONITOR_NAME}"
|
| 69 |
-
-d "url=${MONITOR_URL}"
|
| 70 |
-
-d "interval=${INTERVAL}"
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
if [ -n "${UPTIMEROBOT_ALERT_CONTACTS:-}" ]; then
|
| 74 |
-
CURL_ARGS+=(-d "alert_contacts=${UPTIMEROBOT_ALERT_CONTACTS}")
|
| 75 |
-
fi
|
| 76 |
-
|
| 77 |
-
CREATE_RESPONSE=$(curl "${CURL_ARGS[@]}")
|
| 78 |
-
CREATE_STATUS=$(printf '%s' "$CREATE_RESPONSE" | jq -r '.stat // "fail"')
|
| 79 |
-
|
| 80 |
-
if [ "$CREATE_STATUS" != "ok" ]; then
|
| 81 |
-
printf '{"configured":false,"error":"creation failed","timestamp":"%s"}\n' \
|
| 82 |
-
"$(date -u +%Y-%m-%dT%H:%M:%SZ)" > "$STATUS_FILE"
|
| 83 |
-
echo "Failed to create monitor."
|
| 84 |
-
printf '%s\n' "$CREATE_RESPONSE"
|
| 85 |
-
exit 1
|
| 86 |
-
fi
|
| 87 |
-
|
| 88 |
-
NEW_ID=$(printf '%s' "$CREATE_RESPONSE" | jq -r '.monitor.id // empty')
|
| 89 |
-
printf '{"configured":true,"monitorId":"%s","url":"%s","timestamp":"%s"}\n' \
|
| 90 |
-
"${NEW_ID:-}" "$MONITOR_URL" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" > "$STATUS_FILE"
|
| 91 |
-
echo "Created UptimeRobot monitor ${NEW_ID:-"(id unavailable)"} for ${MONITOR_URL}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
start.sh
CHANGED
|
@@ -261,9 +261,9 @@ fi
|
|
| 261 |
node /opt/healthsrv/health-server.js &
|
| 262 |
HEALTH_PID=$!
|
| 263 |
|
| 264 |
-
if [ -n "${
|
| 265 |
-
echo "Setting up
|
| 266 |
-
|
| 267 |
fi
|
| 268 |
|
| 269 |
sleep 1
|
|
|
|
| 261 |
node /opt/healthsrv/health-server.js &
|
| 262 |
HEALTH_PID=$!
|
| 263 |
|
| 264 |
+
if [ -n "${CLOUDFLARE_WORKERS_TOKEN:-}" ]; then
|
| 265 |
+
echo "Setting up Cloudflare KeepAlive monitor..."
|
| 266 |
+
python3 /opt/cloudflare-keepalive-setup.py || true
|
| 267 |
fi
|
| 268 |
|
| 269 |
sleep 1
|