app_plugin.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package plugins
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "github.com/gosimple/slug"
  6. "github.com/grafana/grafana/pkg/models"
  7. )
  8. type AppPluginPage struct {
  9. Name string `json:"name"`
  10. Slug string `json:"slug"`
  11. Component string `json:"component"`
  12. Role models.RoleType `json:"role"`
  13. }
  14. type AppPluginCss struct {
  15. Light string `json:"light"`
  16. Dark string `json:"dark"`
  17. }
  18. type AppIncludeInfo struct {
  19. Name string `json:"name"`
  20. Type string `json:"type"`
  21. Id string `json:"id"`
  22. }
  23. type AppPlugin struct {
  24. FrontendPluginBase
  25. Css *AppPluginCss `json:"css"`
  26. Pages []*AppPluginPage `json:"pages"`
  27. Routes []*AppPluginRoute `json:"routes"`
  28. Includes []*AppIncludeInfo `json:"-"`
  29. Pinned bool `json:"-"`
  30. Enabled bool `json:"-"`
  31. }
  32. type AppPluginRoute struct {
  33. Path string `json:"path"`
  34. Method string `json:"method"`
  35. ReqSignedIn bool `json:"reqSignedIn"`
  36. ReqGrafanaAdmin bool `json:"reqGrafanaAdmin"`
  37. ReqRole models.RoleType `json:"reqRole"`
  38. Url string `json:"url"`
  39. Headers []AppPluginRouteHeader `json:"headers"`
  40. }
  41. type AppPluginRouteHeader struct {
  42. Name string `json:"name"`
  43. Content string `json:"content"`
  44. }
  45. func (app *AppPlugin) Load(decoder *json.Decoder, pluginDir string) error {
  46. if err := decoder.Decode(&app); err != nil {
  47. return err
  48. }
  49. app.PluginDir = pluginDir
  50. Apps[app.Id] = app
  51. return nil
  52. }
  53. func (app *AppPlugin) initApp() {
  54. app.initFrontendPlugin()
  55. if app.Css != nil {
  56. app.Css.Dark = evalRelativePluginUrlPath(app.Css.Dark, app.Id)
  57. app.Css.Light = evalRelativePluginUrlPath(app.Css.Light, app.Id)
  58. }
  59. // check if we have child panels
  60. for _, panel := range Panels {
  61. if strings.HasPrefix(panel.PluginDir, app.PluginDir) {
  62. panel.setPathsBasedOnApp(app)
  63. app.Includes = append(app.Includes, &AppIncludeInfo{
  64. Name: panel.Name,
  65. Id: panel.Id,
  66. Type: panel.Type,
  67. })
  68. }
  69. }
  70. // check if we have child datasources
  71. for _, ds := range DataSources {
  72. if strings.HasPrefix(ds.PluginDir, app.PluginDir) {
  73. ds.setPathsBasedOnApp(app)
  74. app.Includes = append(app.Includes, &AppIncludeInfo{
  75. Name: ds.Name,
  76. Id: ds.Id,
  77. Type: ds.Type,
  78. })
  79. }
  80. }
  81. // slugify pages
  82. for _, page := range app.Pages {
  83. if page.Slug == "" {
  84. page.Slug = slug.Make(page.Name)
  85. }
  86. }
  87. }