Skip to content

Commit

Permalink
#213 integrate dotenv for environment variable management (#215)
Browse files Browse the repository at this point in the history
Co-authored-by: Ly Dinh Minh Man <[email protected]>
Co-authored-by: droak <[email protected]>
  • Loading branch information
3 people authored Nov 1, 2024
1 parent 2732d31 commit fe9d267
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 29 deletions.
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
39 changes: 39 additions & 0 deletions packages/node/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import fs from "node:fs";
import * as dotenv from "dotenv";
import type { TopologyNodeConfig } from "./index.js";

export function loadConfig(
configPath?: string | undefined,
): TopologyNodeConfig | undefined {
let config: TopologyNodeConfig | undefined;

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

const result = dotenv.config();
if (!result.error) {
config = {};
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;
if (opts.config) {
config = JSON.parse(fs.readFileSync(opts.config, "utf8"));
}
const config: TopologyNodeConfig | undefined = loadConfig(opts.config);

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.

0 comments on commit fe9d267

Please sign in to comment.