Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"license": "GPL-3.0",
"devDependencies": {
"@types/asciichart": "^1.5.4",
"@types/blessed": "^0.1.17",
"@types/express": "^4.17.11",
"@types/minimist": "^1.2.1",
"@types/morgan": "^1.9.2",
Expand All @@ -43,10 +44,13 @@
"dependencies": {
"asciichart": "^1.5.25",
"axios": "^0.21.1",
"blessed": "^0.1.81",
"blessed-contrib": "^4.8.21",
"colors": "^1.4.0",
"express": "^4.17.1",
"minimist": "^1.2.5",
"morgan": "^1.10.0",
"typescript": "^4.2.3"
"typescript": "^4.2.3",
"world-countries": "^4.0.0"
}
}
11 changes: 8 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import express from "express";
import morgan from "morgan";
import { dashboardRouter } from "./api/dashboardRouter";
import { errorHandler } from "./api/errorHandler";
import { plainRouter } from "./api/plainRouter";
import { router } from "./api/router";
import { regularRouter } from "./api/regularRouter";
import { userAgentMiddleware } from "./api/userAgent";
import { lines } from "./utils/getResponses";
import { lines } from "./utils/libs/getResponses";

const port = parseInt(process.env.PORT!) || 7070;

const app = express();
app.use(morgan("common"));
app.use("/history/web/charts", dashboardRouter);

app.use(userAgentMiddleware);

app.use("/history/charts", dashboardRouter);

/**
* Plain CMD/Basic routes have both quiet and full modes
* Same with regular / routes with ansi color codes
*/
app.use(["/quiet/basic", "/quiet/cmd", "/quiet/plain"], plainRouter);
app.use(["/basic", "/cmd", "/plain"], plainRouter);

app.use(["/quiet", "/"], router);
app.use(["/quiet", "/"], regularRouter);
app.use("/", errorHandler);

// Not found handler
Expand Down
54 changes: 54 additions & 0 deletions src/api/dashboardRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Request, Router } from "express";
import {
globalDashboard,
DashboardSize,
countryDashboard,
} from "../utils/routes/dashboard/dashboardHandlers";
import handleAsync from "./handleAsync";
import { isTerminal } from "./userAgent";

export const dashboardRouter = Router({ mergeParams: true });

/**
*
* @param req Express request
* @returns True if the request is from a not from wget, curl or httpie
*/
const isWeb: (req: Request) => boolean = (req) => {
// Check if the link is asking for web version of dashboard
const link = req.baseUrl.startsWith("/history/web/charts");
// Check if the user agent is NOT coming from terminal based application
const isNotTerminal = !isTerminal(req.headers["user-agent"]);
return link && isNotTerminal;
};

dashboardRouter.get(
"/:size?",
handleAsync(async (req, res, next) => {
// Get parameters from request
let size = req.params.size as DashboardSize;

// Set default size and check then check if size var matches
if (size === undefined) size = "sm";
if (!["sm", "md", "lg"].includes(size)) return next();

let response = await globalDashboard(size, isWeb(req));
res.send(response);
})
);

dashboardRouter.get(
"/:country/:size?",
handleAsync(async (req, res, next) => {
// Get parameters from request
let country = req.params.country;
let size = req.params.size as DashboardSize;

// Set default size and check then check if size var matches
if (size === undefined) size = "sm";
if (!["sm", "md", "lg"].includes(size)) return next();

let response = await countryDashboard(country, size, isWeb(req));
res.send(response);
})
);
4 changes: 2 additions & 2 deletions src/api/plainRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
informationPerCountryPlain,
historyPerCountryPlain,
globalHistoryPlain,
} from "../utils/plainHandlers";
import { isQuiet } from "./router";
} from "../utils/routes/plain/plainHandlers";
import { isQuiet } from "./regularRouter";

/**
* The plainRouter handles all the plain routes such as /basic, /cmd, and /plain
Expand Down
15 changes: 7 additions & 8 deletions src/api/router.ts → src/api/regularRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
globalInformation,
historyPerCountry,
informationPerCountry,
} from "../utils/handlers";
} from "../utils/routes/regular/regularHandlers";
import handleAsync from "./handleAsync";

/**
Expand All @@ -16,13 +16,12 @@ export const isQuiet: (req: Request) => boolean = (req) =>
req.baseUrl.startsWith("/quiet");

/**
* The rootRouter handles all the processing of the requests *after* passing through
* The regularRouter handles all the processing of the requests *after* passing through
* all middlewares except not found and error handling middleware
*/
export const router = Router({ mergeParams: true });
export const regularRouter = Router({ mergeParams: true });

// rootRouter.get("/history/:country/:type", historyPerCountryAndType);
router.get(
regularRouter.get(
"/history/:mode?",
handleAsync(async (req, res, next) => {
// get mode from params
Expand All @@ -37,7 +36,7 @@ router.get(
})
);

router.get(
regularRouter.get(
"/history/:country/:mode?",
handleAsync(async (req, res, next) => {
const country = req.params.country;
Expand All @@ -53,15 +52,15 @@ router.get(
})
);

router.get(
regularRouter.get(
"/:country",
handleAsync(async (req, res, _next) => {
const country = req.params.country;
res.send(await informationPerCountry(country, isQuiet(req)));
})
);

router.get(
regularRouter.get(
"/",
handleAsync(async (req, res, _next) => {
res.send(await globalInformation(isQuiet(req)));
Expand Down
6 changes: 4 additions & 2 deletions src/api/userAgent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response, NextFunction } from "express";
import { lines } from "../utils/getResponses";
import { lines } from "../utils/libs/getResponses";

// Type of middleware and handler
export type Handler = (
Expand All @@ -13,7 +13,9 @@ export type Handler = (
* @param userAgent The user agent of the requester
* @returns A boolean that is true of the user agent provided is from curl / wget / httpie
*/
const isTerminal: (userAgent: string | undefined) => boolean = (userAgent) => {
export const isTerminal: (userAgent: string | undefined) => boolean = (
userAgent
) => {
if (userAgent === undefined) return false;
if (/curl|wget|httpie/i.test(userAgent)) return true;
return false;
Expand Down
6 changes: 3 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#!/usr/bin/env node
import argv from "minimist";
import { lines, welcomeMessage } from "./utils/getResponses";
import { lines, welcomeMessage } from "./utils/libs/getResponses";
import {
globalHistory,
globalInformation,
historyPerCountry,
informationPerCountry,
} from "./utils/handlers";
} from "./utils/routes/regular/regularHandlers";
import {
globalHistoryPlain,
globalInformationPlain,
historyPerCountryPlain,
informationPerCountryPlain,
} from "./utils/plainHandlers";
} from "./utils/routes/plain/plainHandlers";

const args = argv(process.argv.slice(2));
let { history, mode, help, quiet, plain } = args;
Expand Down
Loading