frontend_plugin.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. Module string `json:"module"`
  12. BaseUrl string `json:"baseUrl"`
  13. StaticRoot string `json:"staticRoot"`
  14. StaticRootAbs string `json:"-"`
  15. }
  16. func (fp *FrontendPluginBase) initFrontendPlugin() {
  17. if fp.StaticRoot != "" {
  18. fp.StaticRootAbs = filepath.Join(fp.PluginDir, fp.StaticRoot)
  19. StaticRoutes = append(StaticRoutes, &PluginStaticRoute{
  20. Directory: fp.StaticRootAbs,
  21. PluginId: fp.Id,
  22. })
  23. }
  24. fp.handleModuleDefaults()
  25. fp.Info.Logos.Small = evalRelativePluginUrlPath(fp.Info.Logos.Small, fp.BaseUrl)
  26. fp.Info.Logos.Large = evalRelativePluginUrlPath(fp.Info.Logos.Large, fp.BaseUrl)
  27. for i := 0; i < len(fp.Info.Screenshots); i++ {
  28. fp.Info.Screenshots[i].Path = evalRelativePluginUrlPath(fp.Info.Screenshots[i].Path, fp.Id)
  29. }
  30. }
  31. func (fp *FrontendPluginBase) setPathsBasedOnApp(app *AppPlugin) {
  32. appSubPath := strings.Replace(fp.PluginDir, app.StaticRootAbs, "", 1)
  33. fp.IncludedInAppId = app.Id
  34. fp.BaseUrl = app.BaseUrl
  35. fp.Module = util.JoinUrlFragments("plugins/"+app.Id, appSubPath) + "/module"
  36. }
  37. func (fp *FrontendPluginBase) handleModuleDefaults() {
  38. if fp.StaticRoot != "" {
  39. fp.Module = path.Join("plugins", fp.Id, "module")
  40. fp.BaseUrl = path.Join("public/plugins", fp.Id)
  41. return
  42. }
  43. fp.Module = path.Join("app/plugins", fp.Type, fp.Id, "module")
  44. fp.BaseUrl = path.Join("public/app/plugins", fp.Type, fp.Id)
  45. }
  46. func evalRelativePluginUrlPath(pathStr string, baseUrl string) string {
  47. if pathStr == "" {
  48. return ""
  49. }
  50. u, _ := url.Parse(pathStr)
  51. if u.IsAbs() {
  52. return pathStr
  53. }
  54. return path.Join(baseUrl, pathStr)
  55. }