From d064ab52e9df6d1a698a1c10d236174b62a20b45 Mon Sep 17 00:00:00 2001 From: if1live Date: Tue, 3 Oct 2023 14:34:23 +0900 Subject: [PATCH] =?UTF-8?q?=ED=95=98=EB=93=9C=EC=BD=94=EB=94=A9=20?= =?UTF-8?q?=EC=A4=84=EC=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/settings.ts | 94 ++++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/src/settings.ts b/src/settings.ts index 436a2b2..e1d84f5 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -1,4 +1,5 @@ import dotenv from "dotenv"; +import assert from "node:assert"; import path from "node:path"; import url from "node:url"; import { ProviderInput } from "./types.js"; @@ -21,52 +22,55 @@ const dirname = url.fileURLToPath(new URL(".", import.meta.url)); export const rootPath = path.join(dirname, ".."); export const viewPath = path.join(rootPath, "views"); -// TODO: label, type, args로 알아서 대응하기. env 뜯으면 얻을 수 있다. -const ayane_planetscale_label = process.env.AYANE_PLANETSCALE_LABEL!; -const ayane_planetscale_type = process.env.AYANE_PLANETSCALE_TYPE!; -const ayane_planetscale_arg_1 = process.env.AYANE_PLANETSCALE_ARG_1!; - -const input_planetscale: ProviderInput = { - tag: "mysql", - label: ayane_planetscale_label, - endpoint: ayane_planetscale_arg_1, -}; - -const ayane_supabase_label = process.env.AYANE_SUPABASE_LABEL!; -const ayane_supabase_type = process.env.AYANE_SUPABASE_TYPE!; -const ayane_supabase_arg_1 = process.env.AYANE_SUPABASE_ARG_1!; - -const input_supabase: ProviderInput = { - tag: "postgres", - label: ayane_supabase_label, - endpoint: ayane_supabase_arg_1, -}; - -const ayane_redislab_label = process.env.AYANE_REDISLAB_LABEL!; -const ayane_redislab_type = process.env.AYANE_REDISLAB_TYPE!; -const ayane_redislab_arg_1 = process.env.AYANE_REDISLAB_ARG_1!; - -const input_redislab: ProviderInput = { - tag: "redis_native", - label: ayane_redislab_label, - endpoint: ayane_redislab_arg_1, -}; - -const ayane_upstash_redis_label = process.env.AYANE_UPSTASH_REDIS_LABEL!; -const ayane_upstash_redis_type = process.env.AYANE_UPSTASH_REDIS_TYPE!; -const ayane_upstash_redis_arg_1 = process.env.AYANE_UPSTASH_REDIS_ARG_1!; -const ayane_upstash_redis_arg_2 = process.env.AYANE_UPSTASH_REDIS_ARG_2!; - -const input_upstashRedis: ProviderInput = { - tag: "upstash_redis", - label: ayane_upstash_redis_label, - url: ayane_upstash_redis_arg_1, - token: ayane_upstash_redis_arg_2, +const parse = (prefix: string): ProviderInput => { + const label = process.env[`${prefix}_LABEL`]!; + const type = process.env[`${prefix}_TYPE`]!; + const arg_1 = process.env[`${prefix}_ARG_1`]; + const arg_2 = process.env[`${prefix}_ARG_2`]; + switch (type) { + case "mysql": { + assert(arg_1, "endpoint is empty"); + return { + tag: type, + label, + endpoint: arg_1, + }; + } + case "postgres": { + assert(arg_1, "endpoint is empty"); + return { + tag: type, + label, + endpoint: arg_1, + }; + } + case "redis_native": { + assert(arg_1, "endpoint is empty"); + return { + tag: type, + label, + endpoint: arg_1, + }; + } + case "upstash_redis": { + assert(arg_1, "url is empty"); + assert(arg_2, "token is empty"); + return { + tag: type, + label, + url: arg_1, + token: arg_2, + }; + } + default: + throw new Error(`Unknown type: ${type}`); + } }; +// TODO: label, type, args로 알아서 대응하기. env 뜯으면 얻을 수 있다. export const providerInputs: ProviderInput[] = [ - input_planetscale, - input_supabase, - input_redislab, - input_upstashRedis, + parse("AYANE_PLANETSCALE"), + parse("AYANE_SUPABASE"), + parse("AYANE_REDISLAB"), + parse("AYANE_UPSTASH_REDIS"), ];