os-js / osjs-server

OS.js Server Module
https://manual.os-js.org
Other
19 stars 21 forks source link

Accessing (vfs) server configuration from client #39

Closed miladhashemi closed 3 years ago

miladhashemi commented 3 years ago

In a application i need attributes in server vfs. How can i get a vfs adapter attributes that mounted to osjs, from client?

vfs: {
          mountpoints: [{
              name: 'myMonster',
              adapter: 'monster',
              attributes: {
                  endpoint: "http://localhost:12345/auth/v1.0",
                  username: "test:tester",
                  password: "testing"
              }
          }]
      }

for example i want endpoint in client. Is it possible to get it in server.js and send it to index.js?

andersevenrud commented 3 years ago

There's no APIs to get any kind of configurations and/or underlying session data in the client. This is mostly for security reasons.

If you want to share things between your client and server you have a few options.

Note that if you're planning on using secrets or confidential information, then you probably want to use the last solution (HTTP API). The other solutions will bake everything into the build bundles.

Hardcode into client config

export default {
  myNamespace: {
    attributes: {
      endpoint: 'foo'
    }
  }
}
core.config('myNamespace.attributes.endpoint')

Common configurations

# Create a common package
mkdir src/common
cd src/common
npm init
edit config.json

# Use in server
cd src/server
npm install ../common
edit config.js

# Use in client
cd src/client
npm install ../common
edit config.js

Example configuration usage:

// common config
{
  "mountpoint": {
    "attributes": {
      "endpoint": "foo"
    }
  }
}
// server
const common = require('the-name-you-gave-your-package/config.json')
module.exports = {
  vfs: {
    mountpoints: [{
      attributes: {
        ...common.mountpoint.attributes,
        username: 'foo'
      }
    }]
  }
}
// client
import * as common from 'the-name-you-gave-your-package/config.json'
export default {
  myNamespace: common
}
// client usage
core.config('myNamespace.attributes.endpoint')

API endpoint

I've already shown you how to do API endpoints in another issue. You can get the config on the server via the following and send it as a http response or whatever:

const adapters = core.config('mountpoints') // OR: core.make('osjs/vfs').mountpoints to get more information (but probably not relevant in your case)
const found = mountpoints.find(mount => mount.name === 'myMonster')

res.send(found.attributes.endpoint)
andersevenrud commented 3 years ago

If you go down the API endpoint route you can do a bit more generic approach if you need to access more configuration stuff down the line.

routeAuthenticated('GET', '/api/config/:path', (req, res) => {
  res.json(core.config(req.params.path))
})

Then to get any arbitrary value(s) from the tree you can just do core.request('/api/config/vfs.root').

Not really sure I'd recommend anything like this, but hey, it might be OK in your use case.

Edit Tip: This also works core.config('vfs.mountpoints.0.attributes') if you're confident that the order does not change :)

miladhashemi commented 3 years ago

Thank U indeed. It was really complete. I think the last solution (http api) would be best practice. Also your common config solution nearly bring to mind php yii2 advanced framework architecture that could be effortless and sweet solution to access common config (In osjs between server/client and in yii2/advanced backend\frontend). With this deference that in yii2/advanced, frontend and backend are independent applications.

andersevenrud commented 3 years ago

YII really brings back some (not quite good) memories :sweat_smile:

Since we more or less figured out something here; I'll close this issue. Feel free to re-open if you have any problems with the implementation :)

andersevenrud commented 3 years ago

I've already shown you how to do API endpoints in another issue

My bad. This was from another user :blush: I was thinking of this discussion thread:

https://github.com/os-js/osjs-server/issues/41#issuecomment-729793912

https://github.com/os-js/osjs-server/issues/41#issuecomment-729800045