From c92d719a72172f9e88ecbca07ca7eab803cf2d5e Mon Sep 17 00:00:00 2001 From: Mitja Z Date: Thu, 27 Apr 2017 08:54:21 +0200 Subject: [PATCH] added support for listening on unix socket #4030 (#8221) --- conf/defaults.ini | 5 ++++- conf/sample.ini | 5 ++++- pkg/api/http_server.go | 15 ++++++++++++++- pkg/setting/setting.go | 6 ++++++ 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/conf/defaults.ini b/conf/defaults.ini index 85384d5b2f4..8eca55fc67c 100644 --- a/conf/defaults.ini +++ b/conf/defaults.ini @@ -25,7 +25,7 @@ plugins = data/plugins #################################### Server ############################## [server] -# Protocol (http or https) +# Protocol (http, https, socket) protocol = http # The ip address to bind to, empty will bind to all interfaces @@ -57,6 +57,9 @@ enable_gzip = false cert_file = cert_key = +# Unix socket path +socket = /tmp/grafana.sock + #################################### Database ############################ [database] # You can configure the database connection by specifying type, host, name, user and password diff --git a/conf/sample.ini b/conf/sample.ini index 0d74bcd56f1..84328e7d537 100644 --- a/conf/sample.ini +++ b/conf/sample.ini @@ -26,7 +26,7 @@ # #################################### Server #################################### [server] -# Protocol (http or https) +# Protocol (http, https, socket) ;protocol = http # The ip address to bind to, empty will bind to all interfaces @@ -59,6 +59,9 @@ ;cert_file = ;cert_key = +# Unix socket path +;socket = + #################################### Database #################################### [database] # You can configure the database connection by specifying type, host, name, user and password diff --git a/pkg/api/http_server.go b/pkg/api/http_server.go index 6f7581064f5..06ef5a22125 100644 --- a/pkg/api/http_server.go +++ b/pkg/api/http_server.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "errors" "fmt" + "net" "net/http" "os" "path" @@ -49,7 +50,7 @@ func (hs *HttpServer) Start(ctx context.Context) error { hs.streamManager.Run(ctx) listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort) - hs.log.Info("Initializing HTTP Server", "address", listenAddr, "protocol", setting.Protocol, "subUrl", setting.AppSubUrl) + hs.log.Info("Initializing HTTP Server", "address", listenAddr, "protocol", setting.Protocol, "subUrl", setting.AppSubUrl, "socket", setting.SocketPath) hs.httpSrv = &http.Server{Addr: listenAddr, Handler: hs.macaron} switch setting.Protocol { @@ -65,6 +66,18 @@ func (hs *HttpServer) Start(ctx context.Context) error { hs.log.Debug("server was shutdown gracefully") return nil } + 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 + } default: hs.log.Error("Invalid protocol", "protocol", setting.Protocol) err = errors.New("Invalid Protocol") diff --git a/pkg/setting/setting.go b/pkg/setting/setting.go index 6352c1d7913..8cc0e723712 100644 --- a/pkg/setting/setting.go +++ b/pkg/setting/setting.go @@ -27,6 +27,7 @@ type Scheme string const ( HTTP Scheme = "http" HTTPS Scheme = "https" + SOCKET Scheme = "socket" DEFAULT_HTTP_ADDR string = "0.0.0.0" ) @@ -65,6 +66,7 @@ var ( HttpAddr, HttpPort string SshPort int CertFile, KeyFile string + SocketPath string RouterLogging bool DataProxyLogging bool StaticRootPath string @@ -473,6 +475,10 @@ func NewConfigContext(args *CommandLineArgs) error { CertFile = server.Key("cert_file").String() KeyFile = server.Key("cert_key").String() } + if server.Key("protocol").MustString("http") == "socket" { + Protocol = SOCKET + SocketPath = server.Key("socket").String() + } Domain = server.Key("domain").MustString("localhost") HttpAddr = server.Key("http_addr").MustString(DEFAULT_HTTP_ADDR)