2016-12-21 07:36:32 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-02-15 07:29:20 -06:00
|
|
|
"crypto/tls"
|
2016-12-21 07:36:32 -06:00
|
|
|
"fmt"
|
2017-04-27 01:54:21 -05:00
|
|
|
"net"
|
2016-12-21 07:36:32 -06:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
2019-11-27 05:07:21 -06:00
|
|
|
"sync"
|
2016-12-21 07:36:32 -06:00
|
|
|
|
2020-07-27 02:26:16 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/live"
|
2020-06-17 00:39:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/search"
|
|
|
|
|
2020-01-31 04:15:50 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
|
|
|
|
2019-01-16 07:53:59 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
2016-12-21 07:36:32 -06:00
|
|
|
httpstatic "github.com/grafana/grafana/pkg/api/static"
|
2017-04-25 10:17:45 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2019-06-13 03:55:38 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/localcache"
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2019-04-08 06:31:46 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/remotecache"
|
2016-12-21 07:36:32 -06:00
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
2017-04-25 10:17:45 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-12-21 07:36:32 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2018-05-01 08:51:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/registry"
|
2018-10-26 03:40:33 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
2018-10-12 04:26:42 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/hooks"
|
2019-06-13 09:47:52 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/login"
|
2020-03-25 08:14:24 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/provisioning"
|
2019-02-11 14:12:01 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/quota"
|
2018-05-24 08:26:27 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/rendering"
|
2016-12-21 07:36:32 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2019-11-18 02:49:08 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util/errutil"
|
2019-01-16 07:53:59 -06:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2019-05-27 10:47:29 -05:00
|
|
|
macaron "gopkg.in/macaron.v1"
|
2016-12-21 07:36:32 -06:00
|
|
|
)
|
|
|
|
|
2018-05-01 08:51:15 -05:00
|
|
|
func init() {
|
2018-07-01 09:01:43 -05:00
|
|
|
registry.Register(®istry.Descriptor{
|
|
|
|
Name: "HTTPServer",
|
|
|
|
Instance: &HTTPServer{},
|
|
|
|
InitPriority: registry.High,
|
|
|
|
})
|
2018-05-01 08:51:15 -05:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
type HTTPServer struct {
|
2020-07-27 02:26:16 -05:00
|
|
|
log log.Logger
|
|
|
|
macaron *macaron.Macaron
|
|
|
|
context context.Context
|
|
|
|
httpSrv *http.Server
|
|
|
|
middlewares []macaron.Handler
|
2017-02-06 02:40:07 -06:00
|
|
|
|
2020-03-25 08:14:24 -05:00
|
|
|
RouteRegister routing.RouteRegister `inject:""`
|
|
|
|
Bus bus.Bus `inject:""`
|
|
|
|
RenderService rendering.Service `inject:""`
|
|
|
|
Cfg *setting.Cfg `inject:""`
|
|
|
|
HooksService *hooks.HooksService `inject:""`
|
|
|
|
CacheService *localcache.CacheService `inject:""`
|
|
|
|
DatasourceCache datasources.CacheService `inject:""`
|
|
|
|
AuthTokenService models.UserTokenService `inject:""`
|
|
|
|
QuotaService *quota.QuotaService `inject:""`
|
|
|
|
RemoteCacheService *remotecache.RemoteCache `inject:""`
|
|
|
|
ProvisioningService provisioning.ProvisioningService `inject:""`
|
|
|
|
Login *login.LoginService `inject:""`
|
|
|
|
License models.Licensing `inject:""`
|
|
|
|
BackendPluginManager backendplugin.Manager `inject:""`
|
|
|
|
PluginManager *plugins.PluginManager `inject:""`
|
2020-04-20 09:20:45 -05:00
|
|
|
SearchService *search.SearchService `inject:""`
|
2020-07-27 02:26:16 -05:00
|
|
|
Live *live.GrafanaLive
|
2020-08-21 03:41:10 -05:00
|
|
|
Listener net.Listener
|
2016-12-21 07:36:32 -06:00
|
|
|
}
|
|
|
|
|
2018-05-01 08:51:15 -05:00
|
|
|
func (hs *HTTPServer) Init() error {
|
2018-04-27 06:41:58 -05:00
|
|
|
hs.log = log.New("http.server")
|
2018-05-01 08:51:15 -05:00
|
|
|
|
2020-07-27 02:26:16 -05:00
|
|
|
// Set up a websocket broker
|
|
|
|
if hs.Cfg.IsLiveEnabled() { // feature flag
|
|
|
|
node, err := live.InitalizeBroker()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hs.Live = node
|
|
|
|
|
|
|
|
// Spit random walk to example
|
|
|
|
go live.RunRandomCSV(hs.Live, "random-2s-stream", 2000, 0)
|
|
|
|
go live.RunRandomCSV(hs.Live, "random-flakey-stream", 400, .6)
|
|
|
|
}
|
|
|
|
|
2018-07-01 09:01:43 -05:00
|
|
|
hs.macaron = hs.newMacaron()
|
|
|
|
hs.registerRoutes()
|
|
|
|
|
2018-05-01 08:51:15 -05:00
|
|
|
return nil
|
2016-12-21 07:36:32 -06:00
|
|
|
}
|
|
|
|
|
2020-07-23 02:54:07 -05:00
|
|
|
func (hs *HTTPServer) AddMiddleware(middleware macaron.Handler) {
|
|
|
|
hs.middlewares = append(hs.middlewares, middleware)
|
|
|
|
}
|
|
|
|
|
2018-05-01 08:51:15 -05:00
|
|
|
func (hs *HTTPServer) Run(ctx context.Context) error {
|
2016-12-21 07:36:32 -06:00
|
|
|
hs.context = ctx
|
2018-07-02 01:35:50 -05:00
|
|
|
|
2018-07-01 09:01:43 -05:00
|
|
|
hs.applyRoutes()
|
2016-12-21 07:36:32 -06:00
|
|
|
|
2019-11-27 05:07:21 -06:00
|
|
|
hs.httpSrv = &http.Server{
|
|
|
|
Addr: fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort),
|
|
|
|
Handler: hs.macaron,
|
2019-10-22 06:07:43 -05:00
|
|
|
}
|
2016-12-21 07:36:32 -06:00
|
|
|
switch setting.Protocol {
|
2019-08-16 10:06:54 -05:00
|
|
|
case setting.HTTP2:
|
2019-11-27 05:07:21 -06:00
|
|
|
if err := hs.configureHttp2(); err != nil {
|
|
|
|
return err
|
2019-08-16 10:06:54 -05:00
|
|
|
}
|
2016-12-21 07:36:32 -06:00
|
|
|
case setting.HTTPS:
|
2019-11-27 05:07:21 -06:00
|
|
|
if err := hs.configureHttps(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-21 03:41:10 -05:00
|
|
|
listener, err := hs.getListener()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-11-27 05:07:21 -06:00
|
|
|
}
|
2018-03-22 08:14:44 -05:00
|
|
|
|
2019-11-27 05:07:21 -06:00
|
|
|
hs.log.Info("HTTP Server Listen", "address", listener.Addr().String(), "protocol",
|
|
|
|
setting.Protocol, "subUrl", setting.AppSubUrl, "socket", setting.SocketPath)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
// handle http shutdown on server context done
|
|
|
|
go func() {
|
2019-11-27 06:42:15 -06:00
|
|
|
defer wg.Done()
|
|
|
|
|
2019-11-27 05:07:21 -06:00
|
|
|
<-ctx.Done()
|
|
|
|
if err := hs.httpSrv.Shutdown(context.Background()); err != nil {
|
|
|
|
hs.log.Error("Failed to shutdown server", "error", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
switch setting.Protocol {
|
|
|
|
case setting.HTTP, setting.SOCKET:
|
|
|
|
if err := hs.httpSrv.Serve(listener); err != nil {
|
|
|
|
if err == http.ErrServerClosed {
|
|
|
|
hs.log.Debug("server was shutdown gracefully")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case setting.HTTP2, setting.HTTPS:
|
|
|
|
if err := hs.httpSrv.ServeTLS(listener, setting.CertFile, setting.KeyFile); err != nil {
|
|
|
|
if err == http.ErrServerClosed {
|
|
|
|
hs.log.Debug("server was shutdown gracefully")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2017-04-27 01:54:21 -05:00
|
|
|
}
|
2016-12-21 07:36:32 -06:00
|
|
|
default:
|
2019-11-27 05:07:21 -06:00
|
|
|
panic(fmt.Sprintf("Unhandled protocol %q", setting.Protocol))
|
2016-12-21 07:36:32 -06:00
|
|
|
}
|
|
|
|
|
2019-11-27 05:07:21 -06:00
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
2016-12-21 07:36:32 -06:00
|
|
|
}
|
|
|
|
|
2020-08-21 03:41:10 -05:00
|
|
|
func (hs *HTTPServer) getListener() (net.Listener, error) {
|
|
|
|
if hs.Listener != nil {
|
|
|
|
return hs.Listener, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch setting.Protocol {
|
|
|
|
case setting.HTTP, setting.HTTPS, setting.HTTP2:
|
|
|
|
listener, err := net.Listen("tcp", hs.httpSrv.Addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errutil.Wrapf(err, "failed to open listener on address %s", hs.httpSrv.Addr)
|
|
|
|
}
|
|
|
|
return listener, nil
|
|
|
|
case setting.SOCKET:
|
|
|
|
listener, err := net.ListenUnix("unix", &net.UnixAddr{Name: setting.SocketPath, Net: "unix"})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errutil.Wrapf(err, "failed to open listener for socket %s", setting.SocketPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make socket writable by group
|
|
|
|
if err := os.Chmod(setting.SocketPath, 0660); err != nil {
|
|
|
|
return nil, errutil.Wrapf(err, "failed to change socket permissions")
|
|
|
|
}
|
|
|
|
|
|
|
|
return listener, nil
|
|
|
|
default:
|
|
|
|
hs.log.Error("Invalid protocol", "protocol", setting.Protocol)
|
|
|
|
return nil, fmt.Errorf("invalid protocol %q", setting.Protocol)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 05:07:21 -06:00
|
|
|
func (hs *HTTPServer) configureHttps() error {
|
|
|
|
if setting.CertFile == "" {
|
2016-12-21 07:36:32 -06:00
|
|
|
return fmt.Errorf("cert_file cannot be empty when using HTTPS")
|
|
|
|
}
|
|
|
|
|
2019-11-27 05:07:21 -06:00
|
|
|
if setting.KeyFile == "" {
|
2016-12-21 07:36:32 -06:00
|
|
|
return fmt.Errorf("cert_key cannot be empty when using HTTPS")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(setting.CertFile); os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf(`Cannot find SSL cert_file at %v`, setting.CertFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(setting.KeyFile); os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf(`Cannot find SSL key_file at %v`, setting.KeyFile)
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:29:20 -06:00
|
|
|
tlsCfg := &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
PreferServerCipherSuites: true,
|
|
|
|
CipherSuites: []uint16{
|
2020-02-18 11:58:36 -06: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 07:29:20 -06:00
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
|
2020-02-18 11:58:36 -06:00
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
|
2017-02-15 07:29:20 -06:00
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
|
2020-02-18 11:58:36 -06: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 07:29:20 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-06-17 16:10:12 -05:00
|
|
|
hs.httpSrv.TLSConfig = tlsCfg
|
2018-04-16 13:22:12 -05:00
|
|
|
hs.httpSrv.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
|
2017-06-17 16:10:12 -05:00
|
|
|
|
2019-11-27 05:07:21 -06:00
|
|
|
return nil
|
2016-12-21 07:36:32 -06:00
|
|
|
}
|
|
|
|
|
2019-11-27 05:07:21 -06:00
|
|
|
func (hs *HTTPServer) configureHttp2() error {
|
|
|
|
if setting.CertFile == "" {
|
2019-08-16 10:06:54 -05:00
|
|
|
return fmt.Errorf("cert_file cannot be empty when using HTTP2")
|
|
|
|
}
|
|
|
|
|
2019-11-27 05:07:21 -06:00
|
|
|
if setting.KeyFile == "" {
|
2019-08-16 10:06:54 -05:00
|
|
|
return fmt.Errorf("cert_key cannot be empty when using HTTP2")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(setting.CertFile); os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf(`Cannot find SSL cert_file at %v`, setting.CertFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(setting.KeyFile); os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf(`Cannot find SSL key_file at %v`, setting.KeyFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsCfg := &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
PreferServerCipherSuites: false,
|
|
|
|
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-18 11:58:36 -06:00
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
2019-08-16 10:06:54 -05:00
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
2020-02-18 11:58:36 -06:00
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
|
2019-08-16 10:06:54 -05:00
|
|
|
},
|
|
|
|
NextProtos: []string{"h2", "http/1.1"},
|
|
|
|
}
|
|
|
|
|
|
|
|
hs.httpSrv.TLSConfig = tlsCfg
|
|
|
|
|
2019-11-27 05:07:21 -06:00
|
|
|
return nil
|
2019-08-16 10:06:54 -05:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
func (hs *HTTPServer) newMacaron() *macaron.Macaron {
|
2016-12-21 07:36:32 -06:00
|
|
|
macaron.Env = setting.Env
|
|
|
|
m := macaron.New()
|
|
|
|
|
2018-07-02 01:35:50 -05:00
|
|
|
// automatically set HEAD for every GET
|
|
|
|
m.SetAutoHead(true)
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *HTTPServer) applyRoutes() {
|
|
|
|
// start with middlewares & static routes
|
|
|
|
hs.addMiddlewaresAndStaticRoutes()
|
|
|
|
// then add view routes & api routes
|
|
|
|
hs.RouteRegister.Register(hs.macaron)
|
|
|
|
// then custom app proxy routes
|
|
|
|
hs.initAppPluginRoutes(hs.macaron)
|
|
|
|
// lastly not found route
|
2018-10-12 04:26:42 -05:00
|
|
|
hs.macaron.NotFound(hs.NotFoundHandler)
|
2018-07-02 01:35:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *HTTPServer) addMiddlewaresAndStaticRoutes() {
|
|
|
|
m := hs.macaron
|
|
|
|
|
2016-12-21 07:36:32 -06:00
|
|
|
m.Use(middleware.Logger())
|
|
|
|
|
|
|
|
if setting.EnableGzip {
|
|
|
|
m.Use(middleware.Gziper())
|
|
|
|
}
|
|
|
|
|
2017-11-15 06:51:15 -06:00
|
|
|
m.Use(middleware.Recovery())
|
|
|
|
|
2016-12-21 07:36:32 -06:00
|
|
|
for _, route := range plugins.StaticRoutes {
|
|
|
|
pluginRoute := path.Join("/public/plugins/", route.PluginId)
|
2017-10-12 08:29:01 -05:00
|
|
|
hs.log.Debug("Plugins: Adding route", "route", pluginRoute, "dir", route.Directory)
|
2019-11-29 09:34:20 -06:00
|
|
|
hs.mapStatic(m, route.Directory, "", pluginRoute)
|
2016-12-21 07:36:32 -06:00
|
|
|
}
|
|
|
|
|
2018-05-08 06:54:00 -05:00
|
|
|
hs.mapStatic(m, setting.StaticRootPath, "build", "public/build")
|
2016-12-21 07:36:32 -06:00
|
|
|
hs.mapStatic(m, setting.StaticRootPath, "", "public")
|
|
|
|
hs.mapStatic(m, setting.StaticRootPath, "robots.txt", "robots.txt")
|
|
|
|
|
2017-12-30 16:01:51 -06:00
|
|
|
if setting.ImageUploadProvider == "local" {
|
2018-05-24 08:26:27 -05:00
|
|
|
hs.mapStatic(m, hs.Cfg.ImagesDir, "", "/public/img/attachments")
|
2017-12-30 16:01:51 -06:00
|
|
|
}
|
|
|
|
|
2019-05-06 02:22:59 -05:00
|
|
|
m.Use(middleware.AddDefaultResponseHeaders())
|
|
|
|
|
2019-05-27 10:47:29 -05:00
|
|
|
if setting.ServeFromSubPath && setting.AppSubUrl != "" {
|
|
|
|
m.SetURLPrefix(setting.AppSubUrl)
|
|
|
|
}
|
|
|
|
|
2016-12-21 07:36:32 -06:00
|
|
|
m.Use(macaron.Renderer(macaron.RenderOptions{
|
|
|
|
Directory: path.Join(setting.StaticRootPath, "views"),
|
|
|
|
IndentJSON: macaron.Env != macaron.PROD,
|
|
|
|
Delims: macaron.Delims{Left: "[[", Right: "]]"},
|
|
|
|
}))
|
|
|
|
|
2017-09-06 15:24:10 -05:00
|
|
|
m.Use(hs.metricsEndpoint)
|
2019-04-08 06:31:46 -05:00
|
|
|
m.Use(middleware.GetContextHandler(
|
|
|
|
hs.AuthTokenService,
|
|
|
|
hs.RemoteCacheService,
|
2020-02-19 12:47:39 -06:00
|
|
|
hs.RenderService,
|
2019-04-08 06:31:46 -05:00
|
|
|
))
|
2017-02-17 08:02:14 -06:00
|
|
|
m.Use(middleware.OrgRedirect())
|
2016-12-21 07:36:32 -06:00
|
|
|
|
|
|
|
// needs to be after context handler
|
|
|
|
if setting.EnforceDomain {
|
|
|
|
m.Use(middleware.ValidateHostHeader(setting.Domain))
|
|
|
|
}
|
|
|
|
|
2018-10-26 03:40:33 -05:00
|
|
|
m.Use(middleware.HandleNoCacheHeader())
|
2020-07-23 02:54:07 -05:00
|
|
|
|
|
|
|
for _, mw := range hs.middlewares {
|
|
|
|
m.Use(mw)
|
|
|
|
}
|
2016-12-21 07:36:32 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
func (hs *HTTPServer) metricsEndpoint(ctx *macaron.Context) {
|
2018-09-13 07:36:16 -05:00
|
|
|
if !hs.Cfg.MetricsEndpointEnabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-12 00:27:47 -05:00
|
|
|
if ctx.Req.Method != http.MethodGet || ctx.Req.URL.Path != "/metrics" {
|
2017-09-06 15:24:10 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-19 12:15:18 -06:00
|
|
|
if hs.metricsEndpointBasicAuthEnabled() && !BasicAuthenticatedRequest(ctx.Req, hs.Cfg.MetricsEndpointBasicAuthUsername, hs.Cfg.MetricsEndpointBasicAuthPassword) {
|
2018-11-14 16:37:32 -06:00
|
|
|
ctx.Resp.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
2018-11-14 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2019-07-15 00:33:48 -05:00
|
|
|
promhttp.
|
|
|
|
HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}).
|
2017-10-23 02:35:46 -05:00
|
|
|
ServeHTTP(ctx.Resp, ctx.Req.Request)
|
2017-09-06 15:24:10 -05:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
func (hs *HTTPServer) healthHandler(ctx *macaron.Context) {
|
2017-11-21 08:01:59 -06:00
|
|
|
notHeadOrGet := ctx.Req.Method != http.MethodGet && ctx.Req.Method != http.MethodHead
|
|
|
|
if notHeadOrGet || ctx.Req.URL.Path != "/api/health" {
|
2017-04-25 10:17:45 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data := simplejson.New()
|
2017-04-25 10:23:38 -05:00
|
|
|
data.Set("database", "ok")
|
2020-06-17 00:39:50 -05:00
|
|
|
if !hs.Cfg.AnonymousHideVersion {
|
|
|
|
data.Set("version", setting.BuildVersion)
|
|
|
|
data.Set("commit", setting.BuildCommit)
|
|
|
|
}
|
2017-04-25 10:17:45 -05:00
|
|
|
|
|
|
|
if err := bus.Dispatch(&models.GetDBHealthQuery{}); err != nil {
|
2017-04-25 10:24:36 -05:00
|
|
|
data.Set("database", "failing")
|
2017-05-10 08:23:59 -05: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 10:17:45 -05:00
|
|
|
}
|
|
|
|
|
2020-08-18 07:58:08 -05:00
|
|
|
dataBytes, err := data.EncodePretty()
|
|
|
|
if err != nil {
|
|
|
|
hs.log.Error("Failed to encode data", "err", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-08 11:57:53 -05:00
|
|
|
if _, err := ctx.Resp.Write(dataBytes); err != nil {
|
|
|
|
hs.log.Error("Failed to write to response", "err", err)
|
|
|
|
}
|
2017-04-25 10:17:45 -05:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
func (hs *HTTPServer) mapStatic(m *macaron.Macaron, rootDir string, dir string, prefix string) {
|
2016-12-21 07:36:32 -06:00
|
|
|
headers := func(c *macaron.Context) {
|
|
|
|
c.Resp.Header().Set("Cache-Control", "public, max-age=3600")
|
|
|
|
}
|
|
|
|
|
2018-05-08 06:54:00 -05:00
|
|
|
if prefix == "public/build" {
|
|
|
|
headers = func(c *macaron.Context) {
|
|
|
|
c.Resp.Header().Set("Cache-Control", "public, max-age=31536000")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 07:36:32 -06:00
|
|
|
if setting.Env == setting.DEV {
|
|
|
|
headers = func(c *macaron.Context) {
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
2018-11-19 12:15:18 -06:00
|
|
|
|
|
|
|
func (hs *HTTPServer) metricsEndpointBasicAuthEnabled() bool {
|
|
|
|
return hs.Cfg.MetricsEndpointBasicAuthUsername != "" && hs.Cfg.MetricsEndpointBasicAuthPassword != ""
|
|
|
|
}
|