From efd9f1e6cfa958ba1a695786f0f1e58565e5e630 Mon Sep 17 00:00:00 2001 From: Mathieu Lefebvre <49442766+Matlefebvre1234@users.noreply.github.com> Date: Mon, 29 Jul 2024 14:37:32 -0400 Subject: [PATCH] add default config --- src/config.ts | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/config.ts diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 0000000..a4677c9 --- /dev/null +++ b/src/config.ts @@ -0,0 +1,50 @@ +import "dotenv/config"; +import { z } from 'zod'; +import { Option, program } from "commander"; + +import pkg from "../package.json"; + +export const DEFAULT_PORT = "8080"; +export const DEFAULT_HOSTNAME = "localhost"; +export const DEFAULT_HOST = "http://localhost:8123"; +export const DATABASE_SUFFIX = "token_v1"; // API will use {chain}_{DATABASE_SUFFIX} as the database name +export const DEFAULT_USERNAME = "default"; +export const DEFAULT_PASSWORD = "default"; +export const DEFAULT_MAX_LIMIT = 10000; +export const DEFAULT_VERBOSE = true; +export const DEFAULT_SORT_BY = "DESC"; +export const APP_NAME = pkg.name; +export const APP_VERSION = { + version: pkg.version, + commit: process.env.APP_VERSION || "unknown" +}; + +// parse command line options +const opts = program + .name(pkg.name) + .version(`${APP_VERSION.version}+${APP_VERSION.commit}`) + .description(pkg.description) + .showHelpAfterError() + .addOption(new Option("-p, --port ", "HTTP port on which to attach the API").env("PORT").default(DEFAULT_PORT)) + .addOption(new Option("--hostname ", "Server listen on HTTP hostname").env("HOSTNAME").default(DEFAULT_HOSTNAME)) + .addOption(new Option("--host ", "Database HTTP hostname").env("HOST").default(DEFAULT_HOST)) + .addOption(new Option("--database ", "The database suffix to use inside ClickHouse for {chain}_{database}").env("DATABASE").default(`eth_${DATABASE_SUFFIX}`)) + .addOption(new Option("--username ", "Database user").env("USERNAME").default(DEFAULT_USERNAME)) + .addOption(new Option("--password ", "Password associated with the specified username").env("PASSWORD").default(DEFAULT_PASSWORD)) + .addOption(new Option("--max-limit ", "Maximum LIMIT queries").env("MAX_LIMIT").default(DEFAULT_MAX_LIMIT)) + .addOption(new Option("-v, --verbose ", "Enable verbose logging").choices(["true", "false"]).env("VERBOSE").default(DEFAULT_VERBOSE)) + + .parse() + .opts(); + +export const config = z.object({ + port: z.string(), + hostname: z.string(), + host: z.string(), + database: z.string(), + username: z.string(), + password: z.string(), + maxLimit: z.coerce.number(), + verbose: z.coerce.boolean(), +}).parse(opts); +