Documentation
¶
Overview ¶
Package httpsrv provides a default set of configuration for hosting a http server in a service.
Example ¶
package main
import (
"context"
"net/http"
"time"
"github.com/LUSHDigital/core/workers/httpsrv"
)
var (
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
return
})
ctx context.Context
)
func main() {
go httpsrv.New(&http.Server{
Handler: handler,
ReadTimeout: 1 * time.Second,
}).Run(ctx)
}
Index ¶
Examples ¶
Constants ¶
View Source
const (
// Port is the default HTTP port.
Port = 80
)
Variables ¶
View Source
var ( // NotFoundHandler responds with the default a 404 response. NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { res := &rest.Response{ Code: http.StatusNotFound, Message: http.StatusText(http.StatusNotFound), } res.WriteTo(w) }) // DefaultHTTPServer represents the default configuration for the http server DefaultHTTPServer = http.Server{ WriteTimeout: 5 * time.Second, ReadTimeout: 5 * time.Second, IdleTimeout: 5 * time.Second, ReadHeaderTimeout: 1 * time.Second, } // DefaultCORS contains the default CORS settings used when starting an httpsrv.Server DefaultCORS = CORS{ AllowOrigin: "*", AllowHeaders: []string{ "Authorization", "Origin", "Accept", "Content-Type", "X-Requested-With", }, AllowMethods: []string{ http.MethodGet, http.MethodHead, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, }, } )
Functions ¶
func CORSHandler ¶ added in v0.23.3
func CORSHandler(c CORS, next http.Handler) http.HandlerFunc
CORSHandler updates headers on CORS preflight requests and actual CORS requests.
func HealthHandler ¶
func HealthHandler(now func() time.Time) http.HandlerFunc
HealthHandler responds with service health.
func WrapperHandler ¶
WrapperHandler returns the wrapper handler for the http server.
Types ¶
type CORS ¶ added in v0.23.3
CORS defines a struct which allows configuring the CORS settings for httpsrv.Server
type HealthResponse ¶
type HealthResponse struct {
Latency string `json:"latency"`
StackInUse string `json:"stack_in_use"`
HeapInUse string `json:"heap_in_use"`
HeapAlloc string `json:"heap_alloc"`
NumGoRoutines int `json:"num_go_routines"`
}
HealthResponse contains information about the service health.
type Server ¶
type Server struct {
Server *http.Server
CORS CORS
Now func() time.Time
// contains filtered or unexported fields
}
Server represents a collection of functions for starting and running an RPC server.
func NewDefault ¶ added in v0.5.0
NewDefault returns a http server
Click to show internal directories.
Click to hide internal directories.