Skip to content

Commit

Permalink
refactor(contracts): extract environment variables (#430)
Browse files Browse the repository at this point in the history
* Rename file

* Rename flags to EnvVariables

* Fix type

* Move env variables under kernel

* Add DB env variables

* Remove unused defaults

* Api env variables

* Epi development variables

* Apie transaction pool environment variables
  • Loading branch information
sebastijankuzner authored Feb 15, 2024
1 parent b21b032 commit 28d3700
Show file tree
Hide file tree
Showing 32 changed files with 266 additions and 216 deletions.
17 changes: 10 additions & 7 deletions packages/api-database/source/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import { Constants } from "@mainsail/contracts";
import { Environment } from "@mainsail/kernel";

export const defaults = {
database: {
applicationName: "mainsail/api-http",
database:
Environment.get("CORE_DB_DATABASE") ??
`${Environment.get("CORE_TOKEN")}_${Environment.get("CORE_NETWORK_NAME")}`,
Environment.get(Constants.EnvironmentVariables.CORE_DB_DATABASE) ??
`${Environment.get(Constants.EnvironmentVariables.CORE_TOKEN)}_${Environment.get(Constants.EnvironmentVariables.CORE_NETWORK_NAME)}`,
entityPrefix: "public.",
// TODO
extra: {
options: "-c statement_timeout=3000ms",
},

host: Environment.get("CORE_DB_HOST", "localhost"),
host: Environment.get(Constants.EnvironmentVariables.CORE_DB_HOST, "localhost"),

logger: "simple-console",

logging: Environment.isTrue("CORE_DB_LOGGING_ENABLED"),
logging: Environment.isTrue(Constants.EnvironmentVariables.CORE_DB_LOGGING_ENABLED),

password: Environment.get("CORE_DB_PASSWORD", "password"),
password: Environment.get(Constants.EnvironmentVariables.CORE_DB_PASSWORD, "password"),

port: Environment.get("CORE_DB_PORT", 5432),
port: Environment.get(Constants.EnvironmentVariables.CORE_DB_PORT, 5432),

type: "postgres",
username: Environment.get("CORE_DB_USERNAME") ?? Environment.get("CORE_TOKEN"),
username:
Environment.get(Constants.EnvironmentVariables.CORE_DB_USERNAME) ??
Environment.get(Constants.EnvironmentVariables.CORE_TOKEN),
},
};
36 changes: 21 additions & 15 deletions packages/api-development/source/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Constants } from "@mainsail/contracts";
import { Environment } from "@mainsail/kernel";

export const defaults = {
Expand All @@ -6,31 +7,36 @@ export const defaults = {
limit: 100,
},
rateLimit: {
blacklist: Environment.get<undefined>("CORE_API_DEV_RATE_LIMIT_BLACKLIST")?.split(",") ?? [],
duration: Environment.get("CORE_API_DEV_RATE_LIMIT_USER_EXPIRES", 60), // Sec
enabled: Environment.isTrue("CORE_API_DEV_RATE_LIMIT_DISABLED"),
points: Environment.get("CORE_API_DEV_RATE_LIMIT_USER_LIMIT", 100),
blacklist:
Environment.get<undefined>(Constants.EnvironmentVariables.CORE_API_DEV_RATE_LIMIT_BLACKLIST)?.split(
",",
) ?? [],
duration: Environment.get(Constants.EnvironmentVariables.CORE_API_DEV_RATE_LIMIT_USER_EXPIRES, 60), // Sec
enabled: Environment.isTrue(Constants.EnvironmentVariables.CORE_API_DEV_RATE_LIMIT_DISABLED),
points: Environment.get(Constants.EnvironmentVariables.CORE_API_DEV_RATE_LIMIT_USER_LIMIT, 100),

whitelist: Environment.get<undefined>("CORE_API_DEV_RATE_LIMIT_WHITELIST")?.split(",") ?? [],
whitelist:
Environment.get<undefined>(Constants.EnvironmentVariables.CORE_API_DEV_RATE_LIMIT_WHITELIST)?.split(
",",
) ?? [],
},
socketTimeout: 5000,
trustProxy: Environment.isTrue("CORE_API_DEV_TRUST_PROXY"),
trustProxy: Environment.isTrue(Constants.EnvironmentVariables.CORE_API_DEV_TRUST_PROXY),
whitelist: ["*"],
},
server: {
http: {
enabled: Environment.isTrue("CORE_API_DEV_ENABLED"),
host: Environment.get("CORE_API_DEV_HOST", "127.0.0.1"),
port: Environment.get("CORE_API_DEV_PORT", 4006),
enabled: Environment.isTrue(Constants.EnvironmentVariables.CORE_API_DEV_ENABLED),
host: Environment.get(Constants.EnvironmentVariables.CORE_API_DEV_HOST, "127.0.0.1"),
port: Environment.get(Constants.EnvironmentVariables.CORE_API_DEV_PORT, 4006),
},
// @see https://hapijs.com/api#-serveroptionstls
https: {
enabled: Environment.isTrue("CORE_API_DEV_SSL"),
host: Environment.get("CORE_API_DEV_SSL_HOST", "127.0.0.1"),
port: Environment.get("CORE_API_DEV_SSL_PORT", 8446),
enabled: Environment.isTrue(Constants.EnvironmentVariables.CORE_API_DEV_SSL),
host: Environment.get(Constants.EnvironmentVariables.CORE_API_DEV_SSL_HOST, "127.0.0.1"),
port: Environment.get(Constants.EnvironmentVariables.CORE_API_DEV_SSL_PORT, 8446),
tls: {
cert: Environment.get("CORE_API_DEV_SSL_CERT"),
key: Environment.get("CORE_API_DEV_SSL_KEY"),
cert: Environment.get(Constants.EnvironmentVariables.CORE_API_DEV_SSL_CERT),
key: Environment.get(Constants.EnvironmentVariables.CORE_API_DEV_SSL_KEY),
},
},
},
Expand Down
40 changes: 22 additions & 18 deletions packages/api-http/source/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,50 @@
import { Constants } from "@mainsail/contracts";
import { Environment } from "@mainsail/kernel";

export const defaults = {
options: {
estimateTotalCount: !Environment.isTrue("CORE_API_NO_ESTIMATED_TOTAL_COUNT"),
estimateTotalCount: !Environment.isTrue(Constants.EnvironmentVariables.CORE_API_NO_ESTIMATED_TOTAL_COUNT),
},
plugins: {
cache: {
checkperiod: 120,
enabled: Environment.isTrue("CORE_API_CACHE"),
enabled: Environment.isTrue(Constants.EnvironmentVariables.CORE_API_CACHE),
stdTTL: 8,
},
log: {
enabled: Environment.isTrue("CORE_API_LOG"),
enabled: Environment.isTrue(Constants.EnvironmentVariables.CORE_API_LOG),
},
pagination: {
limit: 100,
},
rateLimit: {
blacklist: Environment.get<undefined>("CORE_API_RATE_LIMIT_BLACKLIST")?.split(",") ?? [],
duration: Environment.get("CORE_API_RATE_LIMIT_USER_EXPIRES", 60),
enabled: !Environment.isTrue("CORE_API_RATE_LIMIT_DISABLED"),
points: Environment.get("CORE_API_RATE_LIMIT_USER_LIMIT", 100),
whitelist: Environment.get<undefined>("CORE_API_RATE_LIMIT_WHITELIST")?.split(",") ?? [],
blacklist:
Environment.get<undefined>(Constants.EnvironmentVariables.CORE_API_RATE_LIMIT_BLACKLIST)?.split(",") ??
[],
duration: Environment.get(Constants.EnvironmentVariables.CORE_API_RATE_LIMIT_USER_EXPIRES, 60),
enabled: !Environment.isTrue(Constants.EnvironmentVariables.CORE_API_RATE_LIMIT_DISABLED),
points: Environment.get(Constants.EnvironmentVariables.CORE_API_RATE_LIMIT_USER_LIMIT, 100),
whitelist:
Environment.get<undefined>(Constants.EnvironmentVariables.CORE_API_RATE_LIMIT_WHITELIST)?.split(",") ??
[],
},
socketTimeout: 5000,
trustProxy: Environment.isTrue("CORE_API_TRUST_PROXY"),
trustProxy: Environment.isTrue(Constants.EnvironmentVariables.CORE_API_TRUST_PROXY),
whitelist: ["*"],
},
server: {
http: {
enabled: !Environment.isTrue("CORE_API_DISABLED"),
host: Environment.get("CORE_API_HOST", "0.0.0.0"),
port: Environment.get("CORE_API_PORT", 4003),
enabled: !Environment.isTrue(Constants.EnvironmentVariables.CORE_API_DISABLED),
host: Environment.get(Constants.EnvironmentVariables.CORE_API_HOST, "0.0.0.0"),
port: Environment.get(Constants.EnvironmentVariables.CORE_API_PORT, 4003),
},
// @see https://hapijs.com/api#-serveroptionstls
https: {
enabled: Environment.isTrue("CORE_API_SSL"),
host: Environment.get("CORE_API_SSL_HOST", "0.0.0.0"),
port: Environment.get("CORE_API_SSL_PORT", 8443),
enabled: Environment.isTrue(Constants.EnvironmentVariables.CORE_API_SSL),
host: Environment.get(Constants.EnvironmentVariables.CORE_API_SSL_HOST, "0.0.0.0"),
port: Environment.get(Constants.EnvironmentVariables.CORE_API_SSL_PORT, 8443),
tls: {
cert: Environment.get("CORE_API_SSL_CERT"),
key: Environment.get("CORE_API_SSL_KEY"),
cert: Environment.get(Constants.EnvironmentVariables.CORE_API_SSL_CERT),
key: Environment.get(Constants.EnvironmentVariables.CORE_API_SSL_KEY),
},
},
},
Expand Down
34 changes: 0 additions & 34 deletions packages/api-sync/source/defaults.ts

This file was deleted.

19 changes: 10 additions & 9 deletions packages/api-transaction-pool/source/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Constants } from "@mainsail/contracts";
import { Environment } from "@mainsail/kernel";

export const defaults = {
Expand All @@ -6,23 +7,23 @@ export const defaults = {
limit: 100,
},
socketTimeout: 5000,
trustProxy: Environment.isTrue("CORE_API_TRANSACTION_POOL_TRUST_PROXY"),
trustProxy: Environment.isTrue(Constants.EnvironmentVariables.CORE_API_TRANSACTION_POOL_TRUST_PROXY),
whitelist: ["*"],
},
server: {
http: {
enabled: !Environment.isTrue("CORE_API_TRANSACTION_POOL_DISABLED"),
host: Environment.get("CORE_API_TRANSACTION_POOL_HOST", "0.0.0.0"),
port: Environment.get("CORE_API_TRANSACTION_POOL_PORT", 4007),
enabled: !Environment.isTrue(Constants.EnvironmentVariables.CORE_API_TRANSACTION_POOL_DISABLED),
host: Environment.get(Constants.EnvironmentVariables.CORE_API_TRANSACTION_POOL_HOST, "0.0.0.0"),
port: Environment.get(Constants.EnvironmentVariables.CORE_API_TRANSACTION_POOL_PORT, 4007),
},
// @see https://hapijs.com/api#-serveroptionstls
https: {
enabled: Environment.isTrue("CORE_API_TRANSACTION_POOL_SSL"),
host: Environment.get("CORE_API_TRANSACTION_POOL_SSL_HOST", "0.0.0.0"),
port: Environment.get("CORE_API_TRANSACTION_POOL_SSL_PORT", 8447),
enabled: Environment.isTrue(Constants.EnvironmentVariables.CORE_API_TRANSACTION_POOL_SSL),
host: Environment.get(Constants.EnvironmentVariables.CORE_API_TRANSACTION_POOL_SSL_HOST, "0.0.0.0"),
port: Environment.get(Constants.EnvironmentVariables.CORE_API_TRANSACTION_POOL_SSL_PORT, 8447),
tls: {
cert: Environment.get("CORE_API_TRANSACTION_POOL_SSL_CERT"),
key: Environment.get("CORE_API_TRANSACTION_POOL_SSL_KEY"),
cert: Environment.get(Constants.EnvironmentVariables.CORE_API_TRANSACTION_POOL_SSL_CERT),
key: Environment.get(Constants.EnvironmentVariables.CORE_API_TRANSACTION_POOL_SSL_KEY),
},
},
},
Expand Down
6 changes: 5 additions & 1 deletion packages/cli/source/commands/discover-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export class DiscoverConfig {

try {
return await readJSON(
path.join(process.env[Constants.Flags.CORE_PATH_CONFIG]!, applicationName, "config.json"),
path.join(
process.env[Constants.EnvironmentVariables.CORE_PATH_CONFIG]!,
applicationName,
"config.json",
),
);
} catch {}

Expand Down
8 changes: 4 additions & 4 deletions packages/cli/source/services/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ export class Environment {
paths[key] = `${value}/${network}/${name}`;
}

if (process.env[Constants.Flags.CORE_PATH_CONFIG]) {
if (process.env[Constants.EnvironmentVariables.CORE_PATH_CONFIG]) {
paths = {
...paths,
config: path.resolve(process.env[Constants.Flags.CORE_PATH_CONFIG]!, name),
config: path.resolve(process.env[Constants.EnvironmentVariables.CORE_PATH_CONFIG]!, name),
};
}

if (process.env[Constants.Flags.CORE_PATH_DATA]) {
if (process.env[Constants.EnvironmentVariables.CORE_PATH_DATA]) {
paths = {
...paths,
data: path.resolve(process.env[Constants.Flags.CORE_PATH_DATA]!, name),
data: path.resolve(process.env[Constants.EnvironmentVariables.CORE_PATH_DATA]!, name),
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/source/services/source-providers/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class NPM extends AbstractSource {
}

async #getPackage(value: string, version?: string): Promise<{ name: string; tarball: string }> {
const registry = process.env[Constants.Flags.CORE_NPM_REGISTRY] || "https://registry.npmjs.org";
const registry = process.env[Constants.EnvironmentVariables.CORE_NPM_REGISTRY] || "https://registry.npmjs.org";
const { body } = await got(`${registry}/${value}`);

const response: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Application } from "@mainsail/kernel";
import { ensureDirSync, existsSync } from "fs-extra";

import { ConfigurationWriter } from "./configuration-writer";
import { EnviromentData } from "./contracts";
import { EnvironmentData } from "./contracts";
import {
AppGenerator,
EnvironmentGenerator,
Expand Down Expand Up @@ -210,8 +210,8 @@ export class ConfigurationGenerator {
logger?.info(`Configuration generated on location: ${this.configurationPath}`);
}

#preparteEnvironmentOptions(options: Contracts.NetworkGenerator.EnvironmentOptions): EnviromentData {
const data: EnviromentData = {
#preparteEnvironmentOptions(options: Contracts.NetworkGenerator.EnvironmentOptions): EnvironmentData {
const data: EnvironmentData = {
// CORE_DB_HOST: options.coreDBHost,
// CORE_DB_PORT: options.coreDBPort,
CORE_P2P_PORT: options.coreP2PPort,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { stringify } from "envfile";
import { writeFileSync, writeJSONSync } from "fs-extra";
import path from "path";

import { EnviromentData, Wallet } from "./contracts";
import { EnvironmentData, Wallet } from "./contracts";
import { Identifiers } from "./identifiers";

@injectable()
Expand All @@ -18,7 +18,7 @@ export class ConfigurationWriter {
});
}

writeEnvironment(environment: EnviromentData): void {
writeEnvironment(environment: EnvironmentData): void {
writeFileSync(path.join(this.configurationPath, ".env"), stringify(environment));
}

Expand Down
2 changes: 1 addition & 1 deletion packages/configuration-generator/source/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type PartialRecord<K extends keyof any, T> = {
[P in K]?: T;
};

export type EnviromentData = PartialRecord<Contracts.Flags.Flag, string | number>;
export type EnvironmentData = PartialRecord<Contracts.Kernel.EnvironmentVariable, string | number>;

export type Wallet = {
address: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { injectable } from "@mainsail/container";
import { Contracts } from "@mainsail/contracts";

import { EnviromentData } from "../contracts";
import { EnvironmentData } from "../contracts";

@injectable()
export class EnvironmentGenerator {
#data: EnviromentData = {};
#data: EnvironmentData = {};

addInitialRecords(): EnvironmentGenerator {
this.#data = {
Expand All @@ -20,21 +20,21 @@ export class EnvironmentGenerator {
return this;
}

addRecord(key: Contracts.Flags.Flag, value: string | number): EnvironmentGenerator {
addRecord(key: Contracts.Kernel.EnvironmentVariable, value: string | number): EnvironmentGenerator {
this.#data[key] = value;

return this;
}

addRecords(data: EnviromentData): EnvironmentGenerator {
addRecords(data: EnvironmentData): EnvironmentGenerator {
for (const [key, value] of Object.entries(data)) {
this.addRecord(key as Contracts.Flags.Flag, value);
this.addRecord(key as Contracts.Kernel.EnvironmentVariable, value);
}

return this;
}

generate(): EnviromentData {
generate(): EnvironmentData {
return this.#data;
}
}
Loading

0 comments on commit 28d3700

Please sign in to comment.