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
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ module.exports = {
eqeqeq: "warn",
},
ignorePatterns: ["node_modules/", "dist/", "*.min.js"],
overrides: [
{
files: ["test/**/*.ts"],
env: {
mocha: true,
},
},
],
};
13 changes: 12 additions & 1 deletion backend/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "dotenv/config";

// Run backend with cache updates.
import { updateLumensCache } from "./routes";
import { app, updateLumensCache } from "./routes";
import { updateLedgers } from "./ledgers";

async function beginCacheUpdates() {
Expand All @@ -15,4 +15,15 @@ async function beginCacheUpdates() {
}
}

function startServer() {
if (process.env.NODE_ENV === "test") {
return;
}

app.listen(app.get("port"), () => {
console.log("Listening on port", app.get("port"));
});
}

beginCacheUpdates();
startServer();
27 changes: 19 additions & 8 deletions backend/routes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express from "express";
import proxy from "express-http-proxy";
import proxyAddr from "proxy-addr";
import logger from "morgan";
import path from "path";
import rateLimit from "express-rate-limit";
Expand All @@ -13,10 +14,17 @@ app.set("port", process.env.PORT || 5000);
app.set("json spaces", 2);

// Trust proxy to get real client IPs behind proxies/load balancers.
const defaultTrustProxy = "loopback,linklocal,uniquelocal";
const trustProxy = process.env.TRUST_PROXY || defaultTrustProxy;
console.log(`Setting trust proxy to: ${trustProxy}`);
app.set("trust proxy", trustProxy);
export function parseTrustProxy(trustProxyEnv?: string): string[] {
const defaultTrustProxy = "loopback,linklocal,uniquelocal";
return (trustProxyEnv || defaultTrustProxy)
.split(",")
.map((cidr) => cidr.trim())
.filter(Boolean);
}

const trustProxyCidrs = parseTrustProxy(process.env.TRUST_PROXY);
console.log(`Setting trust proxy to TRUST_PROXY: ${trustProxyCidrs.join(",")}`);
app.set("trust proxy", proxyAddr.compile(trustProxyCidrs));

app.use(logger("combined"));

Expand Down Expand Up @@ -78,7 +86,7 @@ const externalServiceLimiter = createRateLimit(
// Apply general rate limiting to all API routes
app.use("/api/", generalApiLimiter);

if (process.env.DEV) {
if (process.env.DEV === "true") {
// Development: proxy to Vite dev server
app.use(
"/",
Expand Down Expand Up @@ -225,9 +233,12 @@ app.get(
lumensV2V3.v3CirculatingSupplyHandler,
);

app.listen(app.get("port"), () => {
console.log("Listening on port", app.get("port"));
});
// Start listening only when this file is executed directly (not when required by tests)
if (require.main === module && process.env.NODE_ENV !== "test") {
app.listen(app.get("port"), () => {
console.log("Listening on port", app.get("port"));
});
}

export async function updateLumensCache() {
await lumens.updateApiLumens();
Expand Down
50 changes: 50 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@
"@types/lodash": "^4.14.178",
"@types/mocha": "^9.0.0",
"@types/morgan": "^1.9.3",
"@types/proxy-addr": "^2.0.3",
"@types/react": "^18.2.0",
"@types/react-dom": "^18.2.0",
"@types/supertest": "^6.0.2",
"@typescript-eslint/eslint-plugin": "^5.9.1",
"@typescript-eslint/parser": "^5.9.1",
"@vitejs/plugin-react": "^4.0.0",
Expand Down
Loading
Loading