Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Dynamic service providers and arguments #844

Closed
maryam4s26 opened this issue Mar 5, 2024 · 5 comments
Closed

Dynamic service providers and arguments #844

maryam4s26 opened this issue Mar 5, 2024 · 5 comments
Labels

Comments

@maryam4s26
Copy link

hi @andersevenrud

In your opinion, how can I control the registration of adapters and service providers through the docker-compose.yml file?

@andersevenrud
Copy link
Member

I would suggest using (prefixed) environmental flags if you just want basic bootstrapping.

For example imagine you have two providers: Foo and Bar.

const enabledServices = process.env.OSJS_SERVICES.split(',')

if (enabledServices.includes('Foo')) {
  osjs.register(FooServiceProvider, {
    args: {
      arg1: process.env.OSJS_SERVICE_FOO_ARG1,
      arg2: process.env.OSJS_SERVICE_FOO_ARG2,
    }
  })
}
if (enabledServices.includes('Bar')) {
  osjs.register(BarServiceProvicer, {
    args: {
      arg1: process.env.OSJS_SERVICE_BAR_ARG1,
      arg2: process.env.OSJS_SERVICE_BAR_ARG2,
    }
  })
}

Then this could be set up with:

OSJS_SERVICES=Foo,Bar
OSJS_SERVICE_FOO_ARG1=Hello
OSJS_SERVICE_FOO_ARG2=World
OSJS_SERVICE_BAR_ARG1=Bye
OSJS_SERVICE_BAR_ARG2=World

If you need something more advanced, use something like a JSON file that you bind via a [ro] volume and just read that in your bootstrap file and do whatever you need. This can then be copied into an image in your Dockerfile or whatever for deployments.

@andersevenrud
Copy link
Member

andersevenrud commented Mar 5, 2024

Just a note on dynamically registering service providers and behaviour on runtime.

To make sure things don't crash whenever a service provider has not been registered and you perform core.make('my-namespace/something') calls, etc.:

  • Define the provided calls in your provides() returned array, i.e.: return ['my-namespace/something']
  • Do if (core.has('my-namespace/something')) {} wrapping where you do the actual calls

@andersevenrud
Copy link
Member

andersevenrud commented Mar 5, 2024

And I just had a thought. You can actually set up the client-side providers via the backend so you don't have to compile to apply settings from the environment.

  1. Create a provider on the server that sets up an endpoint that returns some kind of configuration object based on process.env like described in my first post
  2. On the client bootstrap init function, do a HTTP call to the above endpoint
  3. Now you can register anything like you want it

// Your server provider
class MyServiceProvider {
  constructor(core, options = {}) {
    this.core = core;
    this.options = options;
  }

  async init() {
    const {route} = this.core.make('osjs/express');

    route('GET', '/my-endpoint', (req, res) => {
      res.json({ services: ['Foo', 'Bar'] });
    });
  }
}
// Your client bootstrap
async function init() {
  const myConfiguration = await osjs.request('/my-endpoint');
  // ... register stuff
  // --> myConfiguration.services
}

@andersevenrud andersevenrud changed the title Registration control Dynamic service providers and arguments Mar 5, 2024
@maryam4s26
Copy link
Author

Thank you for your complete explanation <3

@andersevenrud
Copy link
Member

You're very welcome! I designed the Service (Provider) API to be able to handle this, and my examples should be enough to get you started with implementing this, so I'll close this issue.

If it doesn't work out or you have any additional questions feel free to re-open 😄

@os-js os-js locked and limited conversation to collaborators Mar 5, 2024
@andersevenrud andersevenrud converted this issue into discussion #846 Mar 5, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

2 participants