Support Unix domain sockets for proxy server listener (#1865)

This commit is contained in:
LINKIWI
2023-05-06 02:35:06 -07:00
committed by GitHub
parent e35007b3a2
commit e5aa5974e1
3 changed files with 30 additions and 5 deletions
+23 -5
View File
@@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"log"
"net"
"net/http"
_ "net/http/pprof"
"os"
@@ -45,7 +46,6 @@ func main() {
}
srv := &http.Server{
Addr: conf.Port,
Handler: handler,
ReadHeaderTimeout: 2 * time.Second,
}
@@ -75,11 +75,29 @@ func main() {
}()
}
log.Printf("Starting application at port %v", conf.Port)
if cert != "" && key != "" {
err = srv.ListenAndServeTLS(conf.TLSCertFile, conf.TLSKeyFile)
// Unix socket configuration, if available, takes precedence over TCP port configuration.
var ln net.Listener
if conf.UnixSocket != "" {
log.Printf("Starting application at Unix domain socket %v", conf.UnixSocket)
ln, err = net.Listen("unix", conf.UnixSocket)
if err != nil {
log.Fatalf("error listening on Unix domain socket %v: %v", conf.UnixSocket, err)
}
} else {
err = srv.ListenAndServe()
log.Printf("Starting application at port %v", conf.Port)
ln, err = net.Listen("tcp", conf.Port)
if err != nil {
log.Fatalf("error listening on TCP port %v: %v", conf.Port, err)
}
}
if cert != "" && key != "" {
err = srv.ServeTLS(ln, conf.TLSCertFile, conf.TLSKeyFile)
} else {
err = srv.Serve(ln)
}
if !errors.Is(err, http.ErrServerClosed) {
+6
View File
@@ -131,6 +131,12 @@ StorageType = "memory"
# The PORT must be a number or a number prefixed by ":"
Port = ":3000"
# UnixSocket sets a Unix domain socket path that the proxy listens on.
# This option, when specified, takes precedence over TCP port configuration.
# Defaults to empty (i.e. listen on a TCP port only)
# Env override: ATHENS_UNIX_SOCKET
UnixSocket = ""
# The endpoint for a package registry in case of a proxy cache miss
# NOTE: Currently no registries have been implemented
# Env override: ATHENS_GLOBAL_ENDPOINT
+1
View File
@@ -38,6 +38,7 @@ type Config struct {
StorageType string `validate:"required" envconfig:"ATHENS_STORAGE_TYPE"`
GlobalEndpoint string `envconfig:"ATHENS_GLOBAL_ENDPOINT"` // This feature is not yet implemented
Port string `envconfig:"ATHENS_PORT"`
UnixSocket string `envconfig:"ATHENS_UNIX_SOCKET"`
BasicAuthUser string `envconfig:"BASIC_AUTH_USER"`
BasicAuthPass string `envconfig:"BASIC_AUTH_PASS"`
ForceSSL bool `envconfig:"PROXY_FORCE_SSL"`