Files
gluetun/internal/httpserver/server.go
2022-03-30 09:00:36 +00:00

56 lines
1.3 KiB
Go

// Package httpserver implements an HTTP server.
package httpserver
import (
"context"
"fmt"
"net/http"
"time"
)
var _ Interface = (*Server)(nil)
// Interface is the HTTP server composite interface.
type Interface interface {
Runner
AddressGetter
}
// Runner is the interface for an HTTP server with a Run method.
type Runner interface {
Run(ctx context.Context, ready chan<- struct{}, done chan<- struct{})
}
// AddressGetter obtains the address the HTTP server is listening on.
type AddressGetter interface {
GetAddress() (address string)
}
// Server is an HTTP server implementation, which uses
// the HTTP handler provided.
type Server struct {
address string
addressSet chan struct{}
handler http.Handler
logger Logger
shutdownTimeout time.Duration
}
// New creates a new HTTP server with the given settings.
// It returns an error if one of the settings is not valid.
func New(settings Settings) (s *Server, err error) {
settings.SetDefaults()
if err = settings.Validate(); err != nil {
return nil, fmt.Errorf("http server settings validation failed: %w", err)
}
return &Server{
address: settings.Address,
addressSet: make(chan struct{}),
handler: settings.Handler,
logger: settings.Logger,
shutdownTimeout: *settings.ShutdownTimeout,
}, nil
}