2016-01-09 16:34:20 -06:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-01-19 11:18:53 -06:00
|
|
|
"strings"
|
2016-01-09 16:34:20 -06:00
|
|
|
|
2016-02-09 04:17:49 -06:00
|
|
|
"github.com/gosimple/slug"
|
2016-01-09 16:34:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-03-22 04:15:47 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2016-01-09 16:34:20 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type AppPluginCss struct {
|
|
|
|
Light string `json:"light"`
|
|
|
|
Dark string `json:"dark"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type AppPlugin struct {
|
|
|
|
FrontendPluginBase
|
2016-03-08 11:17:47 -06:00
|
|
|
Routes []*AppPluginRoute `json:"routes"`
|
2016-01-11 03:44:04 -06:00
|
|
|
|
2016-03-08 11:17:47 -06:00
|
|
|
FoundChildPlugins []*PluginInclude `json:"-"`
|
|
|
|
Pinned bool `json:"-"`
|
2016-01-09 16:34:20 -06:00
|
|
|
}
|
|
|
|
|
2016-01-21 11:15:04 -06:00
|
|
|
type AppPluginRoute struct {
|
|
|
|
Path string `json:"path"`
|
|
|
|
Method string `json:"method"`
|
|
|
|
ReqGrafanaAdmin bool `json:"reqGrafanaAdmin"`
|
|
|
|
ReqRole models.RoleType `json:"reqRole"`
|
|
|
|
Url string `json:"url"`
|
|
|
|
Headers []AppPluginRouteHeader `json:"headers"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type AppPluginRouteHeader struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
}
|
|
|
|
|
2016-01-12 03:20:04 -06:00
|
|
|
func (app *AppPlugin) Load(decoder *json.Decoder, pluginDir string) error {
|
|
|
|
if err := decoder.Decode(&app); err != nil {
|
2016-01-09 16:34:20 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-10 04:03:12 -06:00
|
|
|
if err := app.registerPlugin(pluginDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-09 15:10:36 -06:00
|
|
|
|
|
|
|
Apps[app.Id] = app
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *AppPlugin) initApp() {
|
|
|
|
app.initFrontendPlugin()
|
|
|
|
|
2016-01-19 11:18:53 -06:00
|
|
|
// check if we have child panels
|
|
|
|
for _, panel := range Panels {
|
|
|
|
if strings.HasPrefix(panel.PluginDir, app.PluginDir) {
|
2016-02-09 15:10:36 -06:00
|
|
|
panel.setPathsBasedOnApp(app)
|
2016-03-08 11:17:47 -06:00
|
|
|
app.FoundChildPlugins = append(app.FoundChildPlugins, &PluginInclude{
|
2016-01-19 11:18:53 -06:00
|
|
|
Name: panel.Name,
|
|
|
|
Id: panel.Id,
|
|
|
|
Type: panel.Type,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 11:37:08 -06:00
|
|
|
// check if we have child datasources
|
|
|
|
for _, ds := range DataSources {
|
|
|
|
if strings.HasPrefix(ds.PluginDir, app.PluginDir) {
|
2016-02-09 15:10:36 -06:00
|
|
|
ds.setPathsBasedOnApp(app)
|
2016-03-08 11:17:47 -06:00
|
|
|
app.FoundChildPlugins = append(app.FoundChildPlugins, &PluginInclude{
|
2016-02-09 11:37:08 -06:00
|
|
|
Name: ds.Name,
|
|
|
|
Id: ds.Id,
|
|
|
|
Type: ds.Type,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-22 04:15:47 -05:00
|
|
|
app.DefaultNavUrl = setting.AppSubUrl + "/plugins/" + app.Id + "/edit"
|
|
|
|
|
2016-02-09 11:37:08 -06:00
|
|
|
// slugify pages
|
2016-03-22 04:15:47 -05:00
|
|
|
for _, include := range app.Includes {
|
|
|
|
if include.Slug == "" {
|
|
|
|
include.Slug = slug.Make(include.Name)
|
|
|
|
}
|
|
|
|
if include.Type == "page" && include.DefaultNav {
|
|
|
|
app.DefaultNavUrl = setting.AppSubUrl + "/plugins/" + app.Id + "/page/" + include.Slug
|
|
|
|
}
|
|
|
|
if include.Type == "dashboard" && include.DefaultNav {
|
|
|
|
app.DefaultNavUrl = setting.AppSubUrl + "/dashboard/db/" + include.Slug
|
2016-02-09 04:17:49 -06:00
|
|
|
}
|
|
|
|
}
|
2016-01-09 16:34:20 -06:00
|
|
|
}
|