Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#213 integrate dotenv for environment variable management #215

Merged
merged 21 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env

dist/
docs/
Expand Down
4 changes: 4 additions & 0 deletions packages/node/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ADDRESSES=/ip4/0.0.0.0/tcp/50000/ws,/ip4/0.0.0.0/tcp/50001
BOOTSTRAP=true
BOOTSTRAP_PEERS=/dns4/relay.droak.sh/tcp/443/wss/p2p/Qma3GsJmB47xYuyahPZPSadh1avvxfyYQwk8R3UnFrQ6aP
PRIVATE_KEY_SEED=example
1 change: 1 addition & 0 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@chainsafe/libp2p-gossipsub": "^14.1.0",
"@grpc/grpc-js": "^1.12.2",
"@libp2p/interface": "^2.1.3",
"dotenv": "^16.4.5",
"@topology-foundation/blueprints": "0.2.1-4",
"@topology-foundation/network": "0.2.1-4",
"@topology-foundation/object": "0.2.1-4",
Expand Down
38 changes: 38 additions & 0 deletions packages/node/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import fs from "node:fs";
import * as dotenv from "dotenv";
import type { TopologyNodeConfig } from "./index.js";

export function loadConfig(
configPath?: string | undefined,
): TopologyNodeConfig {
let config: TopologyNodeConfig = {};
d-roak marked this conversation as resolved.
Show resolved Hide resolved

if (configPath) {
config = JSON.parse(fs.readFileSync(configPath, "utf8"));
return config;
}

const result = dotenv.config();
if (!result.error) {
config.network_config = {
addresses: process.env.ADDRESSES
? process.env.ADDRESSES.split(",")
: undefined,
bootstrap: process.env.BOOTSTRAP
? process.env.BOOTSTRAP === "true"
: undefined,
bootstrap_peers: process.env.BOOTSTRAP_PEERS
? process.env.BOOTSTRAP_PEERS.split(",")
: undefined,
browser_metrics: process.env.BROWSER_METRICS
? process.env.BROWSER_METRICS === "true"
: undefined,
private_key_seed: process.env.PRIVATE_KEY_SEED
? process.env.PRIVATE_KEY_SEED
: undefined,
};
return config;
}

return config;
}
10 changes: 4 additions & 6 deletions packages/node/src/run.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import fs from "node:fs";
import { program } from "./cli/index.js";
import { TopologyNode, type TopologyNodeConfig } from "./index.js";
import { loadConfig } from "./config.js";
import { TopologyNode } from "./index.js";
import type { TopologyNodeConfig } from "./index.js";
import { init as rpc_init } from "./rpc/index.js";

const run = async () => {
program.parse(process.argv);
const opts = program.opts();
let config: TopologyNodeConfig | undefined;
elielnfinic marked this conversation as resolved.
Show resolved Hide resolved
if (opts.config) {
config = JSON.parse(fs.readFileSync(opts.config, "utf8"));
}
const config: TopologyNodeConfig = loadConfig(opts.config);
d-roak marked this conversation as resolved.
Show resolved Hide resolved

const node = new TopologyNode(config);
await node.start();
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = "0.2.1-3";
export const VERSION = "0.2.1-4";
53 changes: 31 additions & 22 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.