-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enhance: add functions for daemon servers for mTLS #87
Conversation
Signed-off-by: Grant Linville <[email protected]>
Signed-off-by: Grant Linville <[email protected]>
Signed-off-by: Grant Linville <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with the two functions for creating a server, but it seems strange to call CreateServer()
, add a bunch of stuff to the default http
server mux, and then StartServer
.
Instead of returning an *http.Server
, can we return our own server type that has methods like the http.ServerMux
that just passes them onto an *http.ServerMux
field on the server type. Something like:
type Server struct {
mux *http.ServeMux
tlsConfig *tls.Config
port string
}
func CreateServer(port string) (*Server, error) {
retrurn CreateServerWithMux(http.DefaultServeMux, port)
}
func CreateServerWithMux(mux *http.ServeMux, port string) (*Server, error) {
tlsConfig, err := getTLSConfig()
if err != nil {
return nil, fmt.Errorf("failed to get TLS config: %v", err)
}
server := &Server{
tlsConfig: tlsConfig,
port: port,
mux: mux,
}
return server, nil
}
func (s *Server) HandleFunc(patter string, handler http.HandlerFunc) {
s.mux.HandleFunc(pattern, handler)
}
pkg/daemon/daemon.go
Outdated
// StartServer starts an HTTP server created by the CreateServer function. | ||
// This is for use with daemon tools. | ||
func StartServer(server *http.Server) error { | ||
if err := server.ListenAndServeTLS("", ""); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ListenAndServerTLS
always returns a non-nil error. We may want to check for http.ErrServerClosed
because that would be expected, I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
} | ||
|
||
server := &http.Server{ | ||
Addr: fmt.Sprintf("127.0.0.1:%s", os.Getenv("PORT")), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably want to make the port
a parameter. Otherwise, you could only ever run one daemon at a time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we do only want to run one daemon at a time, at least one per process, and it is required to use the port that GPTScript chose for it. So I don't think there is a point in making this a parameter. This is just a library function to be called in each daemon tool, not something that will get used anywhere else.
Signed-off-by: Grant Linville <[email protected]>
Here is an example of what it looks like to use these functions: