Skip to content

Commit

Permalink
feat: add WithServer option (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
haysons authored Nov 14, 2024
1 parent 5e5d296 commit fff4bf9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
10 changes: 10 additions & 0 deletions graceful.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,16 @@ func (g *Graceful) appendHTTPServer() *http.Server {
return srv
}

// appendExistHTTPServer appends an existing HTTP server to the list of servers managed by the Graceful instance.
// This allows for customization of the http.Server, and srv.Handler will be set to the current g.Engine.
func (g *Graceful) appendExistHTTPServer(srv *http.Server) {
srv.Handler = g.Engine

g.lock.Lock()
defer g.lock.Unlock()
g.servers = append(g.servers, srv)
}

// ensureAtLeastDefaultServer ensures that there is at least one server running with the default address ":8080".
// If no server is running, it creates a new server with the default address and starts it.
// It returns an error if there was a problem creating or starting the server.
Expand Down
19 changes: 18 additions & 1 deletion graceful_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ func TestWithListener(t *testing.T) {
}, fmt.Sprintf("http://localhost:%d/example", listener.Addr().(*net.TCPAddr).Port))
}

func TestWithServer(t *testing.T) {
cert, err := tls.LoadX509KeyPair("./testdata/certificate/cert.pem", "./testdata/certificate/key.pem")
assert.NoError(t, err)
tlsConfig := &tls.Config{

Check failure on line 193 in graceful_test.go

View workflow job for this annotation

GitHub Actions / lint

G402: TLS MinVersion too low. (gosec)
Certificates: []tls.Certificate{cert},
}
testRouterConstructor(t, func() (*Graceful, error) {
return Default(
WithServer(&http.Server{Addr: ":8811"}),

Check failure on line 198 in graceful_test.go

View workflow job for this annotation

GitHub Actions / lint

G112: Potential Slowloris Attack because ReadHeaderTimeout is not configured in the http.Server (gosec)
WithServer(&http.Server{Addr: ":9443", TLSConfig: tlsConfig}),

Check failure on line 199 in graceful_test.go

View workflow job for this annotation

GitHub Actions / lint

G112: Potential Slowloris Attack because ReadHeaderTimeout is not configured in the http.Server (gosec)
)
}, "http://localhost:8811/example", "https://localhost:9443/example")
}

func TestWithAll(t *testing.T) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
assert.NoError(t, err)
Expand All @@ -198,11 +212,14 @@ func TestWithAll(t *testing.T) {
return Default(WithAddr(":8080"),
WithTLS(":8443", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem"),
WithListener(listener),
WithServer(&http.Server{Addr: ":8811"}),

Check failure on line 215 in graceful_test.go

View workflow job for this annotation

GitHub Actions / lint

G112: Potential Slowloris Attack because ReadHeaderTimeout is not configured in the http.Server (gosec)
)
},
"http://localhost:8080/example",
"https://localhost:8443/example",
fmt.Sprintf("http://localhost:%d/example", listener.Addr().(*net.TCPAddr).Port))
fmt.Sprintf("http://localhost:%d/example", listener.Addr().(*net.TCPAddr).Port),
"http://localhost:8811/example",
)
}

func TestWithContext(t *testing.T) {
Expand Down
23 changes: 23 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package graceful

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

Expand Down Expand Up @@ -48,6 +50,27 @@ func WithTLS(addr string, certFile string, keyFile string) Option {
})
}

// WithServer configure an existing http.Server to serve HTTP or HTTPS requests.
// This allows for a more complete customization of the http.Server,
// and srv Handler will be set to the current gin.Engine.
// If srv contains TLSConfig, ListenAndServeTLS will be used;
// otherwise, ListenAndServe will be used.
func WithServer(srv *http.Server) Option {
return optionFunc(func(g *Graceful) (listenAndServe, cleanup, error) {
if srv == nil {
return nil, donothing, errors.New("nil http server")
}
return func() error {
g.appendExistHTTPServer(srv)
if srv.TLSConfig == nil {
return srv.ListenAndServe()
} else {
return srv.ListenAndServeTLS("", "")
}
}, donothing, nil
})
}

// WithUnix configure a http.Server to listen on the given unix socket file.
func WithUnix(file string) Option {
return optionFunc(func(g *Graceful) (listenAndServe, cleanup, error) {
Expand Down

0 comments on commit fff4bf9

Please sign in to comment.