-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support to serve http via socket
- Loading branch information
1 parent
db2728f
commit 04c38da
Showing
7 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net" | ||
"net/http" | ||
|
||
"github.com/cnrancher/kube-explorer/internal/config" | ||
dynamicserver "github.com/rancher/dynamiclistener/server" | ||
"github.com/rancher/steve/pkg/server" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func Serve(ctx context.Context, server *server.Server) error { | ||
listener, ipOrPath, err := ensureListener() | ||
if err != nil { | ||
return err | ||
} | ||
if listener != nil { | ||
return serveSocket(ctx, ipOrPath, listener, server) | ||
} | ||
return server.ListenAndServe(ctx, config.Steve.HTTPSListenPort, config.Steve.HTTPListenPort, &dynamicserver.ListenOpts{ | ||
BindHost: ipOrPath, | ||
}) | ||
} | ||
|
||
func serveSocket(ctx context.Context, socketPath string, listener net.Listener, handler http.Handler) error { | ||
logger := logrus.StandardLogger() | ||
errorLog := log.New(logger.WriterLevel(logrus.DebugLevel), "", log.LstdFlags) | ||
socketServer := &http.Server{ | ||
Handler: handler, | ||
ErrorLog: errorLog, | ||
BaseContext: func(_ net.Listener) context.Context { | ||
return ctx | ||
}, | ||
} | ||
go func() { | ||
logrus.Infof("Listening on %s", socketPath) | ||
err := socketServer.Serve(listener) | ||
if err != http.ErrServerClosed && err != nil { | ||
logrus.Fatalf("https server failed: %v", err) | ||
} | ||
}() | ||
go func() { | ||
<-ctx.Done() | ||
socketServer.Shutdown(context.Background()) | ||
}() | ||
<-ctx.Done() | ||
return ctx.Err() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//go:build unix | ||
// +build unix | ||
|
||
package server | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/url" | ||
|
||
"github.com/cnrancher/kube-explorer/internal/config" | ||
) | ||
|
||
func ensureListener() (net.Listener, string, error) { | ||
if config.BindAddress == "" { | ||
return nil, "", nil | ||
} | ||
u, err := url.Parse(config.BindAddress) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
if u.Scheme == "" || u.Scheme == "tcp" { | ||
return nil, u.Host, nil | ||
} | ||
if u.Scheme != "unix" { | ||
return nil, "", fmt.Errorf("Unsupported scheme %s, only tcp and unix are supported in UNIX OS", u.Scheme) | ||
} | ||
listener, err := net.Listen("unix", u.Path) | ||
return listener, u.Path, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package server | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/url" | ||
|
||
"github.com/Microsoft/go-winio" | ||
"github.com/cnrancher/kube-explorer/internal/config" | ||
) | ||
|
||
func ensureListener() (net.Listener, string, error) { | ||
if config.BindAddress == "" { | ||
return nil, "", nil | ||
} | ||
u, err := url.Parse(config.BindAddress) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
if u.Scheme == "" || u.Scheme == "tcp" { | ||
return nil, u.Host, nil | ||
} | ||
if u.Scheme != "namedpipe" { | ||
return nil, "", fmt.Errorf("Unsupported scheme %s, only tcp and namedpipe are supported in windows", u.Scheme) | ||
} | ||
listener, err := winio.ListenPipe(fmt.Sprintf("//%s/%s", u.Host, u.Path), nil) | ||
return listener, u.Path, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters