-
-
Notifications
You must be signed in to change notification settings - Fork 842
Dynamic service providers and arguments #846
-
In your opinion, how can I control the registration of adapters and service providers through the docker-compose.yml file?
Beta Was this translation helpful? Give feedback.
All reactions
-
👀 1
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
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
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') }
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
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.
- Create a provider on the server that sets up an endpoint that returns some kind of configuration object based on
process.envlike described in my first post - On the client bootstrap
initfunction, do a HTTP call to the above endpoint - 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 }
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
Thank you for your complete explanation <3
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 1
-
You're very welcome!
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 1