net/http/server
Package server implements a customizable HTTP server.
Index
- ListenAndServe
- ListenAndServeTLS
- Server
- Server.Serve
- Server.ServeTLS
- Server.Shutdown
- Server.Close
- Server.CloseIdleConnections
- Server.ConnContext
- Server.ErrorLog
- Server.Handler
- Server.ReadTimeout
- Server.WriteTimeout
- Server.IdleTimeout
- Server.MaxHeaderBytes
- Server.TLSConfig
- Server.BaseContext
- Server.ConnState
- Server.TLSNextProto
- Server.ConnContext
- Server.DisableGeneralHandler
- Server.RegisterOnShutdown
- Server.SetKeepAlivesEnabled
ListenAndServe
func ListenAndServe(addr string, handler Handler) error
ListenAndServe starts an HTTP server with a given address and handler. The handler is typically nil, which means to use DefaultServeMux.
The server listens on the TCP network address addr. It then calls Serve with handler to handle requests on incoming connections.
ListenAndServe always returns a non-nil error. Close and shutdown are alternatives to stop ListenAndServe.
ListenAndServeTLS
func ListenAndServeTLS(addr, certFile, keyFile string, handler Handler) error
ListenAndServeTLS starts an HTTP server with a given address and handler, which listens on the TLS network address addr.
The handler is typically nil, which means to use DefaultServeMux. The certFile and keyFile parameters are the paths to the TLS certificate and key.
If Shutdown is called, ListenAndServeTLS will exit.
Server
type Server struct { ... }
A Server defines parameters for running an HTTP server.
Fields
-
Handler Handler
Handler chooses the handler to serve the request by calling the Server.Handler method. If nil, the DefaultServeMux is used.
-
ReadTimeout time.Duration
ReadTimeout is the maximum duration for reading the entire request, including the request line, headers, and body. If ReadTimeout is zero, that means there is no timeout.
-
WriteTimeout time.Duration
WriteTimeout is the maximum duration before writing the response headers and their body. If WriteTimeout is zero, that means there is no timeout.
-
IdleTimeout time.Duration
IdleTimeout is the maximum amount of time a connection will be kept open before closing. If IdleTimeout is zero, that means there is no timeout.
-
MaxHeaderBytes int
MaxHeaderBytes controls the maximum number of bytes the server will read for request headers.
-
TLSConfig *tls.Config
TLSConfig is used to configure the server's TLS configuration. If nil, default TLS configuration is used.
-
ErrorLog *log.Logger
ErrorLog specifies an logger for errors encountered by the server. If nil, logging is sent to os.Stderr.
-
BaseContext context.Context
BaseContext is a base context for newly accepted connections. The http server will sample the BaseContext value on each request received from the connection. If BaseContext is nil, the default base context is used.
-
ConnState ConnStateCallback
ConnState is called when a client connection changes state. If nil, no callback is made.
-
TLSNextProto map[string]func(*Server, *tls.Conn, Handler)
TLSNextProto optionally specifies a function to be called for the transport protocol negotiation of a TLS connection.
Server.Serve
func (srv *Server) Serve(l net.Listener) error
Serve accepts incoming connections on the listener l, creating a service goroutine for each. The service goroutines read requests and then call the handler to reply to them. Serve always returns a non-nil error.