| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | package plugins | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2016-01-20 01:18:53 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-09 18:17:49 +08:00
										 |  |  | 	"github.com/gosimple/slug" | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2016-03-22 17:15:47 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type AppPluginCss struct { | 
					
						
							|  |  |  | 	Light string `json:"light"` | 
					
						
							|  |  |  | 	Dark  string `json:"dark"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type AppPlugin struct { | 
					
						
							|  |  |  | 	FrontendPluginBase | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | 	Routes []*AppPluginRoute `json:"routes"` | 
					
						
							| 
									
										
										
										
											2016-01-11 17:44:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | 	FoundChildPlugins []*PluginInclude `json:"-"` | 
					
						
							|  |  |  | 	Pinned            bool             `json:"-"` | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-22 01:15:04 +08:00
										 |  |  | type AppPluginRoute struct { | 
					
						
							| 
									
										
										
										
											2018-09-05 19:08:06 +08:00
										 |  |  | 	Path         string                 `json:"path"` | 
					
						
							|  |  |  | 	Method       string                 `json:"method"` | 
					
						
							|  |  |  | 	ReqRole      models.RoleType        `json:"reqRole"` | 
					
						
							|  |  |  | 	Url          string                 `json:"url"` | 
					
						
							|  |  |  | 	Headers      []AppPluginRouteHeader `json:"headers"` | 
					
						
							|  |  |  | 	TokenAuth    *JwtTokenAuth          `json:"tokenAuth"` | 
					
						
							|  |  |  | 	JwtTokenAuth *JwtTokenAuth          `json:"jwtTokenAuth"` | 
					
						
							| 
									
										
										
										
											2016-01-22 01:15:04 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type AppPluginRouteHeader struct { | 
					
						
							|  |  |  | 	Name    string `json:"name"` | 
					
						
							|  |  |  | 	Content string `json:"content"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 21:50:16 +08:00
										 |  |  | // JwtTokenAuth struct is both for normal Token Auth and JWT Token Auth with
 | 
					
						
							|  |  |  | // an uploaded JWT file.
 | 
					
						
							| 
									
										
										
										
											2017-08-23 23:18:43 +08:00
										 |  |  | type JwtTokenAuth struct { | 
					
						
							|  |  |  | 	Url    string            `json:"url"` | 
					
						
							| 
									
										
										
										
											2018-09-06 21:50:16 +08:00
										 |  |  | 	Scopes []string          `json:"scopes"` | 
					
						
							| 
									
										
										
										
											2017-08-23 23:18:43 +08:00
										 |  |  | 	Params map[string]string `json:"params"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-12 17:20:04 +08:00
										 |  |  | func (app *AppPlugin) Load(decoder *json.Decoder, pluginDir string) error { | 
					
						
							|  |  |  | 	if err := decoder.Decode(&app); err != nil { | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-10 18:03:12 +08:00
										 |  |  | 	if err := app.registerPlugin(pluginDir); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-10 05:10:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Apps[app.Id] = app | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (app *AppPlugin) initApp() { | 
					
						
							|  |  |  | 	app.initFrontendPlugin() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 01:18:53 +08:00
										 |  |  | 	// check if we have child panels
 | 
					
						
							|  |  |  | 	for _, panel := range Panels { | 
					
						
							|  |  |  | 		if strings.HasPrefix(panel.PluginDir, app.PluginDir) { | 
					
						
							| 
									
										
										
										
											2016-02-10 05:10:36 +08:00
										 |  |  | 			panel.setPathsBasedOnApp(app) | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | 			app.FoundChildPlugins = append(app.FoundChildPlugins, &PluginInclude{ | 
					
						
							| 
									
										
										
										
											2016-01-20 01:18:53 +08:00
										 |  |  | 				Name: panel.Name, | 
					
						
							|  |  |  | 				Id:   panel.Id, | 
					
						
							|  |  |  | 				Type: panel.Type, | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-10 01:37:08 +08:00
										 |  |  | 	// check if we have child datasources
 | 
					
						
							|  |  |  | 	for _, ds := range DataSources { | 
					
						
							|  |  |  | 		if strings.HasPrefix(ds.PluginDir, app.PluginDir) { | 
					
						
							| 
									
										
										
										
											2016-02-10 05:10:36 +08:00
										 |  |  | 			ds.setPathsBasedOnApp(app) | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | 			app.FoundChildPlugins = append(app.FoundChildPlugins, &PluginInclude{ | 
					
						
							| 
									
										
										
										
											2016-02-10 01:37:08 +08:00
										 |  |  | 				Name: ds.Name, | 
					
						
							|  |  |  | 				Id:   ds.Id, | 
					
						
							|  |  |  | 				Type: ds.Type, | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// slugify pages
 | 
					
						
							| 
									
										
										
										
											2016-03-22 17:15:47 +08: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 18:17:49 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | } |