Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Dynamic service providers and arguments #846

Answered by andersevenrud
maryam4s26 asked this question in Q&A
Discussion options

hi @andersevenrud

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

You must be logged in to vote

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_...

Replies: 5 comments

Comment options

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.

You must be logged in to vote
0 replies
Answer selected by andersevenrud
Comment options

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()
  • Do if (core.has()) {} wrapping where you do the actual calls

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')
}
You must be logged in to vote
0 replies
Comment options

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
}
You must be logged in to vote
0 replies
Comment options

Thank you for your complete explanation <3

You must be logged in to vote
0 replies
Comment options

You're very welcome!

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #844 on March 05, 2024 23:16.

AltStyle によって変換されたページ (->オリジナル) /