Skip to content
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

Merged
merged 4 commits into from
Dec 16, 2024

Conversation

g-linville
Copy link
Member

@g-linville g-linville commented Dec 4, 2024

Here is an example of what it looks like to use these functions:

package main

import (
	"fmt"
	"net/http"
	"os"

	"github.com/gptscript-ai/go-gptscript/pkg/daemon"
)

func main() {
	server, err := daemon.CreateServer()
	if err != nil {
		fmt.Printf("failed to create server: %v\n", err)
		os.Exit(1)
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		clientCert := r.TLS.PeerCertificates[0]
		fmt.Fprintf(w, "Hello, %s\n", clientCert.Subject.CommonName)
	})

	if err := daemon.StartServer(server); err != nil {
		fmt.Printf("failed to start server: %v\n", err)
		os.Exit(1)
	}
}

@g-linville g-linville marked this pull request as ready for review December 6, 2024 03:37
Copy link
Collaborator

@thedadams thedadams left a 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)
}

// 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 {
Copy link
Collaborator

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.

Copy link
Member Author

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")),
Copy link
Collaborator

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.

Copy link
Member Author

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]>
@g-linville g-linville requested a review from thedadams December 16, 2024 19:48
@g-linville g-linville merged commit 79a6682 into gptscript-ai:main Dec 16, 2024
2 checks passed
@g-linville g-linville deleted the mtls branch December 16, 2024 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants