Skip to content

Commit

Permalink
fix: don't hardcode deployment environment in logs (#177)
Browse files Browse the repository at this point in the history
* fix: don't hardcode deployment environment in logs

* enh: validate remaining variables and arguments with zod
  • Loading branch information
bard authored Jul 21, 2023
1 parent e308bb5 commit 37a0bf7
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 49 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ IPFS_GATEWAY=
SENTRY_DSN=
STORAGE_DIR=./data
CACHE_DIR=./.cache
DEPLOYMENT_ENVIRONMENT=local
PORT=8080
LOG_LEVEL=debug
2 changes: 2 additions & 0 deletions fly.production.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ kill_timeout = 5

[env]
PORT = "8080"
DEPLOYMENT_ENVIRONMENT = "production"
LOG_LEVEL = "info"
STORAGE_DIR = "/mnt/indexer/data"
CACHE_DIR = "/mnt/indexer/cache"

Expand Down
2 changes: 2 additions & 0 deletions fly.staging.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ kill_timeout = 5

[env]
PORT = "8080"
DEPLOYMENT_ENVIRONMENT = "staging"
LOG_LEVEL = "debug"
STORAGE_DIR = "/mnt/indexer/data"
CACHE_DIR = "/mnt/indexer/cache"

Expand Down
16 changes: 15 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"pluralistic": "github:gitcoinco/pluralistic.js#644d14fff65100f005d7afc18799b0f99b72ae24",
"serve-index": "^1.9.1",
"statuses-bitmap": "github:gitcoinco/statuses-bitmap#3d8fd370f209ccbaffd3781cf2b6d2895237c21c",
"write-file-atomic": "^5.0.1"
"write-file-atomic": "^5.0.1",
"zod": "^3.21.4"
},
"devDependencies": {
"@types/cors": "^2.8.13",
Expand Down
101 changes: 56 additions & 45 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "dotenv/config";
import { parseArgs } from "node:util";
import { ToBlock } from "chainsauce";
import path from "node:path";
import { z } from "zod";

type ChainId = number;

Expand Down Expand Up @@ -352,35 +352,46 @@ export type Config = {
passportApiKey: string;
cacheDir: string | null;
logLevel: "trace" | "debug" | "info" | "warn" | "error";
clear: boolean;
ipfsGateway: string;
coingeckoApiKey: string | null;
coingeckoApiUrl: string;
chains: Chain[];
runOnce: boolean;
apiHttpPort: number;
sentryDsn: string | null;
deploymentEnvironment: "local" | "development" | "staging" | "production";
};

export function getConfig(): Config {
const apiHttpPort = Number(process.env.PORT || "4000");
const apiHttpPort = z.coerce.number().parse(process.env.PORT);

if (!process.env.PASSPORT_SCORER_ID) {
throw new Error("PASSPORT_SCORER_ID is not set");
}
if (!process.env.PASSPORT_API_KEY) {
throw new Error("PASSPORT_SCORER_ID is not set");
}
const passportScorerId = process.env.PASSPORT_SCORER_ID;
const passportApiKey = process.env.PASSPORT_API_KEY;
const deploymentEnvironment = z
.union([
z.literal("local"),
z.literal("development"),
z.literal("staging"),
z.literal("production"),
])
.parse(process.env.DEPLOYMENT_ENVIRONMENT);

const passportScorerId = z.string().parse(process.env.PASSPORT_SCORER_ID);

const passportApiKey = z.string().parse(process.env.PASSPORT_API_KEY);

const coingeckoApiKey = process.env.COINGECKO_API_KEY ?? null;
const coingeckoApiKey = z
.union([z.string(), z.null()])
.default(null)
.parse(process.env.COINGECKO_API_KEY);

const coingeckoApiUrl = process.env.COINGECKO_API_KEY
? "https://pro-api.coingecko.com/api/v3/"
: "https://api.coingecko.com/api/v3";
const coingeckoApiUrl =
coingeckoApiKey === null
? "https://api.coingecko.com/api/v3"
: "https://pro-api.coingecko.com/api/v3/";

const storageDir = path.join(process.env.STORAGE_DIR || "./data");
const storageDir = z
.string()
.default("./data")
.parse(process.env.STORAGE_DIR);

const { values: args } = parseArgs({
options: {
Expand All @@ -399,9 +410,6 @@ export function getConfig(): Config {
"run-once": {
type: "boolean",
},
clear: {
type: "boolean",
},
"no-cache": {
type: "boolean",
},
Expand All @@ -420,37 +428,40 @@ export function getConfig(): Config {
return c;
});

const toBlock =
"to-block" in args
? args["to-block"] === "latest"
? ("latest" as const)
: Number(args["to-block"])
: ("latest" as const);
const toBlock = z
.union([z.coerce.number(), z.literal("latest")])
.default("latest")
.parse(args["to-block"]);

const fromBlock = "from-block" in args ? Number(args["from-block"]) : 0;

const logLevel = args["log-level"] ?? "info";
if (
logLevel !== "trace" &&
logLevel !== "debug" &&
logLevel !== "info" &&
logLevel !== "warn" &&
logLevel !== "error"
) {
throw new Error(`Invalid log level: ${logLevel}`);
}
const fromBlock = z.coerce.number().default(0).parse(args["from-block"]);

const clear = args.clear ?? false;
const logLevel = z
.union([
z.literal("trace"),
z.literal("debug"),
z.literal("info"),
z.literal("warn"),
z.literal("error"),
])
.default("info")
.parse(process.env.LOG_LEVEL);

const runOnce = args["run-once"] ?? false;
const runOnce = z.boolean().default(false).parse(args["run-once"]);

const cacheDir = args["no-cache"]
? null
: process.env.CACHE_DIR || "./.cache";
const cacheDir = z
.union([z.string(), z.null()])
.default("./.cache")
.parse(process.env.CACHE_DIR);

const ipfsGateway = process.env.IPFS_GATEWAY || "https://cloudflare-ipfs.com";
const ipfsGateway = z
.string()
.default("https://cloudflare-ipfs.com")
.parse(process.env.IPFS_GATEWAY);

const sentryDsn = process.env.SENTRY_DSN ?? null;
const sentryDsn = z
.union([z.string(), z.null()])
.default(null)
.parse(process.env.SENTRY_DSN);

return {
sentryDsn,
Expand All @@ -463,10 +474,10 @@ export function getConfig(): Config {
cacheDir,
logLevel,
runOnce,
clear,
ipfsGateway,
passportApiKey,
passportScorerId,
apiHttpPort,
deploymentEnvironment,
};
}
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ async function main(): Promise<void> {
});
}

const baseLogger = pino({ level: "trace" }).child({
service: "indexer-staging", // XXX TODO grab environment name from env variable
const baseLogger = pino({ level: config.logLevel }).child({
service: `indexer-${config.deploymentEnvironment}`,
});

// Promise will be resolved once the catchup is done. Afterwards, services
// will still be in listen-and-update mode
await Promise.all([
catchupAndWatchPassport({
...config,
Expand Down

0 comments on commit 37a0bf7

Please sign in to comment.