http

package module
v0.2.0-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2025 License: MIT Imports: 5 Imported by: 1

README

http

A lightweight and reusable Go package that simplifies the setup of basic net/http configurations, eliminating the need for redundant reimplementation. Streamline your HTTP server setup with ease.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllowCORS

func AllowCORS(next http.Handler) http.Handler

AllowCORS is a middleware that sets the CORS headers

func BadRequest

func BadRequest(w http.ResponseWriter, r *http.Request, message *string, errors map[string]string)

BadRequest sends a 400 Bad Request response with the given message and errors.

func HealthCheckHandler

func HealthCheckHandler(w http.ResponseWriter, r *http.Request)

HealthCheckHandler handles HTTP requests for checking the health status of the server.

func InternalServerError

func InternalServerError(w http.ResponseWriter, r *http.Request)

InternalServerError sends a 500 Internal Server Error response.

func NoContent

func NoContent(w http.ResponseWriter, r *http.Request)

NoContent sends a 204 No Content response.

func NotFound

func NotFound(w http.ResponseWriter, r *http.Request, message string)

NotFound sends a 404 Not Found response with the given message.

func OK

func OK(w http.ResponseWriter, r *http.Request, data any)

OK sends a 200 OK response with the given data.

func Unauthorized

func Unauthorized(w http.ResponseWriter, r *http.Request, message string, errors map[string]string)

Unauthorized sends a 401 Unauthorized response with the given message and errors.

Types

type HandleFunc

type HandleFunc struct {
	Pattern string
	Handler http.HandlerFunc
}

HandleFunc is a struct that contains the pattern and the handler function.

type HealthCheckResponse

type HealthCheckResponse struct {
	Status string `json:"status"`
}

HealthCheckResponse represents the structure of the health check response

type Options

type Options struct {
	// Addr optionally specifies the TCP address for the server to listen on,
	// in the form "host:port". If empty, ":http" (port 80) is used.
	// The service names are defined in RFC 6335 and assigned by IANA.
	// See net.Dial for details of the address format.
	Addr string

	// ReadTimeout is the maximum duration for reading the entire
	// request, including the body. A zero or negative value means
	// there will be no timeout.
	//
	// Because ReadTimeout does not let Handlers make per-request
	// decisions on each request body's acceptable deadline or
	// upload rate, most users will prefer to use
	// ReadHeaderTimeout. It is valid to use them both.
	ReadTimeout time.Duration

	// WriteTimeout is the maximum duration before timing out
	// writes of the response. It is reset whenever a new
	// request's header is read. Like ReadTimeout, it does not
	// let Handlers make decisions on a per-request basis.
	// A zero or negative value means there will be no timeout.
	WriteTimeout time.Duration

	// IdleTimeout is the maximum amount of time to wait for the
	// next request when keep-alives are enabled. If zero, the value
	// of ReadTimeout is used. If negative, or if zero and ReadTimeout
	// is zero or negative, there is no timeout.
	IdleTimeout time.Duration
}

Options contains arguments to configure a Server instance.

type Server

type Server struct {
	HttpServer *http.Server
}

Server provides a convenient wrapper around the standard library's http.Server.

func New

func New(opts Options, endpoints ...HandleFunc) *Server

NewServer creates a new Server instance with the given endpoints and options.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the server without interrupting any active connections. 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 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 Listener(s).

When Shutdown is called, [Serve], [ListenAndServe], and [ListenAndServeTLS] immediately return [ErrServerClosed]. Make sure the program doesn't exit and waits instead for Shutdown to return.

Shutdown does not attempt to close nor wait for hijacked connections such as WebSockets. The caller of Shutdown should separately notify such long-lived connections of shutdown and wait for them to close, if desired. See [Server.RegisterOnShutdown] for a way to register shutdown notification functions.

Once Shutdown has been called on a server, it may not be reused; future calls to methods such as Serve will return ErrServerClosed.

Jump to

Keyboard shortcuts

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