1
0
Fork 0

Add the possiblity to listen on Unix socket

This commit is contained in:
Frédéric Guillot 2018-11-11 16:21:57 -08:00
parent becd086865
commit 1315282c7f
2 changed files with 32 additions and 5 deletions

View file

@ -89,7 +89,9 @@ Maximum number of database connections (default is 20)\&.
Minimum number of database connections (default is 1)\&. Minimum number of database connections (default is 1)\&.
.TP .TP
.B LISTEN_ADDR .B LISTEN_ADDR
HTTP server listening address\&. Address to listen on. Default is 127.0.0.1:8080\&.
.br
Use absolute path to listen on Unix socket (/var/run/miniflux.sock)\&.
.TP .TP
.B PORT .B PORT
Override LISTEN_ADDR to 0.0.0.0:$PORT\&. Override LISTEN_ADDR to 0.0.0.0:$PORT\&.

View file

@ -6,7 +6,10 @@ package httpd // import "miniflux.app/service/httpd"
import ( import (
"crypto/tls" "crypto/tls"
"net"
"net/http" "net/http"
"os"
"strings"
"time" "time"
"miniflux.app/api" "miniflux.app/api"
@ -29,27 +32,49 @@ func Serve(cfg *config.Config, store *storage.Storage, pool *worker.Pool, feedHa
keyFile := cfg.KeyFile() keyFile := cfg.KeyFile()
certDomain := cfg.CertDomain() certDomain := cfg.CertDomain()
certCache := cfg.CertCache() certCache := cfg.CertCache()
listenAddr := cfg.ListenAddr()
server := &http.Server{ server := &http.Server{
ReadTimeout: 30 * time.Second, ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second, WriteTimeout: 30 * time.Second,
IdleTimeout: 60 * time.Second, IdleTimeout: 60 * time.Second,
Addr: cfg.ListenAddr(),
Handler: setupHandler(cfg, store, feedHandler, pool), Handler: setupHandler(cfg, store, feedHandler, pool),
} }
if certDomain != "" && certCache != "" { switch {
case strings.HasPrefix(listenAddr, "/"):
startUnixSocketServer(server, listenAddr)
case certDomain != "" && certCache != "":
cfg.IsHTTPS = true cfg.IsHTTPS = true
startAutoCertTLSServer(server, certDomain, certCache) startAutoCertTLSServer(server, certDomain, certCache)
} else if certFile != "" && keyFile != "" { case certFile != "" && keyFile != "":
cfg.IsHTTPS = true cfg.IsHTTPS = true
server.Addr = listenAddr
startTLSServer(server, certFile, keyFile) startTLSServer(server, certFile, keyFile)
} else { default:
server.Addr = listenAddr
startHTTPServer(server) startHTTPServer(server)
} }
return server return server
} }
func startUnixSocketServer(server *http.Server, socketFile string) {
os.Remove(socketFile)
go func(sock string) {
listener, err := net.Listen("unix", sock)
if err != nil {
logger.Fatal(`Server failed to start: %v`, err)
}
defer listener.Close()
logger.Info(`Listening on Unix socket %q`, sock)
if err := server.Serve(listener); err != http.ErrServerClosed {
logger.Fatal(`Server failed to start: %v`, err)
}
}(socketFile)
}
func startAutoCertTLSServer(server *http.Server, certDomain, certCache string) { func startAutoCertTLSServer(server *http.Server, certDomain, certCache string) {
server.Addr = ":https" server.Addr = ":https"
certManager := autocert.Manager{ certManager := autocert.Manager{