From 37363030ec97342779eab2e258ac7b6bbe342255 Mon Sep 17 00:00:00 2001 From: Avraham Sakal Date: Fri, 5 Jul 2024 15:45:29 -0400 Subject: [PATCH] use envs for secrets --- server/.gitignore | 2 ++ server/package.json | 1 - server/pnpm-lock.yaml | 9 --------- server/src/env.ts | 21 --------------------- server/src/index.ts | 8 ++++++-- server/src/lib/clickhouse.ts | 15 +++++++++------ server/src/lib/polygon.ts | 7 ++++++- 7 files changed, 23 insertions(+), 40 deletions(-) delete mode 100644 server/src/env.ts diff --git a/server/.gitignore b/server/.gitignore index a547bf3..3b0b403 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -22,3 +22,5 @@ dist-ssr *.njsproj *.sln *.sw? + +.env \ No newline at end of file diff --git a/server/package.json b/server/package.json index 5ceab0b..3e6f663 100644 --- a/server/package.json +++ b/server/package.json @@ -13,7 +13,6 @@ "@sinclair/typebox": "^0.32.5", "@trpc/server": "^10.45.0", "cors": "^2.8.5", - "dotenv": "^16.4.1", "execa": "^9.3.0", "p-all": "^5.0.0", "p-queue": "^8.0.1", diff --git a/server/pnpm-lock.yaml b/server/pnpm-lock.yaml index 199ff4b..56cc8b0 100644 --- a/server/pnpm-lock.yaml +++ b/server/pnpm-lock.yaml @@ -23,9 +23,6 @@ importers: cors: specifier: ^2.8.5 version: 2.8.5 - dotenv: - specifier: ^16.4.1 - version: 16.4.1 execa: specifier: ^9.3.0 version: 9.3.0 @@ -396,10 +393,6 @@ packages: resolution: {integrity: sha512-bwy0MGW55bG41VqxxypOsdSdGqLwXPI/focwgTYCFMbdUiBAxLg9CFzG08sz2aqzknwiX7Hkl0bQENjg8iLByw==} engines: {node: '>=8'} - dotenv@16.4.1: - resolution: {integrity: sha512-CjA3y+Dr3FyFDOAMnxZEGtnW9KBR2M0JvvUtXNW+dYJL5ROWxP9DUHCwgFqpMk0OXCc0ljhaNTr2w/kutYIcHQ==} - engines: {node: '>=12'} - emoji-regex@8.0.0: resolution: {integrity: sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==} @@ -1485,8 +1478,6 @@ snapshots: detect-libc@2.0.3: {} - dotenv@16.4.1: {} - emoji-regex@8.0.0: optional: true diff --git a/server/src/env.ts b/server/src/env.ts deleted file mode 100644 index e90ea8d..0000000 --- a/server/src/env.ts +++ /dev/null @@ -1,21 +0,0 @@ -import path from 'path'; -import { fileURLToPath } from 'url'; -import dotenv from 'dotenv'; - -/** ES modules cannot use `__dirname`, so we have to mimic its functionality. - * Taken from [https://flaviocopes.com/fix-dirname-not-defined-es-module-scope/] - */ -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); - -if(process.env.NODE_ENV==="development"){ - const ret = dotenv.config({ path:`${__dirname}/../.env.development` }); - if(ret.parsed){ - console.log("parsed!", process.env) - } - else{ - console.log("not parsed ;-(", ret.error) - } -} - -export default null; \ No newline at end of file diff --git a/server/src/index.ts b/server/src/index.ts index fbd6fa5..1100b42 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -1,4 +1,3 @@ -import _ from "./env"; import { publicProcedure, router } from "./trpc.js"; import { query } from "./lib/clickhouse.js"; import { createHTTPHandler } from "@trpc/server/adapters/standalone"; @@ -12,6 +11,11 @@ import { import { TypeCompiler } from "@sinclair/typebox/compiler"; import { TRPCError } from "@trpc/server"; import { createServer } from "http"; +import { Env } from "@humanwhocodes/env"; + +const env = new Env(); + +const LISTEN_PORT = env.get("LISTEN_PORT", 3005); /** * Generate a TRPC-compatible validator function given a Typebox schema. @@ -324,4 +328,4 @@ const server = createServer((req, res) => { } }); -server.listen(parseInt(process.env.LISTEN_PORT) || 3005); +server.listen(parseInt(LISTEN_PORT)); diff --git a/server/src/lib/clickhouse.ts b/server/src/lib/clickhouse.ts index ce48e4b..08dfddd 100644 --- a/server/src/lib/clickhouse.ts +++ b/server/src/lib/clickhouse.ts @@ -1,13 +1,16 @@ -import _ from "../env.js"; import { createClient as createClickhouseClient } from "@clickhouse/client"; import type { DataFormat } from "@clickhouse/client"; +import { Env } from "@humanwhocodes/env"; + +const env = new Env(); + +const { CLICKHOUSE_USER, CLICKHOUSE_PASS } = env.required; +const CLICKHOUSE_HOST = env.get("CLICKHOUSE_HOST", "http://localhost:8123"); -// prevent from tree-shaking: -console.log(_); export const clickhouse = createClickhouseClient({ - host: process.env.CLICKHOUSE_HOST || "http://localhost:8123", - username: "avraham", - password: "buginoo", + host: CLICKHOUSE_HOST, + username: CLICKHOUSE_USER, + password: CLICKHOUSE_PASS, }); export async function query( diff --git a/server/src/lib/polygon.ts b/server/src/lib/polygon.ts index d4737a3..3bff4e8 100644 --- a/server/src/lib/polygon.ts +++ b/server/src/lib/polygon.ts @@ -1,7 +1,12 @@ // import pThrottle from "p-throttle"; import pRetry from "p-retry"; +import { Env } from "@humanwhocodes/env"; -const apiKey = "H95NTsatM1iTWLUwDLxM2J5zhUVYdCEz"; +const env = new Env(); + +const { POLYGON_API_KEY } = env.required; + +const apiKey = POLYGON_API_KEY; // export const getApiKey = pThrottle({ limit: 5, interval: 60000 })(() => apiKey); export const getApiKey = () => apiKey;