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

Register configuration programmatically #2379

Closed
borjapazr opened this issue Jul 12, 2023 · 7 comments
Closed

Register configuration programmatically #2379

borjapazr opened this issue Jul 12, 2023 · 7 comments
Assignees

Comments

@borjapazr
Copy link
Contributor

borjapazr commented Jul 12, 2023

Is there any way to set the configuration programmatically similar to the way providers are registered with registerProvider(...)? For example, something similar to

registerConfiguration({
  rootDir: __dirname,
  acceptMimes: ['application/json'],
  httpPort: AppConfig.PORT,
  httpsPort: false
})

The motivation for this is to be able to register a configuration at any time without having to use the @Configuration({...}) annotation when you define the server or the PlatformExpress.bootstrap(Server, <configuration_here>) function.

Thanks in advance 🙏

@Romakita
Copy link
Collaborator

Hello @borjapazr
No because object configuration isn't a provider. there is no sense to do that.

@Configuration

attach a configuration to a provider and can be used on any module or injectable class. Attached configuration to a provider are resolved and consumed by the DI before loading all providers.

PlatformExpress.boostrap()

Is the first place where you can add settings. It let developers to fetch a configuration on remote server before loading the Server itself.

Adding a registerConfiguration has no sense for me, because it won't be attached to a provider, it will cause probably a side effect on how the configuration must be merge with the main configuration.

"register at any time" isn't a good idea when it's related to a server configuration.

See you
Romain

@borjapazr
Copy link
Contributor Author

Hello, @Romakita.

I understand, it is perfectly clear. Thank you very much for your answer.

Best regards!

@github-actions
Copy link

🎉 Are you happy?

If you appreciated the support, know that it is free and is carried out on personal time ;)

A support, even a little bit makes a difference for me and continues to bring you answers!

github opencollective

@radoslavirha
Copy link

radoslavirha commented Dec 22, 2023

@Romakita is it possible to invoke some ConfigService before PlatformExpress.boostrap() ?

using registerProvider won't work in all cases, e.g. setting logger = { level: 'off' } won't work, it's too late.

My idea is to have ConfigService which loads some json file, env vars,... and handles all the configuration (server config, and other stuff not important on server startup).

I have a workaround, there is a DI independent class which I instantiate before PlatformExpress.boostrap() and use some values. Then I have another Config service which extends previous one and use it in the app with DI.

Thanks!

@NachtRitter
Copy link

@Romakita is it possible to invoke some ConfigService before PlatformExpress.boostrap() ?

using registerProvider won't work in all cases, e.g. setting logger = { level: 'off' } won't work, it's too late.

My idea is to have ConfigService which loads some json file, env vars,... and handles all the configuration (server config, and other stuff not important on server startup).

Thanks!

You can load your settings and pass fetched configuration as parameter into platform bootstrap function.

import {$log} from "@tsed/common";
import {PlatformExpress} from "@tsed/platform-express";
import {Server} from "./server";

async function bootstrap() {
  try {
    // TODO: fetch your config files here
   
    $log.debug("Start server...");
    const platform = await PlatformExpress.bootstrap(Server, {
      // pass your fetched extra settings here
    });

    await platform.listen();
    $log.debug("Server initialized");
  } catch (er) {
    $log.error(er);
  }
}

bootstrap();

@radoslavirha
Copy link

I know, that's what I'm doing. but I would like to use ConfigService before PlatformExpress.bootstrap using DI.

Currently my ConfigService is DI independent, thus testing is not ideal. I would like to use other services inside ConfigService. You know one for loading json configuration, another for ENV vars,...

@Romakita
Copy link
Collaborator

Why do you need to complexify that… just load your config on index.ts and then use your services. ConfigService must stay simple. If you need other services isn’t normal. it is no longer a configure service but a node bag ^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants