|
| 1 | +import express from "express"; |
| 2 | +import { fileURLToPath } from "url"; |
| 3 | +import cookieParser from "cookie-parser"; |
| 4 | + |
| 5 | +const __filename = fileURLToPath(import.meta.url); |
| 6 | + |
| 7 | +const app = express(); |
| 8 | +app.use(cookieParser()); |
| 9 | + |
| 10 | +interface User { |
| 11 | + id: string; |
| 12 | + name: string; |
| 13 | +} |
| 14 | + |
| 15 | +const USERS: User[] = [ |
| 16 | + { |
| 17 | + id: "1", |
| 18 | + name: "John Doe", |
| 19 | + }, |
| 20 | + { |
| 21 | + id: "2", |
| 22 | + name: "Jane Smith", |
| 23 | + }, |
| 24 | +]; |
| 25 | + |
| 26 | +const AUTH_COOKIE_NAME = "saas_microservices_authed_user"; |
| 27 | + |
| 28 | +app.get("/api/users/login", (req, res) => { |
| 29 | + const existingUser = req.cookies[AUTH_COOKIE_NAME]; |
| 30 | + if (!existingUser) { |
| 31 | + const user = USERS[Math.floor(Math.random() * USERS.length)]; |
| 32 | + res.cookie(AUTH_COOKIE_NAME, user.id); |
| 33 | + } |
| 34 | + return res.redirect("/"); |
| 35 | +}); |
| 36 | + |
| 37 | +app.get("/api/users/logout", (req, res) => { |
| 38 | + return res.clearCookie(AUTH_COOKIE_NAME).redirect("/"); |
| 39 | +}); |
| 40 | + |
| 41 | +app.get("/api/users/user", (req, res) => { |
| 42 | + const existingUser = req.cookies[AUTH_COOKIE_NAME]; |
| 43 | + if (!existingUser) { |
| 44 | + return res.status(404).json({ error: "User not found" }); |
| 45 | + } |
| 46 | + const user = USERS.find((user) => user.id === existingUser); |
| 47 | + if (!user) { |
| 48 | + return res.status(404).json({ error: "User not found" }); |
| 49 | + } |
| 50 | + return res.json(user); |
| 51 | +}); |
| 52 | + |
| 53 | +// Fake data generators |
| 54 | +const firstNames = [ |
| 55 | + "Alex", |
| 56 | + "Jordan", |
| 57 | + "Taylor", |
| 58 | + "Morgan", |
| 59 | + "Casey", |
| 60 | + "Riley", |
| 61 | + "Avery", |
| 62 | + "Quinn", |
| 63 | + "Emma", |
| 64 | + "Liam", |
| 65 | + "Olivia", |
| 66 | + "Noah", |
| 67 | + "Ava", |
| 68 | + "Ethan", |
| 69 | + "Sophia", |
| 70 | + "Mason", |
| 71 | + "Isabella", |
| 72 | + "William", |
| 73 | + "Mia", |
| 74 | + "James", |
| 75 | + "Charlotte", |
| 76 | + "Benjamin", |
| 77 | + "Amelia", |
| 78 | + "Lucas", |
| 79 | + "Harper", |
| 80 | + "Henry", |
| 81 | + "Evelyn", |
| 82 | + "Alexander", |
| 83 | + "Abigail", |
| 84 | + "Michael", |
| 85 | + "Emily", |
| 86 | + "Daniel", |
| 87 | + "Elizabeth", |
| 88 | + "Jacob", |
| 89 | + "Sofia", |
| 90 | + "Logan", |
| 91 | + "Avery", |
| 92 | + "Jackson", |
| 93 | + "Ella", |
| 94 | + "Sebastian", |
| 95 | +]; |
| 96 | + |
| 97 | +const lastNames = [ |
| 98 | + "Smith", |
| 99 | + "Johnson", |
| 100 | + "Williams", |
| 101 | + "Brown", |
| 102 | + "Jones", |
| 103 | + "Garcia", |
| 104 | + "Miller", |
| 105 | + "Davis", |
| 106 | + "Rodriguez", |
| 107 | + "Martinez", |
| 108 | + "Hernandez", |
| 109 | + "Lopez", |
| 110 | + "Gonzalez", |
| 111 | + "Wilson", |
| 112 | + "Anderson", |
| 113 | + "Thomas", |
| 114 | + "Taylor", |
| 115 | + "Moore", |
| 116 | + "Jackson", |
| 117 | + "Martin", |
| 118 | + "Lee", |
| 119 | + "Perez", |
| 120 | + "Thompson", |
| 121 | + "White", |
| 122 | + "Harris", |
| 123 | + "Sanchez", |
| 124 | + "Clark", |
| 125 | + "Ramirez", |
| 126 | + "Lewis", |
| 127 | + "Robinson", |
| 128 | + "Walker", |
| 129 | + "Young", |
| 130 | + "Allen", |
| 131 | + "King", |
| 132 | + "Wright", |
| 133 | + "Scott", |
| 134 | + "Torres", |
| 135 | + "Nguyen", |
| 136 | + "Hill", |
| 137 | + "Flores", |
| 138 | +]; |
| 139 | + |
| 140 | +const actions = [ |
| 141 | + "created a new project", |
| 142 | + "deleted a file", |
| 143 | + "shared a document", |
| 144 | + "commented on a task", |
| 145 | + "completed a milestone", |
| 146 | + "invited a team member", |
| 147 | + "updated project settings", |
| 148 | + "exported data report", |
| 149 | + "created a new team", |
| 150 | + "archived old project", |
| 151 | + "updated billing information", |
| 152 | + "changed password", |
| 153 | + "enabled two-factor authentication", |
| 154 | + "uploaded a new file", |
| 155 | + "created a backup", |
| 156 | + "merged a pull request", |
| 157 | + "deployed to production", |
| 158 | + "ran automated tests", |
| 159 | + "reviewed code changes", |
| 160 | + "created a new API key", |
| 161 | + "updated integration settings", |
| 162 | + "scheduled a meeting", |
| 163 | + "published a blog post", |
| 164 | + "updated documentation", |
| 165 | + "created a new workflow", |
| 166 | + "optimized database queries", |
| 167 | + "configured monitoring alerts", |
| 168 | + "updated security policies", |
| 169 | +]; |
| 170 | + |
| 171 | +function generateRandomActivity() { |
| 172 | + const firstName = firstNames[Math.floor(Math.random() * firstNames.length)]; |
| 173 | + const lastName = lastNames[Math.floor(Math.random() * lastNames.length)]; |
| 174 | + const action = actions[Math.floor(Math.random() * actions.length)]; |
| 175 | + |
| 176 | + // Generate timestamp within the last 30 days |
| 177 | + const now = new Date(); |
| 178 | + const thirtyDaysAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000); |
| 179 | + const randomTime = new Date( |
| 180 | + thirtyDaysAgo.getTime() + |
| 181 | + Math.random() * (now.getTime() - thirtyDaysAgo.getTime()) |
| 182 | + ); |
| 183 | + |
| 184 | + return { |
| 185 | + id: Math.random().toString(36).substr(2, 9), |
| 186 | + name: `${firstName} ${lastName}`, |
| 187 | + action: action, |
| 188 | + timestamp: randomTime.toISOString(), |
| 189 | + }; |
| 190 | +} |
| 191 | + |
| 192 | +app.get("/api/dashboard/activity", (req, res) => { |
| 193 | + const activities = Array.from({ length: 20 }, () => generateRandomActivity()); |
| 194 | + activities.sort( |
| 195 | + (a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime() |
| 196 | + ); |
| 197 | + |
| 198 | + res.json({ |
| 199 | + activities, |
| 200 | + total: activities.length, |
| 201 | + generated_at: new Date().toISOString(), |
| 202 | + }); |
| 203 | +}); |
| 204 | + |
| 205 | +// Health check |
| 206 | +app.get("/healthz", (req, res) => { |
| 207 | + res.status(200).json({ status: "ok", timestamp: new Date().toISOString() }); |
| 208 | +}); |
| 209 | + |
| 210 | +app.listen(3001, () => { |
| 211 | + console.log(`API app listening on port 3001`); |
| 212 | +}); |
0 commit comments