v0.4.0: Remove free tier, add public demo endpoint with watermark
- Remove free account signup flow entirely - Add POST /v1/demo/html and /v1/demo/markdown (public, no auth) - Demo: 5 requests/hour per IP, 50KB body limit, watermarked PDFs - Landing page: interactive playground replaces 'Get Free API Key' - Pricing: Demo (free) + Pro (€9/mo), no more Free tier - /v1/signup returns 410 Gone with redirect to demo/pro - Keep /v1/recover for existing Pro users - Update JSON-LD, API discovery, verify page text
This commit is contained in:
parent
9095175141
commit
53755d6093
6 changed files with 299 additions and 240 deletions
146
src/routes/demo.ts
Normal file
146
src/routes/demo.ts
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
import { Router, Request, Response } from "express";
|
||||
import rateLimit from "express-rate-limit";
|
||||
import { renderPdf } from "../services/browser.js";
|
||||
import { markdownToHtml, wrapHtml } from "../services/markdown.js";
|
||||
import logger from "../services/logger.js";
|
||||
|
||||
const router = Router();
|
||||
|
||||
const WATERMARK_HTML = `<div style="position:fixed;bottom:0;left:0;right:0;background:rgba(52,211,153,0.92);color:#0b0d11;text-align:center;padding:8px;font-family:sans-serif;font-size:12px;font-weight:bold;z-index:999999;letter-spacing:0.3px;">Generated by DocFast — docfast.dev | Upgrade to Pro for clean PDFs</div>`;
|
||||
|
||||
function injectWatermark(html: string): string {
|
||||
if (html.includes("</body>")) {
|
||||
return html.replace("</body>", WATERMARK_HTML + "</body>");
|
||||
}
|
||||
return html + WATERMARK_HTML;
|
||||
}
|
||||
|
||||
// 5 requests per hour per IP
|
||||
const demoLimiter = rateLimit({
|
||||
windowMs: 60 * 60 * 1000,
|
||||
max: 5,
|
||||
message: { error: "Demo limit reached (5/hour). Get a Pro API key at https://docfast.dev for unlimited access." },
|
||||
standardHeaders: true,
|
||||
legacyHeaders: false,
|
||||
keyGenerator: (req) => req.ip || req.socket.remoteAddress || "unknown",
|
||||
});
|
||||
|
||||
router.use(demoLimiter);
|
||||
|
||||
interface DemoBody {
|
||||
html?: string;
|
||||
markdown?: string;
|
||||
css?: string;
|
||||
format?: string;
|
||||
landscape?: boolean;
|
||||
margin?: { top?: string; right?: string; bottom?: string; left?: string };
|
||||
printBackground?: boolean;
|
||||
filename?: string;
|
||||
}
|
||||
|
||||
function sanitizeFilename(name: string): string {
|
||||
return name.replace(/[\x00-\x1f"\\\r\n]/g, "").trim() || "document.pdf";
|
||||
}
|
||||
|
||||
// POST /v1/demo/html
|
||||
router.post("/html", async (req: Request & { acquirePdfSlot?: () => Promise<void>; releasePdfSlot?: () => void }, res: Response) => {
|
||||
let slotAcquired = false;
|
||||
try {
|
||||
const ct = req.headers["content-type"] || "";
|
||||
if (!ct.includes("application/json")) {
|
||||
res.status(415).json({ error: "Unsupported Content-Type. Use application/json." });
|
||||
return;
|
||||
}
|
||||
|
||||
const body: DemoBody = typeof req.body === "string" ? { html: req.body } : req.body;
|
||||
if (!body.html) {
|
||||
res.status(400).json({ error: "Missing 'html' field" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.acquirePdfSlot) {
|
||||
await req.acquirePdfSlot();
|
||||
slotAcquired = true;
|
||||
}
|
||||
|
||||
const fullHtml = body.html.includes("<html")
|
||||
? injectWatermark(body.html)
|
||||
: injectWatermark(wrapHtml(body.html, body.css));
|
||||
|
||||
const pdf = await renderPdf(fullHtml, {
|
||||
format: body.format || "A4",
|
||||
landscape: body.landscape || false,
|
||||
printBackground: body.printBackground !== false,
|
||||
margin: body.margin || { top: "0", right: "0", bottom: "0", left: "0" },
|
||||
});
|
||||
|
||||
const filename = sanitizeFilename(body.filename || "demo.pdf");
|
||||
res.setHeader("Content-Type", "application/pdf");
|
||||
res.setHeader("Content-Disposition", `attachment; filename="${filename}"`);
|
||||
res.setHeader("Content-Length", pdf.length);
|
||||
res.send(pdf);
|
||||
} catch (err: any) {
|
||||
if (err.message === "QUEUE_FULL") {
|
||||
res.status(503).json({ error: "Server busy. Please try again in a moment." });
|
||||
} else if (err.message === "PDF_TIMEOUT") {
|
||||
res.status(504).json({ error: "PDF generation timed out." });
|
||||
} else {
|
||||
logger.error({ err }, "Demo HTML conversion failed");
|
||||
res.status(500).json({ error: "PDF generation failed." });
|
||||
}
|
||||
} finally {
|
||||
if (slotAcquired && req.releasePdfSlot) req.releasePdfSlot();
|
||||
}
|
||||
});
|
||||
|
||||
// POST /v1/demo/markdown
|
||||
router.post("/markdown", async (req: Request & { acquirePdfSlot?: () => Promise<void>; releasePdfSlot?: () => void }, res: Response) => {
|
||||
let slotAcquired = false;
|
||||
try {
|
||||
const ct = req.headers["content-type"] || "";
|
||||
if (!ct.includes("application/json")) {
|
||||
res.status(415).json({ error: "Unsupported Content-Type. Use application/json." });
|
||||
return;
|
||||
}
|
||||
|
||||
const body: DemoBody = typeof req.body === "string" ? { markdown: req.body } : req.body;
|
||||
if (!body.markdown) {
|
||||
res.status(400).json({ error: "Missing 'markdown' field" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.acquirePdfSlot) {
|
||||
await req.acquirePdfSlot();
|
||||
slotAcquired = true;
|
||||
}
|
||||
|
||||
const htmlContent = markdownToHtml(body.markdown);
|
||||
const fullHtml = injectWatermark(wrapHtml(htmlContent, body.css));
|
||||
|
||||
const pdf = await renderPdf(fullHtml, {
|
||||
format: body.format || "A4",
|
||||
landscape: body.landscape || false,
|
||||
printBackground: body.printBackground !== false,
|
||||
margin: body.margin || { top: "0", right: "0", bottom: "0", left: "0" },
|
||||
});
|
||||
|
||||
const filename = sanitizeFilename(body.filename || "demo.pdf");
|
||||
res.setHeader("Content-Type", "application/pdf");
|
||||
res.setHeader("Content-Disposition", `attachment; filename="${filename}"`);
|
||||
res.setHeader("Content-Length", pdf.length);
|
||||
res.send(pdf);
|
||||
} catch (err: any) {
|
||||
if (err.message === "QUEUE_FULL") {
|
||||
res.status(503).json({ error: "Server busy. Please try again in a moment." });
|
||||
} else if (err.message === "PDF_TIMEOUT") {
|
||||
res.status(504).json({ error: "PDF generation timed out." });
|
||||
} else {
|
||||
logger.error({ err }, "Demo markdown conversion failed");
|
||||
res.status(500).json({ error: "PDF generation failed." });
|
||||
}
|
||||
} finally {
|
||||
if (slotAcquired && req.releasePdfSlot) req.releasePdfSlot();
|
||||
}
|
||||
});
|
||||
|
||||
export { router as demoRouter };
|
||||
Loading…
Add table
Add a link
Reference in a new issue