diff --git a/pkg/cmd/web.go b/pkg/cmd/web.go index 9ab2df82921..5fb1e067305 100644 --- a/pkg/cmd/web.go +++ b/pkg/cmd/web.go @@ -14,6 +14,7 @@ import ( "github.com/grafana/grafana/pkg/api/static" "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/middleware" + "github.com/grafana/grafana/pkg/plugins" "github.com/grafana/grafana/pkg/setting" ) @@ -34,7 +35,11 @@ func newMacaron() *macaron.Macaron { mapStatic(m, setting.StaticRootPath, "img", "img") mapStatic(m, setting.StaticRootPath, "fonts", "fonts") mapStatic(m, setting.StaticRootPath, "robots.txt", "robots.txt") - mapStatic(m, setting.DataPath, "plugins", "_plugins") + + for _, route := range plugins.StaticRoutes { + log.Info("Adding plugin static route %s -> %s", route.Url, route.Path) + mapStatic(m, route.Path, "", route.Url) + } m.Use(macaron.Renderer(macaron.RenderOptions{ Directory: path.Join(setting.StaticRootPath, "views"), diff --git a/pkg/plugins/models.go b/pkg/plugins/models.go index fe294d9b576..50f283c95e4 100644 --- a/pkg/plugins/models.go +++ b/pkg/plugins/models.go @@ -12,6 +12,13 @@ type DataSourcePlugin struct { Annotations bool `json:"annotations"` Metrics bool `json:"metrics"` BuiltIn bool `json:"builtIn"` + StaticRootConfig *StaticRootConfig `json:"staticRoot"` +} + +type StaticRootConfig struct { + Url string `json:"url"` + Path string `json:"path"` + PluginRoot string `json:"-"` } type ExternalPluginRoute struct { diff --git a/pkg/plugins/plugins.go b/pkg/plugins/plugins.go index e823964e485..5397c2db67c 100644 --- a/pkg/plugins/plugins.go +++ b/pkg/plugins/plugins.go @@ -6,6 +6,7 @@ import ( "os" "path" "path/filepath" + "strings" "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/setting" @@ -14,6 +15,7 @@ import ( var ( DataSources map[string]DataSourcePlugin ExternalPlugins []ExternalPlugin + StaticRoutes []*StaticRootConfig ) type PluginScanner struct { @@ -21,12 +23,27 @@ type PluginScanner struct { errors []error } -func Init() { +func Init() error { DataSources = make(map[string]DataSourcePlugin) ExternalPlugins = make([]ExternalPlugin, 0) + StaticRoutes = make([]*StaticRootConfig, 0) scan(path.Join(setting.StaticRootPath, "app/plugins")) - scan(path.Join(setting.DataPath, "plugins")) + checkExternalPluginPaths() + return nil +} + +func checkExternalPluginPaths() error { + for _, section := range setting.Cfg.Sections() { + if strings.HasPrefix(section.Name(), "plugin.") { + path := section.Key("path").String() + if path != "" { + log.Info("Plugin: scaning specific dir %s", path) + scan(path) + } + } + } + return nil } func scan(pluginDir string) error { @@ -45,7 +62,7 @@ func scan(pluginDir string) error { return nil } -func (scanner *PluginScanner) walker(path string, f os.FileInfo, err error) error { +func (scanner *PluginScanner) walker(currentPath string, f os.FileInfo, err error) error { if err != nil { return err } @@ -55,17 +72,18 @@ func (scanner *PluginScanner) walker(path string, f os.FileInfo, err error) erro } if f.Name() == "plugin.json" { - err := scanner.loadPluginJson(path) + err := scanner.loadPluginJson(currentPath) if err != nil { - log.Error(3, "Failed to load plugin json file: %v, err: %v", path, err) + log.Error(3, "Failed to load plugin json file: %v, err: %v", currentPath, err) scanner.errors = append(scanner.errors, err) } } return nil } -func (scanner *PluginScanner) loadPluginJson(path string) error { - reader, err := os.Open(path) +func (scanner *PluginScanner) loadPluginJson(pluginJsonFilePath string) error { + currentDir := filepath.Dir(pluginJsonFilePath) + reader, err := os.Open(pluginJsonFilePath) if err != nil { return err } @@ -96,6 +114,11 @@ func (scanner *PluginScanner) loadPluginJson(path string) error { } DataSources[p.Type] = p + + if p.StaticRootConfig != nil { + p.StaticRootConfig.Path = path.Join(currentDir, p.StaticRootConfig.Path) + StaticRoutes = append(StaticRoutes, p.StaticRootConfig) + } } if pluginType == "externalPlugin" { diff --git a/pkg/setting/setting.go b/pkg/setting/setting.go index d6a853a9cdc..d796d401da2 100644 --- a/pkg/setting/setting.go +++ b/pkg/setting/setting.go @@ -275,13 +275,11 @@ func loadSpecifedConfigFile(configFile string) { defaultSec, err := Cfg.GetSection(section.Name()) if err != nil { - log.Error(3, "Unknown config section %s defined in %s", section.Name(), configFile) - continue + defaultSec, _ = Cfg.NewSection(section.Name()) } defaultKey, err := defaultSec.GetKey(key.Name()) if err != nil { - log.Error(3, "Unknown config key %s defined in section %s, in file %s", key.Name(), section.Name(), configFile) - continue + defaultKey, _ = defaultSec.NewKey(key.Name(), key.Value()) } defaultKey.SetValue(key.Value()) } diff --git a/public/app/plugins/externalPlugins/example/_plugin.json b/public/app/plugins/externalPlugins/example/_plugin.json index a8469eadd62..29f35345d64 100644 --- a/public/app/plugins/externalPlugins/example/_plugin.json +++ b/public/app/plugins/externalPlugins/example/_plugin.json @@ -37,5 +37,11 @@ "adminOnly": false, } ] - } + }, + + "staticRoot": { + "url": "_plugins/test_ds", + "path": "public" + }, + } diff --git a/public/app/require_config.js b/public/app/require_config.js index d9f8e90cad9..1ff540e900e 100644 --- a/public/app/require_config.js +++ b/public/app/require_config.js @@ -3,6 +3,8 @@ require.config({ baseUrl: 'public', paths: { + '_plugins': '../_plugins', + 'lodash-src': 'vendor/lodash', lodash: 'app/core/lodash_extended',