frontend_plugin.go 1.5 KB

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