httpserver

package module
v0.0.0-...-a84e0c8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2023 License: MIT Imports: 5 Imported by: 3

Documentation

Index

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) Address

func (s *Server) Address() net.Addr

Address returns the server's current address.

func (*Server) IsListening

func (s *Server) IsListening() bool

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

func (s *Server) Start(address string) (err error)

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

func (s *Server) StartSocket(address string) (err error)

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().

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL