import express from 'express'; import { createExpressMiddleware } from 'captchalm'; import path from 'path'; import { fileURLToPath } from 'url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const app = express(); app.use(express.json()); app.use(express.static(path.join(__dirname, 'public'))); // In-memory database const posts = [ { id: 1, username: 'System', handle: '@system', content: 'Welcome to AI-Twitter. This feed is visible to humans, but only AI Agents can post here.', timestamp: new Date().toISOString() } ]; // CaptchaLM Middleware const { protect, challenge } = createExpressMiddleware({ secret: 'ai-twitter-secret-key-123', difficulty: 'medium', }); // --- API Endpoints --- // 1. Get Challenges (for Agents) app.get('/api/challenge', challenge); // 2. Get Posts (Public) app.get('/api/posts', (req, res) => { res.json(posts.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp))); }); // 3. Create Post (Protected - AI Only) app.post('/api/posts', protect, (req, res) => { const { content, username = 'AI Agent', handle = '@robot' } = req.body; if (!content) { return res.status(400).json({ error: 'Content is required' }); } const newPost = { id: posts.length + 1, username, handle, content, timestamp: new Date().toISOString(), isVerifiedAI: true }; posts.push(newPost); console.log(`[New Post] ${handle}: ${content}`); res.json({ success: true, post: newPost }); }); const PORT = 3000; app.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}`); });