|
| 1 | +import { Request, Router } from "express"; |
| 2 | +import { |
| 3 | + globalDashboard, |
| 4 | + DashboardSize, |
| 5 | + countryDashboard, |
| 6 | +} from "../utils/routes/dashboard/dashboardHandlers"; |
| 7 | +import handleAsync from "./handleAsync"; |
| 8 | +import { isTerminal } from "./userAgent"; |
| 9 | + |
| 10 | +export const dashboardRouter = Router({ mergeParams: true }); |
| 11 | + |
| 12 | +/** |
| 13 | + * |
| 14 | + * @param req Express request |
| 15 | + * @returns True if the request is from a not from wget, curl or httpie |
| 16 | + */ |
| 17 | +const isWeb: (req: Request) => boolean = (req) => { |
| 18 | + // Check if the link is asking for web version of dashboard |
| 19 | + const link = req.baseUrl.startsWith("/history/web/charts"); |
| 20 | + // Check if the user agent is NOT coming from terminal based application |
| 21 | + const isNotTerminal = !isTerminal(req.headers["user-agent"]); |
| 22 | + return link && isNotTerminal; |
| 23 | +}; |
| 24 | + |
| 25 | +dashboardRouter.get( |
| 26 | + "/:size?", |
| 27 | + handleAsync(async (req, res, next) => { |
| 28 | + // Get parameters from request |
| 29 | + let size = req.params.size as DashboardSize; |
| 30 | + |
| 31 | + // Set default size and check then check if size var matches |
| 32 | + if (size === undefined) size = "sm"; |
| 33 | + if (!["sm", "md", "lg"].includes(size)) return next(); |
| 34 | + |
| 35 | + let response = await globalDashboard(size, isWeb(req)); |
| 36 | + res.send(response); |
| 37 | + }) |
| 38 | +); |
| 39 | + |
| 40 | +dashboardRouter.get( |
| 41 | + "/:country/:size?", |
| 42 | + handleAsync(async (req, res, next) => { |
| 43 | + // Get parameters from request |
| 44 | + let country = req.params.country; |
| 45 | + let size = req.params.size as DashboardSize; |
| 46 | + |
| 47 | + // Set default size and check then check if size var matches |
| 48 | + if (size === undefined) size = "sm"; |
| 49 | + if (!["sm", "md", "lg"].includes(size)) return next(); |
| 50 | + |
| 51 | + let response = await countryDashboard(country, size, isWeb(req)); |
| 52 | + res.send(response); |
| 53 | + }) |
| 54 | +); |
0 commit comments