Documentation ¶
Index ¶
- type Server
- func (s *Server) Address() net.Addr
- func (s *Server) IsListening() bool
- func (s *Server) SetShutdownHandler(shutdownHandler func())
- func (s *Server) Start(address string) (err error)
- func (s *Server) StartSocket(address string) (err error)
- func (s *Server) Stop() <-chan struct{}
- func (s *Server) Wait() <-chan struct{}
- func (s *Server) WaitForStart() <-chan struct{}
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Server ¶
type Server struct { TLSConfig *tls.Config DisableHTTP2 bool // contains filtered or unexported fields }
Server helps reduce boilerplate when writing tools that center around an http.Server instance.
For example, to create a standalone server that can bind to a command-line configurable address (e.g.)
./server -address=":80"
Use something like the following:
package main import ( "flag" "log" "os" "os/signal" "syscall" "github.com/jeremyot/httpserver" "github.com/jeremyot/structflag" ) func monitorSignal(s *httpserver.Server, sigChan <-chan os.Signal) { sig := <-sigChan log.Printf("Exiting (%s)...", sig) select { case <-s.Stop(): return case <-sigChan: log.Printf("Force quitting (%s)...", sig) os.Exit(-1) } } type ServerConfig struct { Address string `json:"address" flag:"address,The address to bind to,[::]:8080"` } func main() { var serverConfig ServerConfig structflag.StructToFlags("", &serverConfig) flag.Parse() sigChan := make(chan os.Signal) signal.Notify(sigChan, syscall.SIGINT, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGQUIT) s := httpserver.New(http.NewServeMux().ServeHTTP) go monitorSignal(s, sigChan) s.Start(serverConfig.Address) <-s.Wait() }
func New ¶
func New(handlerFunc http.HandlerFunc) *Server
New returns a server with the specified handler.
func (*Server) IsListening ¶
IsListening returns true if the server is running
func (*Server) SetShutdownHandler ¶
func (s *Server) SetShutdownHandler(shutdownHandler func())
SetShutdownHandler lets you add a function to the shutdown pipeline. It will be called after http.Server.Shutdown and will block Stop and Wait until it returns.
func (*Server) Start ¶
Start starts the Server listening on the specified tcp address. If no port is specified, the Server will pick one. Use Address() after start to see which port was selected.
func (*Server) StartSocket ¶
StartSocket starts the Server listening on the specified socket.
func (*Server) Stop ¶
func (s *Server) Stop() <-chan struct{}
Stop gracefully shuts down the Server and returns the channel from Wait. Note that it has the same limitations as http.Server.Shutdown.
func (*Server) Wait ¶
func (s *Server) Wait() <-chan struct{}
Wait returns a channel that is closed after the Server has shut down.
func (*Server) WaitForStart ¶
func (s *Server) WaitForStart() <-chan struct{}
WaitForStart returns a channel that is closed when the Server has finished starting and is listening for connections on Address().