| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2017-02-15 21:29:20 +08:00
										 |  |  | 	"crypto/tls" | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2017-04-27 14:54:21 +08:00
										 |  |  | 	"net" | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"path" | 
					
						
							| 
									
										
										
										
											2020-10-19 23:35:31 +08:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2021-02-16 00:55:41 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2019-11-27 19:07:21 +08:00
										 |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-02 21:52:30 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/middleware/csrf" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/prometheus/client_golang/prometheus" | 
					
						
							|  |  |  | 	"github.com/prometheus/client_golang/prometheus/promhttp" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 10:56:17 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/avatar" | 
					
						
							| 
									
										
										
										
											2019-01-16 21:53:59 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/routing" | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 	httpstatic "github.com/grafana/grafana/pkg/api/static" | 
					
						
							| 
									
										
										
										
											2017-04-25 23:17:45 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/components/simplejson" | 
					
						
							| 
									
										
										
										
											2022-05-22 08:44:12 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/framework/coremodel" | 
					
						
							| 
									
										
										
										
											2019-06-13 16:55:38 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/localcache" | 
					
						
							| 
									
										
										
										
											2019-05-13 14:45:54 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/log" | 
					
						
							| 
									
										
										
										
											2019-04-08 19:31:46 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/remotecache" | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/tracing" | 
					
						
							| 
									
										
										
										
											2022-04-05 02:36:15 +08:00
										 |  |  | 	loginpkg "github.com/grafana/grafana/pkg/login" | 
					
						
							| 
									
										
										
										
											2021-08-31 01:39:55 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/login/social" | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/middleware" | 
					
						
							| 
									
										
										
										
											2017-04-25 23:17:45 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2021-03-22 20:22:48 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins" | 
					
						
							| 
									
										
										
										
											2021-03-24 01:24:08 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins/plugincontext" | 
					
						
							| 
									
										
										
										
											2021-04-06 21:49:09 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/accesscontrol" | 
					
						
							| 
									
										
										
										
											2021-03-22 20:22:48 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/alerting" | 
					
						
							| 
									
										
										
										
											2021-08-31 01:39:55 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/cleanup" | 
					
						
							| 
									
										
										
										
											2022-02-22 15:47:42 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/comments" | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/contexthandler" | 
					
						
							| 
									
										
										
										
											2022-02-16 21:15:44 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/dashboards" | 
					
						
							| 
									
										
										
										
											2022-02-17 16:31:26 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/dashboardsnapshots" | 
					
						
							| 
									
										
										
										
											2022-05-25 16:41:51 +08:00
										 |  |  | 	dashver "github.com/grafana/grafana/pkg/services/dashboardversion" | 
					
						
							| 
									
										
										
										
											2021-03-18 01:10:40 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/datasourceproxy" | 
					
						
							| 
									
										
										
										
											2018-10-26 16:40:33 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/datasources" | 
					
						
							| 
									
										
										
										
											2022-02-28 16:54:56 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/datasources/permissions" | 
					
						
							| 
									
										
										
										
											2021-08-31 01:39:55 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/encryption" | 
					
						
							| 
									
										
										
										
											2022-04-26 07:59:18 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/export" | 
					
						
							| 
									
										
										
										
											2022-01-27 01:44:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/featuremgmt" | 
					
						
							| 
									
										
										
										
											2018-10-12 17:26:42 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/hooks" | 
					
						
							| 
									
										
										
										
											2022-02-01 19:03:21 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/ldap" | 
					
						
							| 
									
										
										
										
											2021-08-31 01:39:55 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/libraryelements" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/librarypanels" | 
					
						
							| 
									
										
										
										
											2021-03-22 20:22:48 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/live" | 
					
						
							| 
									
										
										
										
											2021-04-26 18:17:49 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/live/pushhttp" | 
					
						
							| 
									
										
										
										
											2019-06-13 22:47:52 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/login" | 
					
						
							| 
									
										
										
										
											2021-08-31 01:39:55 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/ngalert" | 
					
						
							| 
									
										
										
										
											2022-02-03 17:33:46 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/notifications" | 
					
						
							| 
									
										
										
										
											2022-03-11 01:38:04 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/plugindashboards" | 
					
						
							| 
									
										
										
										
											2022-03-03 18:39:15 +08:00
										 |  |  | 	pluginSettings "github.com/grafana/grafana/pkg/services/pluginsettings/service" | 
					
						
							| 
									
										
										
										
											2022-04-21 21:03:17 +08:00
										 |  |  | 	pref "github.com/grafana/grafana/pkg/services/preference" | 
					
						
							| 
									
										
										
										
											2020-03-25 21:14:24 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/provisioning" | 
					
						
							| 
									
										
										
										
											2022-01-26 22:48:41 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/query" | 
					
						
							| 
									
										
										
										
											2022-01-29 00:55:09 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/queryhistory" | 
					
						
							| 
									
										
										
										
											2019-02-12 04:12:01 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/quota" | 
					
						
							| 
									
										
										
										
											2018-05-24 21:26:27 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/rendering" | 
					
						
							| 
									
										
										
										
											2021-03-22 20:22:48 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/search" | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/searchusers" | 
					
						
							| 
									
										
										
										
											2021-11-05 00:47:21 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/secrets" | 
					
						
							| 
									
										
										
										
											2022-01-26 22:48:41 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/serviceaccounts" | 
					
						
							| 
									
										
										
										
											2021-03-22 20:22:48 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/shorturls" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/sqlstore" | 
					
						
							| 
									
										
										
										
											2022-05-19 20:32:10 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/star" | 
					
						
							| 
									
										
										
										
											2022-03-18 01:19:23 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/store" | 
					
						
							| 
									
										
										
										
											2022-01-07 20:38:00 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/teamguardian" | 
					
						
							| 
									
										
										
										
											2022-01-26 22:48:41 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/thumbs" | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/updatechecker" | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/web" | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | type HTTPServer struct { | 
					
						
							| 
									
										
										
										
											2022-01-24 21:41:18 +08:00
										 |  |  | 	log              log.Logger | 
					
						
							|  |  |  | 	web              *web.Mux | 
					
						
							|  |  |  | 	context          context.Context | 
					
						
							|  |  |  | 	httpSrv          *http.Server | 
					
						
							|  |  |  | 	middlewares      []web.Handler | 
					
						
							|  |  |  | 	namedMiddlewares []routing.RegisterNamedMiddleware | 
					
						
							| 
									
										
										
										
											2017-02-06 16:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-09 21:01:32 +08:00
										 |  |  | 	PluginContextProvider        *plugincontext.Provider | 
					
						
							|  |  |  | 	RouteRegister                routing.RouteRegister | 
					
						
							|  |  |  | 	RenderService                rendering.Service | 
					
						
							|  |  |  | 	Cfg                          *setting.Cfg | 
					
						
							|  |  |  | 	Features                     *featuremgmt.FeatureManager | 
					
						
							|  |  |  | 	SettingsProvider             setting.Provider | 
					
						
							|  |  |  | 	HooksService                 *hooks.HooksService | 
					
						
							|  |  |  | 	CacheService                 *localcache.CacheService | 
					
						
							|  |  |  | 	DataSourceCache              datasources.CacheService | 
					
						
							|  |  |  | 	AuthTokenService             models.UserTokenService | 
					
						
							|  |  |  | 	QuotaService                 *quota.QuotaService | 
					
						
							|  |  |  | 	RemoteCacheService           *remotecache.RemoteCache | 
					
						
							|  |  |  | 	ProvisioningService          provisioning.ProvisioningService | 
					
						
							|  |  |  | 	Login                        login.Service | 
					
						
							|  |  |  | 	License                      models.Licensing | 
					
						
							|  |  |  | 	AccessControl                accesscontrol.AccessControl | 
					
						
							|  |  |  | 	DataProxy                    *datasourceproxy.DataSourceProxyService | 
					
						
							|  |  |  | 	PluginRequestValidator       models.PluginRequestValidator | 
					
						
							|  |  |  | 	pluginClient                 plugins.Client | 
					
						
							|  |  |  | 	pluginStore                  plugins.Store | 
					
						
							| 
									
										
										
										
											2022-03-11 01:38:04 +08:00
										 |  |  | 	pluginDashboardService       plugindashboards.Service | 
					
						
							| 
									
										
										
										
											2022-02-09 21:01:32 +08:00
										 |  |  | 	pluginStaticRouteResolver    plugins.StaticRouteResolver | 
					
						
							|  |  |  | 	pluginErrorResolver          plugins.ErrorResolver | 
					
						
							|  |  |  | 	SearchService                search.Service | 
					
						
							|  |  |  | 	ShortURLService              shorturls.Service | 
					
						
							|  |  |  | 	QueryHistoryService          queryhistory.Service | 
					
						
							|  |  |  | 	Live                         *live.GrafanaLive | 
					
						
							|  |  |  | 	LivePushGateway              *pushhttp.Gateway | 
					
						
							|  |  |  | 	ThumbService                 thumbs.Service | 
					
						
							| 
									
										
										
										
											2022-04-26 07:59:18 +08:00
										 |  |  | 	ExportService                export.ExportService | 
					
						
							| 
									
										
										
										
											2022-03-18 01:19:23 +08:00
										 |  |  | 	StorageService               store.HTTPStorageService | 
					
						
							| 
									
										
										
										
											2022-02-09 21:01:32 +08:00
										 |  |  | 	ContextHandler               *contexthandler.ContextHandler | 
					
						
							|  |  |  | 	SQLStore                     sqlstore.Store | 
					
						
							|  |  |  | 	AlertEngine                  *alerting.AlertEngine | 
					
						
							|  |  |  | 	AlertNG                      *ngalert.AlertNG | 
					
						
							|  |  |  | 	LibraryPanelService          librarypanels.Service | 
					
						
							|  |  |  | 	LibraryElementService        libraryelements.Service | 
					
						
							|  |  |  | 	SocialService                social.Service | 
					
						
							|  |  |  | 	Listener                     net.Listener | 
					
						
							|  |  |  | 	EncryptionService            encryption.Internal | 
					
						
							|  |  |  | 	SecretsService               secrets.Service | 
					
						
							| 
									
										
										
										
											2022-02-11 22:52:14 +08:00
										 |  |  | 	DataSourcesService           datasources.DataSourceService | 
					
						
							| 
									
										
										
										
											2022-02-09 21:01:32 +08:00
										 |  |  | 	cleanUpService               *cleanup.CleanUpService | 
					
						
							|  |  |  | 	tracer                       tracing.Tracer | 
					
						
							|  |  |  | 	grafanaUpdateChecker         *updatechecker.GrafanaService | 
					
						
							|  |  |  | 	pluginsUpdateChecker         *updatechecker.PluginsService | 
					
						
							|  |  |  | 	searchUsersService           searchusers.Service | 
					
						
							|  |  |  | 	ldapGroups                   ldap.Groups | 
					
						
							|  |  |  | 	teamGuardian                 teamguardian.TeamGuardian | 
					
						
							|  |  |  | 	queryDataService             *query.Service | 
					
						
							|  |  |  | 	serviceAccountsService       serviceaccounts.Service | 
					
						
							|  |  |  | 	authInfoService              login.AuthInfoService | 
					
						
							| 
									
										
										
										
											2022-04-05 02:36:15 +08:00
										 |  |  | 	authenticator                loginpkg.Authenticator | 
					
						
							| 
									
										
										
										
											2022-05-10 21:48:47 +08:00
										 |  |  | 	teamPermissionsService       accesscontrol.TeamPermissionsService | 
					
						
							| 
									
										
										
										
											2022-02-09 21:01:32 +08:00
										 |  |  | 	NotificationService          *notifications.NotificationService | 
					
						
							| 
									
										
										
										
											2022-02-16 21:15:44 +08:00
										 |  |  | 	dashboardService             dashboards.DashboardService | 
					
						
							|  |  |  | 	dashboardProvisioningService dashboards.DashboardProvisioningService | 
					
						
							|  |  |  | 	folderService                dashboards.FolderService | 
					
						
							| 
									
										
										
										
											2022-02-28 16:54:56 +08:00
										 |  |  | 	DatasourcePermissionsService permissions.DatasourcePermissionsService | 
					
						
							| 
									
										
										
										
											2022-02-22 15:47:42 +08:00
										 |  |  | 	commentsService              *comments.Service | 
					
						
							| 
									
										
										
										
											2022-02-17 01:54:29 +08:00
										 |  |  | 	AlertNotificationService     *alerting.AlertNotificationService | 
					
						
							| 
									
										
										
										
											2022-02-17 16:31:26 +08:00
										 |  |  | 	DashboardsnapshotsService    *dashboardsnapshots.Service | 
					
						
							| 
									
										
										
										
											2022-03-03 18:39:15 +08:00
										 |  |  | 	PluginSettings               *pluginSettings.Service | 
					
						
							| 
									
										
										
										
											2022-04-06 10:56:17 +08:00
										 |  |  | 	AvatarCacheServer            *avatar.AvatarCacheServer | 
					
						
							| 
									
										
										
										
											2022-04-21 21:03:17 +08:00
										 |  |  | 	preferenceService            pref.Service | 
					
						
							| 
									
										
										
										
											2022-06-02 21:52:30 +08:00
										 |  |  | 	Csrf                         csrf.Service | 
					
						
							| 
									
										
										
										
											2022-04-27 16:29:39 +08:00
										 |  |  | 	entityEventsService          store.EntityEventsService | 
					
						
							| 
									
										
										
										
											2022-05-10 21:48:47 +08:00
										 |  |  | 	folderPermissionsService     accesscontrol.FolderPermissionsService | 
					
						
							|  |  |  | 	dashboardPermissionsService  accesscontrol.DashboardPermissionsService | 
					
						
							| 
									
										
										
										
											2022-05-25 16:41:51 +08:00
										 |  |  | 	dashboardVersionService      dashver.Service | 
					
						
							| 
									
										
										
										
											2022-05-19 20:32:10 +08:00
										 |  |  | 	starService                  star.Service | 
					
						
							| 
									
										
										
										
											2022-05-22 08:44:12 +08:00
										 |  |  | 	CoremodelRegistry            *coremodel.Registry | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type ServerOptions struct { | 
					
						
							|  |  |  | 	Listener net.Listener | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-13 21:24:13 +08:00
										 |  |  | func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routing.RouteRegister, | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | 	renderService rendering.Service, licensing models.Licensing, hooksService *hooks.HooksService, | 
					
						
							| 
									
										
										
										
											2021-11-29 21:21:54 +08:00
										 |  |  | 	cacheService *localcache.CacheService, sqlStore *sqlstore.SQLStore, alertEngine *alerting.AlertEngine, | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	pluginRequestValidator models.PluginRequestValidator, pluginStaticRouteResolver plugins.StaticRouteResolver, | 
					
						
							| 
									
										
										
										
											2022-03-11 01:38:04 +08:00
										 |  |  | 	pluginDashboardService plugindashboards.Service, pluginStore plugins.Store, pluginClient plugins.Client, | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	pluginErrorResolver plugins.ErrorResolver, settingsProvider setting.Provider, | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | 	dataSourceCache datasources.CacheService, userTokenService models.UserTokenService, | 
					
						
							| 
									
										
										
										
											2022-01-29 00:55:09 +08:00
										 |  |  | 	cleanUpService *cleanup.CleanUpService, shortURLService shorturls.Service, queryHistoryService queryhistory.Service, | 
					
						
							|  |  |  | 	thumbService thumbs.Service, remoteCache *remotecache.RemoteCache, provisioningService provisioning.ProvisioningService, | 
					
						
							| 
									
										
										
										
											2022-04-05 02:36:15 +08:00
										 |  |  | 	loginService login.Service, authenticator loginpkg.Authenticator, accessControl accesscontrol.AccessControl, | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | 	dataSourceProxy *datasourceproxy.DataSourceProxyService, searchService *search.SearchService, | 
					
						
							|  |  |  | 	live *live.GrafanaLive, livePushGateway *pushhttp.Gateway, plugCtxProvider *plugincontext.Provider, | 
					
						
							| 
									
										
										
										
											2022-01-27 01:44:20 +08:00
										 |  |  | 	contextHandler *contexthandler.ContextHandler, features *featuremgmt.FeatureManager, | 
					
						
							| 
									
										
										
										
											2022-06-07 08:52:44 +08:00
										 |  |  | 	alertNG *ngalert.AlertNG, libraryPanelService librarypanels.Service, libraryElementService libraryelements.Service, | 
					
						
							| 
									
										
										
										
											2022-04-26 07:59:18 +08:00
										 |  |  | 	quotaService *quota.QuotaService, socialService social.Service, tracer tracing.Tracer, exportService export.ExportService, | 
					
						
							| 
									
										
										
										
											2022-01-31 23:06:16 +08:00
										 |  |  | 	encryptionService encryption.Internal, grafanaUpdateChecker *updatechecker.GrafanaService, | 
					
						
							|  |  |  | 	pluginsUpdateChecker *updatechecker.PluginsService, searchUsersService searchusers.Service, | 
					
						
							| 
									
										
										
										
											2022-02-11 22:52:14 +08:00
										 |  |  | 	dataSourcesService datasources.DataSourceService, secretsService secrets.Service, queryDataService *query.Service, | 
					
						
							| 
									
										
										
										
											2022-02-01 19:03:21 +08:00
										 |  |  | 	ldapGroups ldap.Groups, teamGuardian teamguardian.TeamGuardian, serviceaccountsService serviceaccounts.Service, | 
					
						
							| 
									
										
										
										
											2022-05-10 21:48:47 +08:00
										 |  |  | 	authInfoService login.AuthInfoService, storageService store.HTTPStorageService, | 
					
						
							| 
									
										
										
										
											2022-02-17 01:54:29 +08:00
										 |  |  | 	notificationService *notifications.NotificationService, dashboardService dashboards.DashboardService, | 
					
						
							|  |  |  | 	dashboardProvisioningService dashboards.DashboardProvisioningService, folderService dashboards.FolderService, | 
					
						
							| 
									
										
										
										
											2022-02-28 16:54:56 +08:00
										 |  |  | 	datasourcePermissionsService permissions.DatasourcePermissionsService, alertNotificationService *alerting.AlertNotificationService, | 
					
						
							| 
									
										
										
										
											2022-03-03 18:39:15 +08:00
										 |  |  | 	dashboardsnapshotsService *dashboardsnapshots.Service, commentsService *comments.Service, pluginSettings *pluginSettings.Service, | 
					
						
							| 
									
										
										
										
											2022-04-27 16:29:39 +08:00
										 |  |  | 	avatarCacheServer *avatar.AvatarCacheServer, preferenceService pref.Service, entityEventsService store.EntityEventsService, | 
					
						
							| 
									
										
										
										
											2022-05-10 21:48:47 +08:00
										 |  |  | 	teamsPermissionsService accesscontrol.TeamPermissionsService, folderPermissionsService accesscontrol.FolderPermissionsService, | 
					
						
							| 
									
										
										
										
											2022-05-25 16:41:51 +08:00
										 |  |  | 	dashboardPermissionsService accesscontrol.DashboardPermissionsService, dashboardVersionService dashver.Service, | 
					
						
							| 
									
										
										
										
											2022-06-02 21:52:30 +08:00
										 |  |  | 	starService star.Service, coremodelRegistry *coremodel.Registry, csrfService csrf.Service, | 
					
						
							| 
									
										
										
										
											2022-02-17 01:54:29 +08:00
										 |  |  | ) (*HTTPServer, error) { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	web.Env = cfg.Env | 
					
						
							|  |  |  | 	m := web.New() | 
					
						
							| 
									
										
										
										
											2018-05-01 21:51:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | 	hs := &HTTPServer{ | 
					
						
							| 
									
										
										
										
											2022-02-09 21:01:32 +08:00
										 |  |  | 		Cfg:                          cfg, | 
					
						
							|  |  |  | 		RouteRegister:                routeRegister, | 
					
						
							|  |  |  | 		RenderService:                renderService, | 
					
						
							|  |  |  | 		License:                      licensing, | 
					
						
							|  |  |  | 		HooksService:                 hooksService, | 
					
						
							|  |  |  | 		CacheService:                 cacheService, | 
					
						
							|  |  |  | 		SQLStore:                     sqlStore, | 
					
						
							|  |  |  | 		AlertEngine:                  alertEngine, | 
					
						
							|  |  |  | 		PluginRequestValidator:       pluginRequestValidator, | 
					
						
							|  |  |  | 		pluginClient:                 pluginClient, | 
					
						
							|  |  |  | 		pluginStore:                  pluginStore, | 
					
						
							|  |  |  | 		pluginStaticRouteResolver:    pluginStaticRouteResolver, | 
					
						
							| 
									
										
										
										
											2022-03-11 01:38:04 +08:00
										 |  |  | 		pluginDashboardService:       pluginDashboardService, | 
					
						
							| 
									
										
										
										
											2022-02-09 21:01:32 +08:00
										 |  |  | 		pluginErrorResolver:          pluginErrorResolver, | 
					
						
							|  |  |  | 		grafanaUpdateChecker:         grafanaUpdateChecker, | 
					
						
							|  |  |  | 		pluginsUpdateChecker:         pluginsUpdateChecker, | 
					
						
							|  |  |  | 		SettingsProvider:             settingsProvider, | 
					
						
							|  |  |  | 		DataSourceCache:              dataSourceCache, | 
					
						
							|  |  |  | 		AuthTokenService:             userTokenService, | 
					
						
							|  |  |  | 		cleanUpService:               cleanUpService, | 
					
						
							|  |  |  | 		ShortURLService:              shortURLService, | 
					
						
							|  |  |  | 		QueryHistoryService:          queryHistoryService, | 
					
						
							|  |  |  | 		Features:                     features, | 
					
						
							|  |  |  | 		ThumbService:                 thumbService, | 
					
						
							| 
									
										
										
										
											2022-03-18 01:19:23 +08:00
										 |  |  | 		StorageService:               storageService, | 
					
						
							| 
									
										
										
										
											2022-02-09 21:01:32 +08:00
										 |  |  | 		RemoteCacheService:           remoteCache, | 
					
						
							|  |  |  | 		ProvisioningService:          provisioningService, | 
					
						
							|  |  |  | 		Login:                        loginService, | 
					
						
							|  |  |  | 		AccessControl:                accessControl, | 
					
						
							|  |  |  | 		DataProxy:                    dataSourceProxy, | 
					
						
							|  |  |  | 		SearchService:                searchService, | 
					
						
							| 
									
										
										
										
											2022-04-26 07:59:18 +08:00
										 |  |  | 		ExportService:                exportService, | 
					
						
							| 
									
										
										
										
											2022-02-09 21:01:32 +08:00
										 |  |  | 		Live:                         live, | 
					
						
							|  |  |  | 		LivePushGateway:              livePushGateway, | 
					
						
							|  |  |  | 		PluginContextProvider:        plugCtxProvider, | 
					
						
							|  |  |  | 		ContextHandler:               contextHandler, | 
					
						
							|  |  |  | 		AlertNG:                      alertNG, | 
					
						
							|  |  |  | 		LibraryPanelService:          libraryPanelService, | 
					
						
							|  |  |  | 		LibraryElementService:        libraryElementService, | 
					
						
							|  |  |  | 		QuotaService:                 quotaService, | 
					
						
							|  |  |  | 		tracer:                       tracer, | 
					
						
							|  |  |  | 		log:                          log.New("http.server"), | 
					
						
							|  |  |  | 		web:                          m, | 
					
						
							|  |  |  | 		Listener:                     opts.Listener, | 
					
						
							|  |  |  | 		SocialService:                socialService, | 
					
						
							|  |  |  | 		EncryptionService:            encryptionService, | 
					
						
							|  |  |  | 		SecretsService:               secretsService, | 
					
						
							|  |  |  | 		DataSourcesService:           dataSourcesService, | 
					
						
							|  |  |  | 		searchUsersService:           searchUsersService, | 
					
						
							|  |  |  | 		ldapGroups:                   ldapGroups, | 
					
						
							|  |  |  | 		teamGuardian:                 teamGuardian, | 
					
						
							|  |  |  | 		queryDataService:             queryDataService, | 
					
						
							|  |  |  | 		serviceAccountsService:       serviceaccountsService, | 
					
						
							|  |  |  | 		authInfoService:              authInfoService, | 
					
						
							| 
									
										
										
										
											2022-04-05 02:36:15 +08:00
										 |  |  | 		authenticator:                authenticator, | 
					
						
							| 
									
										
										
										
											2022-02-09 21:01:32 +08:00
										 |  |  | 		NotificationService:          notificationService, | 
					
						
							| 
									
										
										
										
											2022-02-16 21:15:44 +08:00
										 |  |  | 		dashboardService:             dashboardService, | 
					
						
							|  |  |  | 		dashboardProvisioningService: dashboardProvisioningService, | 
					
						
							|  |  |  | 		folderService:                folderService, | 
					
						
							| 
									
										
										
										
											2022-02-09 21:01:32 +08:00
										 |  |  | 		DatasourcePermissionsService: datasourcePermissionsService, | 
					
						
							| 
									
										
										
										
											2022-02-22 15:47:42 +08:00
										 |  |  | 		commentsService:              commentsService, | 
					
						
							| 
									
										
										
										
											2022-05-10 21:48:47 +08:00
										 |  |  | 		teamPermissionsService:       teamsPermissionsService, | 
					
						
							| 
									
										
										
										
											2022-02-17 01:54:29 +08:00
										 |  |  | 		AlertNotificationService:     alertNotificationService, | 
					
						
							| 
									
										
										
										
											2022-02-17 16:31:26 +08:00
										 |  |  | 		DashboardsnapshotsService:    dashboardsnapshotsService, | 
					
						
							| 
									
										
										
										
											2022-02-25 18:29:18 +08:00
										 |  |  | 		PluginSettings:               pluginSettings, | 
					
						
							| 
									
										
										
										
											2022-04-06 10:56:17 +08:00
										 |  |  | 		AvatarCacheServer:            avatarCacheServer, | 
					
						
							| 
									
										
										
										
											2022-04-21 21:03:17 +08:00
										 |  |  | 		preferenceService:            preferenceService, | 
					
						
							| 
									
										
										
										
											2022-06-02 21:52:30 +08:00
										 |  |  | 		Csrf:                         csrfService, | 
					
						
							| 
									
										
										
										
											2022-04-27 16:29:39 +08:00
										 |  |  | 		entityEventsService:          entityEventsService, | 
					
						
							| 
									
										
										
										
											2022-05-10 21:48:47 +08:00
										 |  |  | 		folderPermissionsService:     folderPermissionsService, | 
					
						
							|  |  |  | 		dashboardPermissionsService:  dashboardPermissionsService, | 
					
						
							| 
									
										
										
										
											2022-05-25 16:41:51 +08:00
										 |  |  | 		dashboardVersionService:      dashboardVersionService, | 
					
						
							| 
									
										
										
										
											2022-05-19 20:32:10 +08:00
										 |  |  | 		starService:                  starService, | 
					
						
							| 
									
										
										
										
											2022-05-22 08:44:12 +08:00
										 |  |  | 		CoremodelRegistry:            coremodelRegistry, | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if hs.Listener != nil { | 
					
						
							|  |  |  | 		hs.log.Debug("Using provided listener") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-01 22:01:43 +08:00
										 |  |  | 	hs.registerRoutes() | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 00:33:21 +08:00
										 |  |  | 	// Register access control scope resolver for annotations
 | 
					
						
							| 
									
										
										
										
											2022-05-02 15:29:30 +08:00
										 |  |  | 	hs.AccessControl.RegisterScopeAttributeResolver(AnnotationTypeScopeResolver()) | 
					
						
							| 
									
										
										
										
											2022-03-19 00:33:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | 	if err := hs.declareFixedRoles(); err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return hs, nil | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | func (hs *HTTPServer) AddMiddleware(middleware web.Handler) { | 
					
						
							| 
									
										
										
										
											2020-07-23 15:54:07 +08:00
										 |  |  | 	hs.middlewares = append(hs.middlewares, middleware) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-24 21:41:18 +08:00
										 |  |  | func (hs *HTTPServer) AddNamedMiddleware(middleware routing.RegisterNamedMiddleware) { | 
					
						
							|  |  |  | 	hs.namedMiddlewares = append(hs.namedMiddlewares, middleware) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-01 21:51:15 +08:00
										 |  |  | func (hs *HTTPServer) Run(ctx context.Context) error { | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 	hs.context = ctx | 
					
						
							| 
									
										
										
										
											2018-07-02 14:35:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-01 22:01:43 +08:00
										 |  |  | 	hs.applyRoutes() | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-16 00:55:41 +08:00
										 |  |  | 	// Remove any square brackets enclosing IPv6 addresses, a format we support for backwards compatibility
 | 
					
						
							| 
									
										
										
										
											2021-03-10 19:41:29 +08:00
										 |  |  | 	host := strings.TrimSuffix(strings.TrimPrefix(hs.Cfg.HTTPAddr, "["), "]") | 
					
						
							| 
									
										
										
										
											2019-11-27 19:07:21 +08:00
										 |  |  | 	hs.httpSrv = &http.Server{ | 
					
						
							| 
									
										
										
										
											2021-03-19 18:21:52 +08:00
										 |  |  | 		Addr:        net.JoinHostPort(host, hs.Cfg.HTTPPort), | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 		Handler:     hs.web, | 
					
						
							| 
									
										
										
										
											2021-03-19 18:21:52 +08:00
										 |  |  | 		ReadTimeout: hs.Cfg.ReadTimeout, | 
					
						
							| 
									
										
										
										
											2019-10-22 19:07:43 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 	switch hs.Cfg.Protocol { | 
					
						
							| 
									
										
										
										
											2020-10-02 21:45:45 +08:00
										 |  |  | 	case setting.HTTP2Scheme: | 
					
						
							| 
									
										
										
										
											2019-11-27 19:07:21 +08:00
										 |  |  | 		if err := hs.configureHttp2(); err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							| 
									
										
										
										
											2019-08-16 23:06:54 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-10-02 21:45:45 +08:00
										 |  |  | 	case setting.HTTPSScheme: | 
					
						
							| 
									
										
										
										
											2019-11-27 19:07:21 +08:00
										 |  |  | 		if err := hs.configureHttps(); err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-12-01 16:53:27 +08:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2019-11-27 19:07:21 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 16:41:10 +08:00
										 |  |  | 	listener, err := hs.getListener() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2019-11-27 19:07:21 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-22 21:14:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-27 19:07:21 +08:00
										 |  |  | 	hs.log.Info("HTTP Server Listen", "address", listener.Addr().String(), "protocol", | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 		hs.Cfg.Protocol, "subUrl", hs.Cfg.AppSubURL, "socket", hs.Cfg.SocketPath) | 
					
						
							| 
									
										
										
										
											2019-11-27 19:07:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var wg sync.WaitGroup | 
					
						
							|  |  |  | 	wg.Add(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// handle http shutdown on server context done
 | 
					
						
							|  |  |  | 	go func() { | 
					
						
							| 
									
										
										
										
											2019-11-27 20:42:15 +08:00
										 |  |  | 		defer wg.Done() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-27 19:07:21 +08:00
										 |  |  | 		<-ctx.Done() | 
					
						
							|  |  |  | 		if err := hs.httpSrv.Shutdown(context.Background()); err != nil { | 
					
						
							|  |  |  | 			hs.log.Error("Failed to shutdown server", "error", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 	switch hs.Cfg.Protocol { | 
					
						
							| 
									
										
										
										
											2020-10-02 21:45:45 +08:00
										 |  |  | 	case setting.HTTPScheme, setting.SocketScheme: | 
					
						
							| 
									
										
										
										
											2019-11-27 19:07:21 +08:00
										 |  |  | 		if err := hs.httpSrv.Serve(listener); err != nil { | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 			if errors.Is(err, http.ErrServerClosed) { | 
					
						
							| 
									
										
										
										
											2019-11-27 19:07:21 +08:00
										 |  |  | 				hs.log.Debug("server was shutdown gracefully") | 
					
						
							|  |  |  | 				return nil | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-10-02 21:45:45 +08:00
										 |  |  | 	case setting.HTTP2Scheme, setting.HTTPSScheme: | 
					
						
							| 
									
										
										
										
											2021-03-10 19:41:29 +08:00
										 |  |  | 		if err := hs.httpSrv.ServeTLS(listener, hs.Cfg.CertFile, hs.Cfg.KeyFile); err != nil { | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 			if errors.Is(err, http.ErrServerClosed) { | 
					
						
							| 
									
										
										
										
											2019-11-27 19:07:21 +08:00
										 |  |  | 				hs.log.Debug("server was shutdown gracefully") | 
					
						
							|  |  |  | 				return nil | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return err | 
					
						
							| 
									
										
										
										
											2017-04-27 14:54:21 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 		panic(fmt.Sprintf("Unhandled protocol %q", hs.Cfg.Protocol)) | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-27 19:07:21 +08:00
										 |  |  | 	wg.Wait() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 16:41:10 +08:00
										 |  |  | func (hs *HTTPServer) getListener() (net.Listener, error) { | 
					
						
							|  |  |  | 	if hs.Listener != nil { | 
					
						
							|  |  |  | 		return hs.Listener, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 	switch hs.Cfg.Protocol { | 
					
						
							| 
									
										
										
										
											2020-10-02 21:45:45 +08:00
										 |  |  | 	case setting.HTTPScheme, setting.HTTPSScheme, setting.HTTP2Scheme: | 
					
						
							| 
									
										
										
										
											2020-08-21 16:41:10 +08:00
										 |  |  | 		listener, err := net.Listen("tcp", hs.httpSrv.Addr) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2022-06-07 04:30:31 +08:00
										 |  |  | 			return nil, fmt.Errorf("failed to open listener on address %s: %w", hs.httpSrv.Addr, err) | 
					
						
							| 
									
										
										
										
											2020-08-21 16:41:10 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		return listener, nil | 
					
						
							| 
									
										
										
										
											2020-10-02 21:45:45 +08:00
										 |  |  | 	case setting.SocketScheme: | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 		listener, err := net.ListenUnix("unix", &net.UnixAddr{Name: hs.Cfg.SocketPath, Net: "unix"}) | 
					
						
							| 
									
										
										
										
											2020-08-21 16:41:10 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2022-06-07 04:30:31 +08:00
										 |  |  | 			return nil, fmt.Errorf("failed to open listener for socket %s: %w", hs.Cfg.SocketPath, err) | 
					
						
							| 
									
										
										
										
											2020-08-21 16:41:10 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Make socket writable by group
 | 
					
						
							| 
									
										
										
										
											2020-12-10 18:53:21 +08:00
										 |  |  | 		// nolint:gosec
 | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 		if err := os.Chmod(hs.Cfg.SocketPath, 0660); err != nil { | 
					
						
							| 
									
										
										
										
											2022-06-07 04:30:31 +08:00
										 |  |  | 			return nil, fmt.Errorf("failed to change socket permissions: %w", err) | 
					
						
							| 
									
										
										
										
											2020-08-21 16:41:10 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return listener, nil | 
					
						
							|  |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 		hs.log.Error("Invalid protocol", "protocol", hs.Cfg.Protocol) | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("invalid protocol %q", hs.Cfg.Protocol) | 
					
						
							| 
									
										
										
										
											2020-08-21 16:41:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-27 19:07:21 +08:00
										 |  |  | func (hs *HTTPServer) configureHttps() error { | 
					
						
							| 
									
										
										
										
											2021-03-10 19:41:29 +08:00
										 |  |  | 	if hs.Cfg.CertFile == "" { | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 		return fmt.Errorf("cert_file cannot be empty when using HTTPS") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-10 19:41:29 +08:00
										 |  |  | 	if hs.Cfg.KeyFile == "" { | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 		return fmt.Errorf("cert_key cannot be empty when using HTTPS") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-10 19:41:29 +08:00
										 |  |  | 	if _, err := os.Stat(hs.Cfg.CertFile); os.IsNotExist(err) { | 
					
						
							|  |  |  | 		return fmt.Errorf(`cannot find SSL cert_file at %q`, hs.Cfg.CertFile) | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-10 19:41:29 +08:00
										 |  |  | 	if _, err := os.Stat(hs.Cfg.KeyFile); os.IsNotExist(err) { | 
					
						
							|  |  |  | 		return fmt.Errorf(`cannot find SSL key_file at %q`, hs.Cfg.KeyFile) | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-15 21:29:20 +08:00
										 |  |  | 	tlsCfg := &tls.Config{ | 
					
						
							| 
									
										
										
										
											2022-03-04 00:48:26 +08:00
										 |  |  | 		MinVersion: tls.VersionTLS12, | 
					
						
							| 
									
										
										
										
											2017-02-15 21:29:20 +08:00
										 |  |  | 		CipherSuites: []uint16{ | 
					
						
							| 
									
										
										
										
											2020-02-19 01:58:36 +08:00
										 |  |  | 			tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | 
					
						
							|  |  |  | 			tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, | 
					
						
							|  |  |  | 			tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | 
					
						
							|  |  |  | 			tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, | 
					
						
							| 
									
										
										
										
											2017-02-15 21:29:20 +08:00
										 |  |  | 			tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, | 
					
						
							| 
									
										
										
										
											2020-02-19 01:58:36 +08:00
										 |  |  | 			tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, | 
					
						
							| 
									
										
										
										
											2017-02-15 21:29:20 +08:00
										 |  |  | 			tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, | 
					
						
							| 
									
										
										
										
											2020-02-19 01:58:36 +08:00
										 |  |  | 			tls.TLS_RSA_WITH_AES_128_GCM_SHA256, | 
					
						
							|  |  |  | 			tls.TLS_RSA_WITH_AES_256_GCM_SHA384, | 
					
						
							|  |  |  | 			tls.TLS_RSA_WITH_AES_128_CBC_SHA, | 
					
						
							|  |  |  | 			tls.TLS_RSA_WITH_AES_256_CBC_SHA, | 
					
						
							| 
									
										
										
										
											2017-02-15 21:29:20 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 05:10:12 +08:00
										 |  |  | 	hs.httpSrv.TLSConfig = tlsCfg | 
					
						
							| 
									
										
										
										
											2018-04-17 02:22:12 +08:00
										 |  |  | 	hs.httpSrv.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler)) | 
					
						
							| 
									
										
										
										
											2017-06-18 05:10:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-27 19:07:21 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-27 19:07:21 +08:00
										 |  |  | func (hs *HTTPServer) configureHttp2() error { | 
					
						
							| 
									
										
										
										
											2021-03-10 19:41:29 +08:00
										 |  |  | 	if hs.Cfg.CertFile == "" { | 
					
						
							| 
									
										
										
										
											2019-08-16 23:06:54 +08:00
										 |  |  | 		return fmt.Errorf("cert_file cannot be empty when using HTTP2") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-10 19:41:29 +08:00
										 |  |  | 	if hs.Cfg.KeyFile == "" { | 
					
						
							| 
									
										
										
										
											2019-08-16 23:06:54 +08:00
										 |  |  | 		return fmt.Errorf("cert_key cannot be empty when using HTTP2") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-10 19:41:29 +08:00
										 |  |  | 	if _, err := os.Stat(hs.Cfg.CertFile); os.IsNotExist(err) { | 
					
						
							|  |  |  | 		return fmt.Errorf(`cannot find SSL cert_file at %q`, hs.Cfg.CertFile) | 
					
						
							| 
									
										
										
										
											2019-08-16 23:06:54 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-10 19:41:29 +08:00
										 |  |  | 	if _, err := os.Stat(hs.Cfg.KeyFile); os.IsNotExist(err) { | 
					
						
							|  |  |  | 		return fmt.Errorf(`cannot find SSL key_file at %q`, hs.Cfg.KeyFile) | 
					
						
							| 
									
										
										
										
											2019-08-16 23:06:54 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tlsCfg := &tls.Config{ | 
					
						
							| 
									
										
										
										
											2022-03-04 00:48:26 +08:00
										 |  |  | 		MinVersion: tls.VersionTLS12, | 
					
						
							| 
									
										
										
										
											2019-08-16 23:06:54 +08:00
										 |  |  | 		CipherSuites: []uint16{ | 
					
						
							|  |  |  | 			tls.TLS_CHACHA20_POLY1305_SHA256, | 
					
						
							|  |  |  | 			tls.TLS_AES_128_GCM_SHA256, | 
					
						
							|  |  |  | 			tls.TLS_AES_256_GCM_SHA384, | 
					
						
							|  |  |  | 			tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | 
					
						
							|  |  |  | 			tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, | 
					
						
							| 
									
										
										
										
											2020-02-19 01:58:36 +08:00
										 |  |  | 			tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | 
					
						
							| 
									
										
										
										
											2019-08-16 23:06:54 +08:00
										 |  |  | 			tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, | 
					
						
							| 
									
										
										
										
											2020-02-19 01:58:36 +08:00
										 |  |  | 			tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, | 
					
						
							|  |  |  | 			tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, | 
					
						
							| 
									
										
										
										
											2019-08-16 23:06:54 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		NextProtos: []string{"h2", "http/1.1"}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	hs.httpSrv.TLSConfig = tlsCfg | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-27 19:07:21 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2019-08-16 23:06:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 14:35:50 +08:00
										 |  |  | func (hs *HTTPServer) applyRoutes() { | 
					
						
							|  |  |  | 	// start with middlewares & static routes
 | 
					
						
							|  |  |  | 	hs.addMiddlewaresAndStaticRoutes() | 
					
						
							|  |  |  | 	// then add view routes & api routes
 | 
					
						
							| 
									
										
										
										
											2022-01-24 21:41:18 +08:00
										 |  |  | 	hs.RouteRegister.Register(hs.web, hs.namedMiddlewares...) | 
					
						
							| 
									
										
										
										
											2018-07-02 14:35:50 +08:00
										 |  |  | 	// then custom app proxy routes
 | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	hs.initAppPluginRoutes(hs.web) | 
					
						
							| 
									
										
										
										
											2018-07-02 14:35:50 +08:00
										 |  |  | 	// lastly not found route
 | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	hs.web.NotFound(middleware.ReqSignedIn, hs.NotFoundHandler) | 
					
						
							| 
									
										
										
										
											2018-07-02 14:35:50 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (hs *HTTPServer) addMiddlewaresAndStaticRoutes() { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	m := hs.web | 
					
						
							| 
									
										
										
										
											2018-07-02 14:35:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-20 18:10:12 +08:00
										 |  |  | 	m.Use(middleware.RequestTracing(hs.tracer)) | 
					
						
							| 
									
										
										
										
											2022-02-03 01:48:46 +08:00
										 |  |  | 	m.Use(middleware.RequestMetrics(hs.Features)) | 
					
						
							| 
									
										
										
										
											2021-04-20 21:22:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 	m.Use(middleware.Logger(hs.Cfg)) | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-10 19:41:29 +08:00
										 |  |  | 	if hs.Cfg.EnableGzip { | 
					
						
							| 
									
										
										
										
											2021-08-10 19:29:46 +08:00
										 |  |  | 		m.UseMiddleware(middleware.Gziper()) | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 	m.Use(middleware.Recovery(hs.Cfg)) | 
					
						
							| 
									
										
										
										
											2022-06-02 21:52:30 +08:00
										 |  |  | 	m.UseMiddleware(hs.Csrf.Middleware(hs.log)) | 
					
						
							| 
									
										
										
										
											2017-11-15 20:51:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-10 19:41:29 +08:00
										 |  |  | 	hs.mapStatic(m, hs.Cfg.StaticRootPath, "build", "public/build") | 
					
						
							| 
									
										
										
										
											2022-02-08 20:38:43 +08:00
										 |  |  | 	hs.mapStatic(m, hs.Cfg.StaticRootPath, "", "public", "/public/views/swagger.html") | 
					
						
							| 
									
										
										
										
											2021-03-10 19:41:29 +08:00
										 |  |  | 	hs.mapStatic(m, hs.Cfg.StaticRootPath, "robots.txt", "robots.txt") | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-10 19:41:29 +08:00
										 |  |  | 	if hs.Cfg.ImageUploadProvider == "local" { | 
					
						
							| 
									
										
										
										
											2018-05-24 21:26:27 +08:00
										 |  |  | 		hs.mapStatic(m, hs.Cfg.ImagesDir, "", "/public/img/attachments") | 
					
						
							| 
									
										
										
										
											2017-12-31 06:01:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 	m.Use(middleware.AddDefaultResponseHeaders(hs.Cfg)) | 
					
						
							| 
									
										
										
										
											2019-05-06 15:22:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-10 19:41:29 +08:00
										 |  |  | 	if hs.Cfg.ServeFromSubPath && hs.Cfg.AppSubURL != "" { | 
					
						
							|  |  |  | 		m.SetURLPrefix(hs.Cfg.AppSubURL) | 
					
						
							| 
									
										
										
										
											2019-05-27 23:47:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	m.UseMiddleware(web.Renderer(filepath.Join(hs.Cfg.StaticRootPath, "views"), "[[", "]]")) | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-02 13:14:26 +08:00
										 |  |  | 	// These endpoints are used for monitoring the Grafana instance
 | 
					
						
							| 
									
										
										
										
											2020-10-21 17:06:19 +08:00
										 |  |  | 	// and should not be redirected or rejected.
 | 
					
						
							| 
									
										
										
										
											2020-10-02 13:14:26 +08:00
										 |  |  | 	m.Use(hs.healthzHandler) | 
					
						
							|  |  |  | 	m.Use(hs.apiHealthHandler) | 
					
						
							| 
									
										
										
										
											2017-09-07 04:24:10 +08:00
										 |  |  | 	m.Use(hs.metricsEndpoint) | 
					
						
							| 
									
										
										
										
											2022-03-29 17:18:26 +08:00
										 |  |  | 	m.Use(hs.pluginMetricsEndpoint) | 
					
						
							| 
									
										
										
										
											2020-10-02 13:14:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 	m.Use(hs.ContextHandler.Middleware) | 
					
						
							| 
									
										
										
										
											2022-04-08 16:33:19 +08:00
										 |  |  | 	m.Use(middleware.OrgRedirect(hs.Cfg, hs.SQLStore)) | 
					
						
							| 
									
										
										
										
											2022-04-28 16:46:18 +08:00
										 |  |  | 	m.Use(accesscontrol.LoadPermissionsMiddleware(hs.AccessControl)) | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// needs to be after context handler
 | 
					
						
							| 
									
										
										
										
											2021-03-10 19:41:29 +08:00
										 |  |  | 	if hs.Cfg.EnforceDomain { | 
					
						
							| 
									
										
										
										
											2020-12-16 02:09:04 +08:00
										 |  |  | 		m.Use(middleware.ValidateHostHeader(hs.Cfg)) | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 14:42:32 +08:00
										 |  |  | 	m.Use(middleware.HandleNoCacheHeader) | 
					
						
							| 
									
										
										
										
											2021-08-10 15:03:22 +08:00
										 |  |  | 	m.UseMiddleware(middleware.AddCSPHeader(hs.Cfg, hs.log)) | 
					
						
							| 
									
										
										
										
											2020-07-23 15:54:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for _, mw := range hs.middlewares { | 
					
						
							|  |  |  | 		m.Use(mw) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | func (hs *HTTPServer) metricsEndpoint(ctx *web.Context) { | 
					
						
							| 
									
										
										
										
											2018-09-13 20:36:16 +08:00
										 |  |  | 	if !hs.Cfg.MetricsEndpointEnabled { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-12 13:27:47 +08:00
										 |  |  | 	if ctx.Req.Method != http.MethodGet || ctx.Req.URL.Path != "/metrics" { | 
					
						
							| 
									
										
										
										
											2017-09-07 04:24:10 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 02:15:18 +08:00
										 |  |  | 	if hs.metricsEndpointBasicAuthEnabled() && !BasicAuthenticatedRequest(ctx.Req, hs.Cfg.MetricsEndpointBasicAuthUsername, hs.Cfg.MetricsEndpointBasicAuthPassword) { | 
					
						
							| 
									
										
										
										
											2018-11-15 06:37:32 +08:00
										 |  |  | 		ctx.Resp.WriteHeader(http.StatusUnauthorized) | 
					
						
							|  |  |  | 		return | 
					
						
							| 
									
										
										
										
											2018-11-15 04:42:47 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 13:33:48 +08:00
										 |  |  | 	promhttp. | 
					
						
							| 
									
										
										
										
											2020-12-01 22:04:59 +08:00
										 |  |  | 		HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{EnableOpenMetrics: true}). | 
					
						
							| 
									
										
										
										
											2021-09-01 17:18:30 +08:00
										 |  |  | 		ServeHTTP(ctx.Resp, ctx.Req) | 
					
						
							| 
									
										
										
										
											2017-09-07 04:24:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-18 19:03:18 +08:00
										 |  |  | // healthzHandler always return 200 - Ok if Grafana's web server is running
 | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | func (hs *HTTPServer) healthzHandler(ctx *web.Context) { | 
					
						
							| 
									
										
										
										
											2020-10-02 13:14:26 +08:00
										 |  |  | 	notHeadOrGet := ctx.Req.Method != http.MethodGet && ctx.Req.Method != http.MethodHead | 
					
						
							|  |  |  | 	if notHeadOrGet || ctx.Req.URL.Path != "/healthz" { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-10 19:29:46 +08:00
										 |  |  | 	ctx.Resp.WriteHeader(200) | 
					
						
							| 
									
										
										
										
											2020-09-18 19:03:18 +08:00
										 |  |  | 	_, err := ctx.Resp.Write([]byte("Ok")) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		hs.log.Error("could not write to response", "err", err) | 
					
						
							| 
									
										
										
										
											2017-04-25 23:17:45 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-09-18 19:03:18 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-04-25 23:17:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-18 19:03:18 +08:00
										 |  |  | // apiHealthHandler will return ok if Grafana's web server is running and it
 | 
					
						
							| 
									
										
										
										
											2020-10-21 17:06:19 +08:00
										 |  |  | // can access the database. If the database cannot be accessed it will return
 | 
					
						
							| 
									
										
										
										
											2020-09-18 19:03:18 +08:00
										 |  |  | // http status code 503.
 | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | func (hs *HTTPServer) apiHealthHandler(ctx *web.Context) { | 
					
						
							| 
									
										
										
										
											2020-10-02 13:14:26 +08:00
										 |  |  | 	notHeadOrGet := ctx.Req.Method != http.MethodGet && ctx.Req.Method != http.MethodHead | 
					
						
							|  |  |  | 	if notHeadOrGet || ctx.Req.URL.Path != "/api/health" { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-25 23:17:45 +08:00
										 |  |  | 	data := simplejson.New() | 
					
						
							| 
									
										
										
										
											2017-04-25 23:23:38 +08:00
										 |  |  | 	data.Set("database", "ok") | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | 	if !hs.Cfg.AnonymousHideVersion { | 
					
						
							| 
									
										
										
										
											2021-03-10 19:41:29 +08:00
										 |  |  | 		data.Set("version", hs.Cfg.BuildVersion) | 
					
						
							|  |  |  | 		data.Set("commit", hs.Cfg.BuildCommit) | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-25 23:17:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 20:35:03 +08:00
										 |  |  | 	if !hs.databaseHealthy(ctx.Req.Context()) { | 
					
						
							| 
									
										
										
										
											2017-04-25 23:24:36 +08:00
										 |  |  | 		data.Set("database", "failing") | 
					
						
							| 
									
										
										
										
											2017-05-10 21:23:59 +08:00
										 |  |  | 		ctx.Resp.Header().Set("Content-Type", "application/json; charset=UTF-8") | 
					
						
							|  |  |  | 		ctx.Resp.WriteHeader(503) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		ctx.Resp.Header().Set("Content-Type", "application/json; charset=UTF-8") | 
					
						
							|  |  |  | 		ctx.Resp.WriteHeader(200) | 
					
						
							| 
									
										
										
										
											2017-04-25 23:17:45 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-18 20:58:08 +08:00
										 |  |  | 	dataBytes, err := data.EncodePretty() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		hs.log.Error("Failed to encode data", "err", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-09 00:57:53 +08:00
										 |  |  | 	if _, err := ctx.Resp.Write(dataBytes); err != nil { | 
					
						
							|  |  |  | 		hs.log.Error("Failed to write to response", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-25 23:17:45 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-08 20:38:43 +08:00
										 |  |  | func (hs *HTTPServer) mapStatic(m *web.Mux, rootDir string, dir string, prefix string, exclude ...string) { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	headers := func(c *web.Context) { | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 		c.Resp.Header().Set("Cache-Control", "public, max-age=3600") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-08 19:54:00 +08:00
										 |  |  | 	if prefix == "public/build" { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 		headers = func(c *web.Context) { | 
					
						
							| 
									
										
										
										
											2018-05-08 19:54:00 +08:00
										 |  |  | 			c.Resp.Header().Set("Cache-Control", "public, max-age=31536000") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 	if hs.Cfg.Env == setting.Dev { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 		headers = func(c *web.Context) { | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 			c.Resp.Header().Set("Cache-Control", "max-age=0, must-revalidate, no-cache") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m.Use(httpstatic.Static( | 
					
						
							|  |  |  | 		path.Join(rootDir, dir), | 
					
						
							|  |  |  | 		httpstatic.StaticOptions{ | 
					
						
							|  |  |  | 			SkipLogging: true, | 
					
						
							|  |  |  | 			Prefix:      prefix, | 
					
						
							|  |  |  | 			AddHeaders:  headers, | 
					
						
							| 
									
										
										
										
											2022-02-08 20:38:43 +08:00
										 |  |  | 			Exclude:     exclude, | 
					
						
							| 
									
										
										
										
											2016-12-21 21:36:32 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	)) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-11-20 02:15:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (hs *HTTPServer) metricsEndpointBasicAuthEnabled() bool { | 
					
						
							|  |  |  | 	return hs.Cfg.MetricsEndpointBasicAuthUsername != "" && hs.Cfg.MetricsEndpointBasicAuthPassword != "" | 
					
						
							|  |  |  | } |