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
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-04-27 01:54:21 -05:00
|
|
|
"net"
|
2016-12-21 07:36:32 -06:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
2017-08-23 06:31:26 -05:00
|
|
|
"time"
|
2016-12-21 07:36:32 -06:00
|
|
|
|
2017-10-12 05:02:36 -05:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
|
2017-09-06 15:24:10 -05:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
|
2017-08-23 06:31:26 -05:00
|
|
|
gocache "github.com/patrickmn/go-cache"
|
2016-12-21 07:36:32 -06:00
|
|
|
macaron "gopkg.in/macaron.v1"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/live"
|
|
|
|
httpstatic "github.com/grafana/grafana/pkg/api/static"
|
2017-04-25 10:17:45 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2016-12-21 07:36:32 -06:00
|
|
|
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
|
2017-04-25 10:17:45 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2016-12-21 07:36:32 -06:00
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
|
"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"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HttpServer struct {
|
|
|
|
log log.Logger
|
|
|
|
macaron *macaron.Macaron
|
|
|
|
context context.Context
|
|
|
|
streamManager *live.StreamManager
|
2017-08-23 06:31:26 -05:00
|
|
|
cache *gocache.Cache
|
2017-02-06 02:40:07 -06:00
|
|
|
|
|
|
|
httpSrv *http.Server
|
2016-12-21 07:36:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHttpServer() *HttpServer {
|
|
|
|
return &HttpServer{
|
2017-08-23 06:31:26 -05:00
|
|
|
log: log.New("http.server"),
|
|
|
|
cache: gocache.New(5*time.Minute, 10*time.Minute),
|
2016-12-21 07:36:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *HttpServer) Start(ctx context.Context) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
hs.context = ctx
|
|
|
|
hs.streamManager = live.NewStreamManager()
|
|
|
|
hs.macaron = hs.newMacaron()
|
|
|
|
hs.registerRoutes()
|
|
|
|
|
|
|
|
hs.streamManager.Run(ctx)
|
|
|
|
|
|
|
|
listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort)
|
2017-04-27 01:54:21 -05:00
|
|
|
hs.log.Info("Initializing HTTP Server", "address", listenAddr, "protocol", setting.Protocol, "subUrl", setting.AppSubUrl, "socket", setting.SocketPath)
|
2016-12-21 07:36:32 -06:00
|
|
|
|
2017-02-06 02:40:07 -06:00
|
|
|
hs.httpSrv = &http.Server{Addr: listenAddr, Handler: hs.macaron}
|
2016-12-21 07:36:32 -06:00
|
|
|
switch setting.Protocol {
|
|
|
|
case setting.HTTP:
|
2017-02-06 02:40:07 -06:00
|
|
|
err = hs.httpSrv.ListenAndServe()
|
|
|
|
if err == http.ErrServerClosed {
|
|
|
|
hs.log.Debug("server was shutdown gracefully")
|
|
|
|
return nil
|
|
|
|
}
|
2016-12-21 07:36:32 -06:00
|
|
|
case setting.HTTPS:
|
2017-06-17 16:10:12 -05:00
|
|
|
err = hs.listenAndServeTLS(setting.CertFile, setting.KeyFile)
|
2017-02-06 02:40:07 -06:00
|
|
|
if err == http.ErrServerClosed {
|
|
|
|
hs.log.Debug("server was shutdown gracefully")
|
|
|
|
return nil
|
|
|
|
}
|
2017-04-27 01:54:21 -05:00
|
|
|
case setting.SOCKET:
|
|
|
|
ln, err := net.Listen("unix", setting.SocketPath)
|
|
|
|
if err != nil {
|
|
|
|
hs.log.Debug("server was shutdown gracefully")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = hs.httpSrv.Serve(ln)
|
|
|
|
if err != nil {
|
|
|
|
hs.log.Debug("server was shutdown gracefully")
|
|
|
|
return nil
|
|
|
|
}
|
2016-12-21 07:36:32 -06:00
|
|
|
default:
|
|
|
|
hs.log.Error("Invalid protocol", "protocol", setting.Protocol)
|
|
|
|
err = errors.New("Invalid Protocol")
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-06 02:40:07 -06:00
|
|
|
func (hs *HttpServer) Shutdown(ctx context.Context) error {
|
|
|
|
err := hs.httpSrv.Shutdown(ctx)
|
|
|
|
hs.log.Info("stopped http server")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-17 16:10:12 -05:00
|
|
|
func (hs *HttpServer) listenAndServeTLS(certfile, keyfile string) error {
|
2016-12-21 07:36:32 -06:00
|
|
|
if certfile == "" {
|
|
|
|
return fmt.Errorf("cert_file cannot be empty when using HTTPS")
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyfile == "" {
|
|
|
|
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{
|
|
|
|
tls.TLS_RSA_WITH_AES_128_CBC_SHA,
|
|
|
|
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
|
|
|
|
tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-06-17 16:10:12 -05:00
|
|
|
hs.httpSrv.TLSConfig = tlsCfg
|
|
|
|
hs.httpSrv.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler), 0)
|
|
|
|
|
|
|
|
return hs.httpSrv.ListenAndServeTLS(setting.CertFile, setting.KeyFile)
|
2016-12-21 07:36:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *HttpServer) newMacaron() *macaron.Macaron {
|
|
|
|
macaron.Env = setting.Env
|
|
|
|
m := macaron.New()
|
|
|
|
|
|
|
|
m.Use(middleware.Logger())
|
|
|
|
m.Use(middleware.Recovery())
|
|
|
|
|
|
|
|
if setting.EnableGzip {
|
|
|
|
m.Use(middleware.Gziper())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, route := range plugins.StaticRoutes {
|
|
|
|
pluginRoute := path.Join("/public/plugins/", route.PluginId)
|
|
|
|
logger.Debug("Plugins: Adding route", "route", pluginRoute, "dir", route.Directory)
|
|
|
|
hs.mapStatic(m, route.Directory, "", pluginRoute)
|
|
|
|
}
|
|
|
|
|
|
|
|
hs.mapStatic(m, setting.StaticRootPath, "", "public")
|
|
|
|
hs.mapStatic(m, setting.StaticRootPath, "robots.txt", "robots.txt")
|
|
|
|
|
|
|
|
m.Use(macaron.Renderer(macaron.RenderOptions{
|
|
|
|
Directory: path.Join(setting.StaticRootPath, "views"),
|
|
|
|
IndentJSON: macaron.Env != macaron.PROD,
|
|
|
|
Delims: macaron.Delims{Left: "[[", Right: "]]"},
|
|
|
|
}))
|
|
|
|
|
2017-04-25 10:17:45 -05:00
|
|
|
m.Use(hs.healthHandler)
|
2017-09-06 15:24:10 -05:00
|
|
|
m.Use(hs.metricsEndpoint)
|
2016-12-21 07:36:32 -06:00
|
|
|
m.Use(middleware.GetContextHandler())
|
|
|
|
m.Use(middleware.Sessioner(&setting.SessionOptions))
|
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))
|
|
|
|
}
|
|
|
|
|
2017-07-04 09:33:37 -05:00
|
|
|
m.Use(middleware.AddDefaultResponseHeaders())
|
|
|
|
|
2016-12-21 07:36:32 -06:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2017-09-06 15:24:10 -05:00
|
|
|
func (hs *HttpServer) metricsEndpoint(ctx *macaron.Context) {
|
|
|
|
if ctx.Req.Method != "GET" || ctx.Req.URL.Path != "/metrics" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-12 05:02:36 -05:00
|
|
|
promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{
|
|
|
|
DisableCompression: true,
|
|
|
|
}).ServeHTTP(ctx.Resp, ctx.Req.Request)
|
2017-09-06 15:24:10 -05:00
|
|
|
}
|
|
|
|
|
2017-04-25 10:17:45 -05:00
|
|
|
func (hs *HttpServer) healthHandler(ctx *macaron.Context) {
|
|
|
|
if ctx.Req.Method != "GET" || ctx.Req.URL.Path != "/api/health" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data := simplejson.New()
|
2017-04-25 10:23:38 -05:00
|
|
|
data.Set("database", "ok")
|
2017-04-25 10:17:45 -05:00
|
|
|
data.Set("version", setting.BuildVersion)
|
|
|
|
data.Set("commit", setting.BuildCommit)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
dataBytes, _ := data.EncodePretty()
|
|
|
|
ctx.Resp.Write(dataBytes)
|
|
|
|
}
|
|
|
|
|
2016-12-21 07:36:32 -06:00
|
|
|
func (hs *HttpServer) mapStatic(m *macaron.Macaron, rootDir string, dir string, prefix string) {
|
|
|
|
headers := func(c *macaron.Context) {
|
|
|
|
c.Resp.Header().Set("Cache-Control", "public, max-age=3600")
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|