app_plugin.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. Pages []*AppPluginPage `json:"pages"`
  26. Routes []*AppPluginRoute `json:"routes"`
  27. Includes []*AppIncludeInfo `json:"-"`
  28. Pinned bool `json:"-"`
  29. Enabled bool `json:"-"`
  30. }
  31. type AppPluginRoute struct {
  32. Path string `json:"path"`
  33. Method string `json:"method"`
  34. ReqGrafanaAdmin bool `json:"reqGrafanaAdmin"`
  35. ReqRole models.RoleType `json:"reqRole"`
  36. Url string `json:"url"`
  37. Headers []AppPluginRouteHeader `json:"headers"`
  38. }
  39. type AppPluginRouteHeader struct {
  40. Name string `json:"name"`
  41. Content string `json:"content"`
  42. }
  43. func (app *AppPlugin) Load(decoder *json.Decoder, pluginDir string) error {
  44. if err := decoder.Decode(&app); err != nil {
  45. return err
  46. }
  47. if err := app.registerPlugin(pluginDir); err != nil {
  48. return err
  49. }
  50. Apps[app.Id] = app
  51. return nil
  52. }
  53. func (app *AppPlugin) initApp() {
  54. app.initFrontendPlugin()
  55. // check if we have child panels
  56. for _, panel := range Panels {
  57. if strings.HasPrefix(panel.PluginDir, app.PluginDir) {
  58. panel.setPathsBasedOnApp(app)
  59. app.Includes = append(app.Includes, &AppIncludeInfo{
  60. Name: panel.Name,
  61. Id: panel.Id,
  62. Type: panel.Type,
  63. })
  64. }
  65. }
  66. // check if we have child datasources
  67. for _, ds := range DataSources {
  68. if strings.HasPrefix(ds.PluginDir, app.PluginDir) {
  69. ds.setPathsBasedOnApp(app)
  70. app.Includes = append(app.Includes, &AppIncludeInfo{
  71. Name: ds.Name,
  72. Id: ds.Id,
  73. Type: ds.Type,
  74. })
  75. }
  76. }
  77. // slugify pages
  78. for _, page := range app.Pages {
  79. if page.Slug == "" {
  80. page.Slug = slug.Make(page.Name)
  81. }
  82. }
  83. }