frontend_plugin.go 1.9 KB

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