Dynamic service providers and arguments #846
-
| In your opinion, how can I control the registration of adapters and service providers through the  | 
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
| 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: 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. | 
Beta Was this translation helpful? Give feedback.
-
| 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  
 class MyServiceProvider {
  constructor(core, options = {}) {
    this.core = core;
    this.options = options;
  }
  provides() {
    return ['my-namespace/something'];
  }
  async init() {
    this.core.singleton('my-namespace/something', () => ({
      foo: name => alert(`Hello ${name}!`)
    }));
  }
}if (core.has('my-namespace/something')) {
  const {foo} = core.make('my-namespace/something')
  foo('World')
} | 
Beta Was this translation helpful? Give feedback.
-
| 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. 
 // 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
} | 
Beta Was this translation helpful? Give feedback.
-
| Thank you for your complete explanation <3 | 
Beta Was this translation helpful? Give feedback.
-
| You're very welcome! | 
Beta Was this translation helpful? Give feedback.
I would suggest using (prefixed) environmental flags if you just want basic bootstrapping.
For example imagine you have two providers: Foo and Bar.
Then this could be set up with: