Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Grafana API in OSJS #9

Closed
miladhashemi opened this issue Aug 22, 2020 · 13 comments
Closed

Using Grafana API in OSJS #9

miladhashemi opened this issue Aug 22, 2020 · 13 comments

Comments

@miladhashemi
Copy link
Contributor

Hi,
I have plan to use grafana api in osjs/widget.
Because of CORS policy I can't use it directly in client side.
What is your suggestion to use grafana api in osjs?
Is it need to use server service provider to handle this kind of problem or should be addressed in other ways?

@andersevenrud
Copy link
Member

Try using a proxy on the server:

https://manual.os-js.org/config/#proxies

@andersevenrud
Copy link
Member

Example:

  proxy: [{
    source: '/grafana',
    destination: 'http://localhost:8080',
  }],

@miladhashemi
Copy link
Contributor Author

miladhashemi commented Aug 22, 2020

Then, Is it possible to call grafana API directly in widget?

@andersevenrud
Copy link
Member

Not directly, via the OS.js server as a proxy request.

@andersevenrud
Copy link
Member

If you can't control the policies on the grafana server, this is what you need. If you run OS.js on a reverse-proxy server like nginx, you can just set up the proxy there instead.

@miladhashemi
Copy link
Contributor Author

I thought, it must be like swiftstack, that writing an adapter was helpful.
I think I need something like VFSServiceProvider,which present some method. Then we could write our custom adapters that implement must implement VFSServiceProvider methods. At the last application like filemanager uses registered adapter.

Here I want to show grafana data on widget as a chart. I need to call method in widget for getting information from grafana.

You say if I add grafana as proxy then I can call its api?

@andersevenrud
Copy link
Member

You say if I add grafana as proxy then I can call its api?

Yes, then you can call the HTTP APIs in your client via your proxy just using an URL likke /mygrafana/api/users.

@miladhashemi
Copy link
Contributor Author

Thanks a lot.

@andersevenrud
Copy link
Member

No problem! Closing the issue, but feel free to keep commenting if you run into something.

@miladhashemi
Copy link
Contributor Author

I defined bellow code in servers's config, but I'm not certain how to use it in widget(I tested in widget by fetch, but already i get cors policy error). How must i use api in widget?

proxy: [{
    source: 'http://zdrive.ir:3000',
    destination: 'http://localhost:8000/',
    options: {
      proxyReqOptDecorator: function(proxyReqOpts, srcReq) {
        proxyReqOpts.headers = {'Authorization': `Bearer ${token}`};
        return proxyReqOpts;
      }
    }
  }]

@andersevenrud
Copy link
Member

You have to define the proxy like in my example:

  proxy: [{
    source: '/grafana',
    destination: 'http://localhost:8080',
  }],

This allows you just to use /grafana as the URL in your widget (or if you need an absolute URL do core.url('/grafana').

@miladhashemi
Copy link
Contributor Author

Oh, shame on me. You are right. first I must define my source path and use it in widget then proxy converts it to destination path. Such a huge mistake :'|

@andersevenrud
Copy link
Member

No worries :) Hopefully this will solve your issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants