Documentation
¶
Index ¶
- Constants
- func BaseContext(ctx context.Context) func(_ net.Listener) context.Context
- func DefaultTLSConfig() *tls.Config
- func JoinHostPort(host string, port Port) string
- func ServerName(ctx context.Context) string
- func WriteJSON(w http.ResponseWriter, v any) error
- type CertificateLoader
- type Config
- type DefaultLogger
- type Logger
- type Option
- func WithBaseContext(ctx context.Context) Option
- func WithDefaultLogger() Option
- func WithHandler(h http.Handler) Option
- func WithLogger(l Logger) Option
- func WithMiddleware(mw ...middleware.Wrapper) Option
- func WithName(name string) Option
- func WithOptions(opts ...Option) Option
- func WithRoutes(reg ...RoutesRegisterer) Option
- func WithTLS(conf *tls.Config, opts ...TLSOption) Option
- type Port
- type PortParseError
- type Route
- type RouteHandler
- type Router
- type RoutesRegisterer
- type RoutesRegistererFunc
- type ServeMux
- type Server
- func (srv *Server) Close() error
- func (srv *Server) IsStarted() bool
- func (srv *Server) ListenAndServe() error
- func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error
- func (srv *Server) Name() string
- func (srv *Server) Run() error
- func (srv *Server) Serve(l net.Listener) error
- func (srv *Server) ServeTLS(l net.Listener, certFile, keyFile string) error
- func (srv *Server) Shutdown(ctx context.Context) error
- func (srv *Server) With(opts ...Option) error
- type TLSConfig
- type TLSKeyPair
- type TLSOption
- type TLSPemBlocks
Examples ¶
Constants ¶
const ( ErrMissingPort errors.Msg = "missing port" ErrInvalidFormat errors.Msg = "invalid format" )
const ( ErrAlreadyStarted errors.Msg = "server is already started" ErrUnstartedShutdown errors.Msg = "cannot shutdown server that is not started" ErrUnstartedClose errors.Msg = "cannot close server that is not started" )
const ErrHandlerIsNoRouteHandler errors.Msg = "server handler is not a RouteHandler"
Variables ¶
This section is empty.
Functions ¶
func BaseContext ¶
BaseContext returns a function which returns the provided context.
func DefaultTLSConfig ¶
DefaultTLSConfig returns a modern preconfigured tls.Config.
func JoinHostPort ¶
JoinHostPort uses net.JoinHostPort to combine host and port into a network address of the form "host:port". If host contains a colon, as found in literal IPv6 addresses, then JoinHostPort returns "[host]:port".
func ServerName ¶
ServerName gets the server's name from context values. Its return value may be an empty string.
Types ¶
type CertificateLoader ¶
type CertificateLoader interface {
LoadCertificate() (*tls.Certificate, error)
}
CertificateLoader loads a tls.Certificate from any source.
type Config ¶
type Config struct { // ReadTimeout is the maximum duration for reading the entire request, // including the body. // See http.Server for additional information. ReadTimeout time.Duration `default:"5s"` // ReadHeaderTimeout is the amount of time allowed to read request headers. // See http.Server for additional information. ReadHeaderTimeout time.Duration `default:"2s"` // WriteTimeout is the maximum duration before timing out writes of the // response. // See http.Server for additional information. WriteTimeout time.Duration `default:"10s"` // IdleTimeout is the maximum amount of time to wait for the next request // when keep-alives are enabled. // See http.Server for additional information. IdleTimeout time.Duration `default:"120s"` // ShutdownTimeout is the maximum duration for shutting down the server and // waiting for all connections to be closed. ShutdownTimeout time.Duration `default:"60s"` // MaxHeaderBytes controls the maximum number of bytes the server will read // parsing the request header's keys and values, including the request line. // It does not limit the size of the request body. // See http.Server for additional information. MaxHeaderBytes uint64 `default:"10240"` // data.Bytes => 10 KiB }
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a Config with default values.
type DefaultLogger ¶
func (*DefaultLogger) ServerClose ¶
func (l *DefaultLogger) ServerClose(name string)
func (*DefaultLogger) ServerShutdown ¶
func (l *DefaultLogger) ServerShutdown(name string)
func (*DefaultLogger) ServerStart ¶
func (l *DefaultLogger) ServerStart(name, addr string)
type Logger ¶
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
func WithBaseContext ¶ added in v0.2.0
func WithDefaultLogger ¶ added in v0.2.0
func WithDefaultLogger() Option
func WithHandler ¶ added in v0.2.0
func WithLogger ¶
func WithMiddleware ¶
func WithMiddleware(mw ...middleware.Wrapper) Option
WithMiddleware adds the middleware.Middleware to an internal list. When the Server is started, it's Handler is wrapped with this middleware.
func WithOptions ¶
WithOptions wraps multiple Option(s) into a single Option.
func WithRoutes ¶
func WithRoutes(reg ...RoutesRegisterer) Option
type Port ¶
type Port uint16
func ParsePort ¶
Example ¶
port, err := ParsePort(":8080") if err != nil { log.Fatal(err) } fmt.Println(port)
Output: 8080
func SplitHostPort ¶
SplitHostPort uses net.SplitHostPort to split a network address of the form "host:port", "host%zone:port", "[host]:port" or "[host%zone]:port" into host or host%zone and Port.
Example ¶
host, port, err := SplitHostPort("localhost:8080") if err != nil { log.Fatal(err) } fmt.Println(host, port)
Output: localhost 8080
func (Port) MarshalText ¶
func (*Port) UnmarshalText ¶
type PortParseError ¶
type PortParseError struct { // Cause is the underlying error. It is never nil. Cause error // Input string that triggered the error Input string }
func (*PortParseError) Error ¶
func (p *PortParseError) Error() string
func (*PortParseError) Unwrap ¶
func (p *PortParseError) Unwrap() error
type Route ¶ added in v0.2.0
type RouteHandler ¶ added in v0.2.0
type RouteHandler interface {
HandleRoute(route Route)
}
RouteHandler handles routes.
type Router ¶ added in v0.2.0
type Router interface { RouteHandler http.Handler }
Router is a http.Handler that can handle routes.
type RoutesRegisterer ¶ added in v0.2.0
type RoutesRegisterer interface {
RegisterRoutes(r RouteHandler)
}
RoutesRegisterer registers routes to a RouteHandler.
type RoutesRegistererFunc ¶ added in v0.2.0
type RoutesRegistererFunc func(r RouteHandler)
RoutesRegistererFunc registers routes to a RouteHandler.
func (RoutesRegistererFunc) RegisterRoutes ¶ added in v0.2.0
func (fn RoutesRegistererFunc) RegisterRoutes(r RouteHandler)
type ServeMux ¶ added in v0.2.0
type ServeMux struct {
// contains filtered or unexported fields
}
ServeMux is a http.ServeMux wrapper which implements the Router interface. See http.ServeMux for more information.
func NewServeMux ¶ added in v0.2.0
func NewServeMux() *ServeMux
NewServeMux creates a new ServeMux and is ready to be used.
func (*ServeMux) HandleRoute ¶ added in v0.2.0
type Server ¶
type Server struct { // Config to apply to the server, DefaultConfig if nil. Config *Config // Addr optionally specifies the TCP address for the server to listen on. // See net.Dial for details of the address format. // See http.Server for additional information. Addr string // Handler to invoke, http.DefaultServeMux if nil. Handler http.Handler // contains filtered or unexported fields }
Server is a wrapper for http.Server. The zero value is safe and ready to use, and will apply safe defaults on starting the server.
func New ¶
New creates a new Server. DefaultConfig is applied to it when no other Config is provided as option.
func (*Server) Close ¶
Close immediately closes all active net.Listeners and any connections in state http.StateNew, http.StateActive, or http.StateIdle. For a graceful shutdown, use Shutdown. It is a wrapper for http.NewClient.Close.
func (*Server) ListenAndServe ¶
ListenAndServe is a wrapper for http.Server.ListenAndServe.
func (*Server) ListenAndServeTLS ¶
ListenAndServeTLS is a wrapper for http.Server.ListenAndServeTLS.
func (*Server) Shutdown ¶
Shutdown gracefully shuts down the server without interrupting any active connections. Just like the underlying http.Server, Shutdown works by first closing all open listeners, then closing all idle connections, and then waiting indefinitely for connections to return to idle and then shut down. If ShutdownTimeout is set and/or the provided context expires before the shutdown is complete, Shutdown returns the context's error. Otherwise, it returns any error returned from closing the Server's underlying net.Listener(s).
type TLSConfig ¶
type TLSConfig struct { CaCertFile string `env:"" flag:"tls-cacert"` CertFile string `env:"" flag:"tls-cert"` KeyFile string `env:"" flag:"tls-key"` // VerifyClient enables mutual tls authentication. VerifyClient bool `env:""` // InsecureSkipVerify disabled all certificate verification and should only // be used for testing. InsecureSkipVerify bool `env:""` }
type TLSKeyPair ¶
TLSKeyPair contains the paths to a public/private key pair of files.
func (TLSKeyPair) LoadCertificate ¶
func (kp TLSKeyPair) LoadCertificate() (*tls.Certificate, error)
LoadCertificate reads and parses the key pair files with tls.LoadX509KeyPair. The files must contain PEM encoded data.
type TLSPemBlocks ¶
certPEMBlock, keyPEMBlock
func (TLSPemBlocks) ApplyTo ¶ added in v0.2.0
func (pb TLSPemBlocks) ApplyTo(conf *tls.Config) error
func (TLSPemBlocks) LoadCertificate ¶
func (pb TLSPemBlocks) LoadCertificate() (*tls.Certificate, error)