Documentation ¶
Overview ¶
Package socket provides common implementations for local APIs that use HTTP over Unix domain sockets.
Index ¶
- func AuthMiddleware(token string, errorLogf func(f string, v ...any)) func(http.Handler) http.Handler
- func Available() bool
- func GenerateToken(len int) (string, error)
- func HeadersMiddleware(headers http.Header) func(http.Handler) http.Handler
- func LoggerMiddleware(prefix string, logf func(f string, v ...any)) func(http.Handler) http.Handler
- func WriteError(w http.ResponseWriter, err any, code int) error
- type APIErr
- type Client
- type ErrorResponse
- type Server
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AuthMiddleware ¶ added in v3.47.0
func AuthMiddleware(token string, errorLogf func(f string, v ...any)) func(http.Handler) http.Handler
AuthMiddleware is a middleware that checks the Authorization header of an incoming request for a Bearer token and checks that that token is the correct one. If there is an error while responding with an auth failure, it is logged with errorLogf.
func GenerateToken ¶
GenerateToken generates a new random token that contains approximately 8*len bits of entropy.
func HeadersMiddleware ¶
HeadersMiddleware is a middleware that sets common headers for all responses.
func LoggerMiddleware ¶
LoggerMiddleware logs all requests (method, URL, and handle time) with the formatted logging function (logf).
func WriteError ¶ added in v3.47.0
func WriteError(w http.ResponseWriter, err any, code int) error
WriteError writes an error as an ErrorResponse (JSON-encoded). The err value is converted to a string with fmt.Sprint.
Types ¶
type APIErr ¶
APIErr is an error type used for API error responses (the API returned a well-formed JSON ErrorResponse and a non-200 status code).
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a client for a HTTP-over-Unix Domain Socket API.
func NewClient ¶
NewClient creates a new Client. The context is used for an internal check that the socket can be dialled.
func (*Client) Do ¶
Do implements the common bits of an API call. req is serialised to JSON and passed as the request body if not nil. The method is called, with the token added in the Authorization header. The response is deserialised, either into the object passed into resp if the status is 200 OK, otherwise into an error.
type ErrorResponse ¶
type ErrorResponse struct {
Error string `json:"error"`
}
Error response is the response body for any errors that occur
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server hosts a HTTP server on a Unix domain socket.
func NewServer ¶
NewServer creates a server that, when started, will listen on a socket at the given path.