Documentation
¶
Index ¶
- func AllowCORS(next http.Handler) http.Handler
- func BadRequest(w http.ResponseWriter, r *http.Request, message *string, ...)
- func HealthCheckHandler(w http.ResponseWriter, r *http.Request)
- func InternalServerError(w http.ResponseWriter, r *http.Request)
- func NoContent(w http.ResponseWriter, r *http.Request)
- func NotFound(w http.ResponseWriter, r *http.Request, message string)
- func NotFoundHandler(w http.ResponseWriter, r *http.Request)
- func OK(w http.ResponseWriter, r *http.Request, data any)
- func Unauthorized(w http.ResponseWriter, r *http.Request, message string, ...)
- type HandleFunc
- type HealthCheckResponse
- type Options
- type Server
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BadRequest ¶
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 NotFoundHandler ¶
func NotFoundHandler(w http.ResponseWriter, r *http.Request)
NotFoundHandler handles requests to non-existent resources.
func OK ¶
func OK(w http.ResponseWriter, r *http.Request, data any)
OK sends a 200 OK response with the given data.
func Unauthorized ¶
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
// Middlewares is a slice of http.Handler that can be used to apply
// middleware to the HTTP server. These middlewares will be applied
// in the order they are provided.
Middlewares []middleware.Middleware
}
Options contains arguments to configure a Server instance.
type 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 ¶
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.