Skip to content

Commit

Permalink
하드코딩 줄임
Browse files Browse the repository at this point in the history
  • Loading branch information
if1live committed Oct 3, 2023
1 parent e4c51cf commit d064ab5
Showing 1 changed file with 49 additions and 45 deletions.
94 changes: 49 additions & 45 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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"),
];

0 comments on commit d064ab5

Please sign in to comment.