httpx

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

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

Go to latest
Published: Nov 11, 2020 License: MIT Imports: 8 Imported by: 1

README

httpx Build Status Go Report Card GoDoc

Provides an extended, production-ready HTTP server.

Backstory: https://bojanz.github.io/increasing-http-server-boilerplate-go/

Features

  1. Production-ready defaults (TLS, timeouts), following Cloudflare recommendations.
  2. Limiter for max simultaneous connections.
  3. Support for systemd sockets.
    // Serve r on port 80.
    server := httpx.NewServer(":80", r)
    err := server.Start()
    // Serve r on a systemd socket (FileDescriptorName=myapp-http).
    server := httpx.NewServer("systemd:myapp-http", r)
    err := server.Start()

    cert, err := tls.LoadX509KeyPair("/srv/cert.pem", "/srv/key.pem")
    // Serve r on port 443.
    server := httpx.NewServerTLS(":443", cert, r)
    err := server.Start()
    // Serve r on a systemd TLS socket (FileDescriptorName=myapp-https).
    server := httpx.NewServerTLS("systemd:myapp-https", cert, r)
    err := server.Start()

    // Serve up to 1000 simultaneous connections on port 8080.
    server := httpx.NewServer(":8080", r)
    server.MaxConnections = 1000
    err := server.Start()

Systemd setup

/etc/systemd/system/myapp.service:

[Unit]
Description=MyApp
Requires=myapp-http.socket myapp-https.socket

[Service]
Type=simple
ExecStart=/usr/bin/myapp serve
NonBlocking=true
Restart=always

[Install]
WantedBy=multi-user.target

/etc/systemd/system/myapp-http.socket:

[Unit]
Description=MyApp HTTP socket
PartOf=myapp.service

[Socket]
ListenStream=80
NoDelay=true
Service=myapp.service
FileDescriptorName=myapp-http

[Install]
WantedBy=sockets.target

/etc/systemd/system/myapp-https.socket:

[Unit]
Description=MyApp HTTPS socket
PartOf=myapp.service

[Socket]
ListenStream=443
NoDelay=true
Service=myapp.service
FileDescriptorName=myapp-https

[Install]
WantedBy=sockets.target

Additional resources:

Alternatives

  • ory/graceful provides production-ready defaults and graceful shutdown.

Documentation

Overview

Package httpx provides an extended, production-ready HTTP server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	*http.Server

	// MaxConnections limits the number of accepted simultaneous connections.
	// Defaults to 0, indicating no limit.
	MaxConnections int
}

Server represents an HTTP server.

func NewServer

func NewServer(addr string, handler http.Handler) *Server

NewServer creates a new HTTP server.

The addr is a TCP address in the form of "host:port" (e.g. "0.0.0.0:80") or a systemd socket name (e.g. "systemd:myapp-http"). The handler can be nil, in which case http.DefaultServeMux is used.

func NewServerTLS

func NewServerTLS(addr string, cert tls.Certificate, handler http.Handler) *Server

NewServerTLS creates a new HTTPS server.

The addr is a TCP address in the form of "host:port" (e.g. "0.0.0.0:80") or a systemd socket name (e.g. "systemd:myapp-http"). The handler can be nil, in which case http.DefaultServeMux is used.

func (*Server) IsTLS

func (srv *Server) IsTLS() bool

IsTLS returns whether TLS is enabled.

func (*Server) Listen

func (srv *Server) Listen() (net.Listener, error)

Listen returns a TCP or systemd socket listener for srv.Addr.

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe() error

ListenAndServe listens on srv.Addr and calls Serve to handle incoming requests.

Accepted connections are configured to enable TCP keep-alives.

ListenAndServe always returns a non-nil error. After Shutdown or Close, the returned error is ErrServerClosed.

func (*Server) ListenAndServeTLS

func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error

ListenAndServeTLS listens on srv.Addr and calls ServeTLS to handle incoming requests.

Accepted connections are configured to enable TCP keep-alives.

Filenames containing a certificate and matching private key for the server must be provided if neither the Server's TLSConfig.Certificates nor TLSConfig.GetCertificate are populated. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.

ListenAndServeTLS always returns a non-nil error. After Shutdown or Close, the returned error is ErrServerClosed.

func (*Server) Start

func (srv *Server) Start() error

Start starts the server, handling incoming requests.

Equivalent to ListenAndServe / ListenAndServeTLS without requiring the caller to choose, the server already knows whether TLS is enabled.

Accepted connections are configured to enable TCP keep-alives.

Start always returns a non-nil error. After Shutdown or Close, the returned error is ErrServerClosed.

Jump to

Keyboard shortcuts

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