Giter Club home page Giter Club logo

Comments (5)

andersevenrud avatar andersevenrud commented on July 20, 2024 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_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.

from os.js.

andersevenrud avatar andersevenrud commented on July 20, 2024 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() returned array, i.e.: return ['my-namespace/something']
  • Do if (core.has('my-namespace/something')) {} wrapping where you do the actual calls

from os.js.

andersevenrud avatar andersevenrud commented on July 20, 2024 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.

  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
}

from os.js.

maryam4s26 avatar maryam4s26 commented on July 20, 2024 1

Thank you for your complete explanation <3

from os.js.

andersevenrud avatar andersevenrud commented on July 20, 2024

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 😄

from os.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.