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

fix: di and config mgmt #38

Merged
merged 10 commits into from
Jan 19, 2025

Conversation

jleach
Copy link
Contributor

@jleach jleach commented Jan 17, 2025

This PR makes the following changes:

  • Fixes Unable to run mediator #34: I diffed the work done by Credebl and this repo. Looks like the problem was caused some imports using the word type. I've removed all the ones causing problems and left the others.
  • Moved constants to config: I moved some constants to a config file so they can be easily exposed in Helm charts. This allows Kubernetes to use environment variables for configuration.
  • Added dotenv support: I added support for dotenv so that developers can use environment variables in development environments. In production, the library will load from process.env.*.
  • biome rules: The biome rule useImportType was causing issues so I disabled it.

Verified

This commit was signed with the committer’s verified signature.
jleach Jason C. Leach
Signed-off-by: Jason C. Leach <[email protected]>

Verified

This commit was signed with the committer’s verified signature.
jleach Jason C. Leach
Signed-off-by: Jason C. Leach <[email protected]>

Verified

This commit was signed with the committer’s verified signature.
jleach Jason C. Leach
Signed-off-by: Jason C. Leach <[email protected]>

Verified

This commit was signed with the committer’s verified signature.
jleach Jason C. Leach
Signed-off-by: Jason C. Leach <[email protected]>
@jleach
Copy link
Contributor Author

jleach commented Jan 17, 2025

@TimoGlastra This runs fine. I still need to fix the style errors. Wanted to put it up for early review since I kinda went rogue in fixing #34. Thoughts?

Copy link
Contributor

@TimoGlastra TimoGlastra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally ok, but could use some cleanup.

Haven't used nconf before but i like the config file based approach.

package.json Outdated
Comment on lines 33 to 36
"dotenv": "^16.4.7",
"express": "^4.21.2",
"nconf": "^0.12.1",
"node-fetch": "^2.6.11",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally try to keep dependencies to a minimum. Makes maintenance easier. Node 18 has fetch built in. Node 20+ i think (we can keep that as a minimum i think?) has env file support built in.

I just removed node fetch before migration.

@@ -48,14 +50,14 @@ export async function createAgent() {
const app = express()
const socketServer = new Server({ noServer: true })

const logger = new Logger(LOG_LEVEL)
const logger = new Logger(config.get('agent:logLevel'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have typing support for config.get?

Otherwise i'd rather export these from a config file so we have correct typing

Copy link
Contributor Author

@jleach jleach Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No typing unfortunately. A bit more on this in my latest comment. I can go either way.

src/agent.ts Outdated
@@ -104,6 +106,7 @@ export async function createAgent() {
res.status(200).send('Ok')
})

// eslint-disable-next-line @typescript-eslint/no-misused-promises
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use eslint

src/config.ts Outdated
import dotenv from 'dotenv';
import nconf from 'nconf';
import path from 'path';
// import { fileURLToPath } from 'url';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the commented code?

src/index.ts Outdated

console.log(`AGENT_PORT: ${config.get('agent:port')}`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the console logs? It should only log if the logger is enabled

import type { MessageRepository } from './MessageRepository'
import { MessageRepository } from './MessageRepository'
import { PushNotificationsFcmRepository } from '../push-notifications/fcm/repository'
import fetch from 'node-fetch'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for node fetch

@@ -137,8 +144,9 @@ export class StorageServiceMessageQueue implements MessagePickupRepository {
private async processNotification(message: NotificationMessage) {
try {
const body = {
fcmToken: message.token,
fcmToken: message.token || 'abc',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

messageType: message.messageType,
clientCode: message.clientCode || '5b4d6bc6-362e-4f53-bdad-ee2742bc0de3',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Comment on lines 130 to 131
token: pushNotificationFcmRecord?.deviceToken || '',
clientCode: pushNotificationFcmRecord?.clientCode || '',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
token: pushNotificationFcmRecord?.deviceToken || '',
clientCode: pushNotificationFcmRecord?.clientCode || '',
token: pushNotificationFcmRecord?.deviceToken,
clientCode: pushNotificationFcmRecord?.clientCode,

src/config.ts Outdated
// nconf.defaults({});

if (env === 'development') {
console.log('Configuration:', nconf.get());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Console.log

@sairanjit
Copy link
Contributor

@jleach You can remove the clientCode as it was built for some custom logic and can be removed now.

Verified

This commit was signed with the committer’s verified signature.
jleach Jason C. Leach
Signed-off-by: Jason C. Leach <[email protected]>

Verified

This commit was signed with the committer’s verified signature.
jleach Jason C. Leach
Signed-off-by: Jason C. Leach <[email protected]>

Verified

This commit was signed with the committer’s verified signature.
jleach Jason C. Leach
Signed-off-by: Jason C. Leach <[email protected]>

Verified

This commit was signed with the committer’s verified signature.
jleach Jason C. Leach
Signed-off-by: Jason C. Leach <[email protected]>
@jleach
Copy link
Contributor Author

jleach commented Jan 17, 2025

This pr is undoing quite a few things i added. Can you check to only integrate needed changes from credebl repo?

@TimoGlastra For sure. That was just a draft for early feedback.

I wen't through and diffed the two repos. Looks like #34 was caused by some import type { PushNotificationsFcmService } from './services/PushNotificationsFcmService' where key word type was causing the problem. I went through and removed all the ones that were causing the errors, then when I ran biome to fix any formatting issues it added them back. I've disabled the problematic rule for now.

This PR is mostly the config changes. I'm Ok with a more simple approach:

import dotenv from "dotenv";

if (env === 'development') {
  dotenv.config();
}

type Config = {
  PORT: number;
  NODE_ENV: string;
}

const config: Config = {
  PORT: parseInt(process.env.PORT || "3000", 10),
  NODE_ENV: process.env.NODE_ENV || "development",
};

The up-side to nconf is that you can use a config file like config.json which can be handy, especially in Kubernets where you often use a ConfigMap for non-confidential data.

{
  "db": {
    "host": "foo.example.ca",
    ...
  },
  "agent": {
    "port": 8888,
    ...
  },
  "wallet": {
    "name": "my-cool-wallet",
    ...
  }
}

Also, I couldn't find the option --env-file in ts-node so I changed this and added dotenv:

./node_modules/.bin/ts-node --env-file .env src/index.ts
...
Error: Unknown or unexpected option: --env-file

node ➜ /work (fix/di-and-config) $ ./node_modules/.bin/ts-node -v
v10.9.2

Verified

This commit was signed with the committer’s verified signature.
jleach Jason C. Leach
Signed-off-by: Jason C. Leach <[email protected]>
@jleach jleach force-pushed the fix/di-and-config branch from 22bff80 to f7803d1 Compare January 17, 2025 23:03
@jleach jleach marked this pull request as ready for review January 17, 2025 23:12
@jleach jleach requested a review from TimoGlastra January 17, 2025 23:12

Verified

This commit was signed with the committer’s verified signature.
jleach Jason C. Leach
Signed-off-by: Jason C. Leach <[email protected]>
@TimoGlastra
Copy link
Contributor

TimoGlastra commented Jan 18, 2025

This is the env feature in node 20.6+ https://dev.to/usulpro/nodejs-a-guide-to-native-env-support-and-local-development-300g

Edit: seems it only works if you can node and register ts-node: TypeStrong/ts-node#2058 (comment)

@TimoGlastra
Copy link
Contributor

Re the biome rule, somehow it doesn't recognize the service is being used for dependency injection. It shouldn't be type import there. Disabling the rule makes sense for now

@TimoGlastra TimoGlastra linked an issue Jan 18, 2025 that may be closed by this pull request
@@ -18,7 +18,7 @@
"test": "jest",
"build": "tsc -p tsconfig.build.json",
"prepublishOnly": "pnpm build",
"dev": "ts-node --env-file=.env.development --env-file .env.local dev",
"dev": "ts-node src/index.ts",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jleach Do you want to add the config related changes to dev.ts as well ? As it helps if we want to run locally using ngrok

Copy link
Contributor Author

@jleach jleach Jan 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need dev.ts any more. Just change the AGENT_ENDPOINTS='https://mediator.example.ca,wss://mediator.example.ca' in your .env file and pnpm dev and you're good to go. To me there shouldn't me any more of a change between dev and test. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dev script connected to ngrok and changed the urls. Now you have to run it manually in another terminal and set the env variables.

It's ok, but does require more setup work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TimoGlastra I don't mind. I can create create a pnpm dev:standalone or something for ppl that don't use ngrok. Will create a new ticket for tracking since I merged this one already.

@jleach jleach merged commit 215ffa7 into openwallet-foundation:main Jan 19, 2025
4 checks passed
@jleach jleach deleted the fix/di-and-config branch January 19, 2025 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to run mediator Add a /health endpoint
3 participants