frontend_plugin.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package plugins
  2. import (
  3. "net/url"
  4. "path"
  5. "strings"
  6. "github.com/grafana/grafana/pkg/setting"
  7. "github.com/grafana/grafana/pkg/util"
  8. )
  9. type FrontendPluginBase struct {
  10. PluginBase
  11. }
  12. func (fp *FrontendPluginBase) initFrontendPlugin() {
  13. if isInternalPlugin(fp.PluginDir) {
  14. StaticRoutes = append(StaticRoutes, &PluginStaticRoute{
  15. Directory: fp.PluginDir,
  16. PluginId: fp.Id,
  17. })
  18. }
  19. fp.handleModuleDefaults()
  20. fp.Info.Logos.Small = evalRelativePluginUrlPath(fp.Info.Logos.Small, fp.BaseUrl)
  21. fp.Info.Logos.Large = evalRelativePluginUrlPath(fp.Info.Logos.Large, fp.BaseUrl)
  22. for i := 0; i < len(fp.Info.Screenshots); i++ {
  23. fp.Info.Screenshots[i].Path = evalRelativePluginUrlPath(fp.Info.Screenshots[i].Path, fp.BaseUrl)
  24. }
  25. }
  26. func (fp *FrontendPluginBase) setPathsBasedOnApp(app *AppPlugin) {
  27. appSubPath := strings.Replace(fp.PluginDir, app.PluginDir, "", 1)
  28. fp.IncludedInAppId = app.Id
  29. fp.BaseUrl = app.BaseUrl
  30. fp.Module = util.JoinUrlFragments("plugins/"+app.Id, appSubPath) + "/module"
  31. }
  32. func (fp *FrontendPluginBase) handleModuleDefaults() {
  33. if isInternalPlugin(fp.PluginDir) {
  34. fp.Module = path.Join("plugins", fp.Id, "module")
  35. fp.BaseUrl = path.Join("public/plugins", fp.Id)
  36. return
  37. }
  38. fp.Module = path.Join("app/plugins", fp.Type, fp.Id, "module")
  39. fp.BaseUrl = path.Join("public/app/plugins", fp.Type, fp.Id)
  40. }
  41. func isInternalPlugin(pluginDir string) bool {
  42. return !strings.Contains(pluginDir, setting.StaticRootPath)
  43. }
  44. func evalRelativePluginUrlPath(pathStr string, baseUrl string) string {
  45. if pathStr == "" {
  46. return ""
  47. }
  48. u, _ := url.Parse(pathStr)
  49. if u.IsAbs() {
  50. return pathStr
  51. }
  52. return path.Join(baseUrl, pathStr)
  53. }