| 
									
										
										
										
											2022-02-08 20:38:43 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-15 22:42:35 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/routing" | 
					
						
							| 
									
										
										
										
											2024-08-14 14:03:00 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/webassets" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/middleware" | 
					
						
							| 
									
										
										
										
											2023-01-27 15:50:36 +08:00
										 |  |  | 	contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" | 
					
						
							| 
									
										
										
										
											2024-08-14 14:03:00 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util/errhttp" | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2022-02-08 20:38:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-14 14:03:00 +08:00
										 |  |  | func (hs *HTTPServer) registerSwaggerUI(r routing.RouteRegister) { | 
					
						
							| 
									
										
										
										
											2023-11-15 22:42:35 +08:00
										 |  |  | 	// Deprecated
 | 
					
						
							|  |  |  | 	r.Get("/swagger-ui", func(w http.ResponseWriter, r *http.Request) { | 
					
						
							|  |  |  | 		http.Redirect(w, r, "swagger", http.StatusMovedPermanently) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	// Deprecated
 | 
					
						
							|  |  |  | 	r.Get("/openapi3", func(w http.ResponseWriter, r *http.Request) { | 
					
						
							| 
									
										
										
										
											2024-08-14 14:03:00 +08:00
										 |  |  | 		http.Redirect(w, r, "swagger", http.StatusMovedPermanently) | 
					
						
							| 
									
										
										
										
											2023-11-15 22:42:35 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2023-08-01 16:27:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-14 14:03:00 +08:00
										 |  |  | 	// The swagger based api navigator
 | 
					
						
							| 
									
										
										
										
											2023-11-15 22:42:35 +08:00
										 |  |  | 	r.Get("/swagger", func(c *contextmodel.ReqContext) { | 
					
						
							| 
									
										
										
										
											2024-08-14 14:03:00 +08:00
										 |  |  | 		ctx := c.Context.Req.Context() | 
					
						
							|  |  |  | 		assets, err := webassets.GetWebAssets(ctx, hs.Cfg, hs.License) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			errhttp.Write(ctx, err, c.Resp) | 
					
						
							|  |  |  | 			return | 
					
						
							| 
									
										
										
										
											2023-11-15 22:42:35 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-08-01 16:27:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-14 14:03:00 +08:00
										 |  |  | 		data := map[string]any{ | 
					
						
							|  |  |  | 			"Nonce":          c.RequestNonce, | 
					
						
							|  |  |  | 			"Assets":         assets, | 
					
						
							|  |  |  | 			"FavIcon":        "public/img/fav32.png", | 
					
						
							|  |  |  | 			"AppleTouchIcon": "public/img/apple-touch-icon.png", | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if hs.Cfg.CSPEnabled { | 
					
						
							|  |  |  | 			data["CSPEnabled"] = true | 
					
						
							|  |  |  | 			data["CSPContent"] = middleware.ReplacePolicyVariables(hs.Cfg.CSPTemplate, hs.Cfg.AppURL, c.RequestNonce) | 
					
						
							| 
									
										
										
										
											2023-11-15 22:42:35 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		c.HTML(http.StatusOK, "swagger", data) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2022-02-08 20:38:43 +08:00
										 |  |  | } |