Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 37f76ea

Browse files
committedDec 16, 2024··
PR feedback
Signed-off-by: Grant Linville <grant@acorn.io>
1 parent d3c7d38 commit 37f76ea

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed
 

‎pkg/daemon/daemon.go

+25-20
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,58 @@ import (
44
"crypto/tls"
55
"crypto/x509"
66
"encoding/base64"
7+
"errors"
78
"fmt"
89
"net/http"
910
"os"
1011
)
1112

13+
type Server struct {
14+
mux *http.ServeMux
15+
tlsConfig *tls.Config
16+
}
17+
1218
// CreateServer creates a new HTTP server with TLS configured for GPTScript.
1319
// This function should be used when creating a new server for a daemon tool.
1420
// The server should then be started with the StartServer function.
15-
func CreateServer() (*http.Server, error) {
16-
tlsConfig, err := getTLSConfig()
17-
if err != nil {
18-
return nil, fmt.Errorf("failed to get TLS config: %v", err)
19-
}
20-
21-
server := &http.Server{
22-
Addr: fmt.Sprintf("127.0.0.1:%s", os.Getenv("PORT")),
23-
TLSConfig: tlsConfig,
24-
}
25-
return server, nil
21+
func CreateServer() (*Server, error) {
22+
return CreateServerWithMux(http.DefaultServeMux)
2623
}
2724

2825
// CreateServerWithMux creates a new HTTP server with TLS configured for GPTScript.
2926
// This function should be used when creating a new server for a daemon tool with a custom ServeMux.
3027
// The server should then be started with the StartServer function.
31-
func CreateServerWithMux(mux *http.ServeMux) (*http.Server, error) {
28+
func CreateServerWithMux(mux *http.ServeMux) (*Server, error) {
3229
tlsConfig, err := getTLSConfig()
3330
if err != nil {
3431
return nil, fmt.Errorf("failed to get TLS config: %v", err)
3532
}
3633

34+
return &Server{
35+
mux: mux,
36+
tlsConfig: tlsConfig,
37+
}, nil
38+
}
39+
40+
// Start starts an HTTP server created by the CreateServer function.
41+
// This is for use with daemon tools.
42+
func (s *Server) Start() error {
3743
server := &http.Server{
3844
Addr: fmt.Sprintf("127.0.0.1:%s", os.Getenv("PORT")),
39-
TLSConfig: tlsConfig,
40-
Handler: mux,
45+
TLSConfig: s.tlsConfig,
46+
Handler: s.mux,
4147
}
42-
return server, nil
43-
}
4448

45-
// StartServer starts an HTTP server created by the CreateServer function.
46-
// This is for use with daemon tools.
47-
func StartServer(server *http.Server) error {
48-
if err := server.ListenAndServeTLS("", ""); err != nil {
49+
if err := server.ListenAndServeTLS("", ""); err != nil && !errors.Is(err, http.ErrServerClosed) {
4950
return fmt.Errorf("stopped serving: %v", err)
5051
}
5152
return nil
5253
}
5354

55+
func (s *Server) HandleFunc(pattern string, handler http.HandlerFunc) {
56+
s.mux.HandleFunc(pattern, handler)
57+
}
58+
5459
func getTLSConfig() (*tls.Config, error) {
5560
certB64 := os.Getenv("CERT")
5661
privateKeyB64 := os.Getenv("PRIVATE_KEY")

0 commit comments

Comments
 (0)
Please sign in to comment.