# README
RESTful
Quick introduction
This Go package is a powerful extension of standard Go HTTP server and client libraries. It lets you handle HTTP+JSON without any extra code.
Lambda server
You receive and respond with data structures.
type reqData struct{
Num int `json:"num" validate:"lt=1000000"`
}
type respData struct{
Number int `json:"number"`
}
func create(ctx context.Context, req *reqData) (*respData, error) {
// You use data structures directly, without marshalling and unmarshalling.
resp := respData{Number: reqData.Num}
return &respData, nil
}
func main() {
restful.HandleFunc("/user/v1", create).Methods(http.MethodPost)
restful.Start()
}
RESTful client
You send and receive data structures.
location, err := restful.Post(ctx, "https://example.com", &reqData, &respData)
Details
- Lambda server Focus on business logic. It is a modern variant of an HTTP server.
- RESTful server An underlying HTTP server of Lambda. An HTTP server with goodies. Besides helper functions for receiving and sending JSON data and it can do logging. Router is based on Gorilla/Mux, offering similar services.
- RESTful client Sending GET, POST (and receiving Location), PUT, PATCH or DELETE requests and receiving their responses. And numerous other helper functions.
- Tracing information is propagated in context, received in Lambda and used in client request. That is all done without any extra coding on your side. Based on OpenTelemetry.
- Monitor is a convenient middleware solution to pre-process requests and post-process responses. Pre and post hooks can be used for whatever you want, such as adding Prometheus counters on router level, without littering your business logic.
- Error is a Go error object containing HTTP status code besides traditional error.
Trace context and error are the glue between Lambda and Client. That is why they form a module together.
Principles
- Simple, intuitive, Go-ish.
- Similar to Go's built-in http package, with some advanced router inherited from Gorilla/Mux project.
- Powerful HTTP+JSON framework reducing development costs while improving quality.
- Have quite many goodies needed on developing complex applications.
# Packages
No description provided by the author
No description provided by the author
No description provided by the author
# Functions
AddLambdaToContext returns the context extended with value of the Lambda.
BaseContentType returns the MIME type of the Content-Type header as lower-case string E.g.: "application/JSON; charset=ISO-8859-1" --> "application/json".
Delete deletes a resource.
DetailError adds further description to the error.
Get gets a resource.
GetBaseContentType returns base content type from HTTP header.
GetDataBytes returns []byte received.
GetDataBytesForContentType returns []byte received, if Content-Type is matching or empty string.
GetErrBody returns unprocessed body of a response with error status.
GetErrStatusCode returns status code of error response.
GetErrStatusCodeElse returns status code of error response, if available.
GetRequestData returns request data from HTTP request.
GetResponseData returns response data from JSON body of HTTP response.
HandleFunc assigns an HTTP path template to a function.
IsConnectError determines if error is due to failed connection.
L returns lambda-related data from context.
LambdaWrap wraps a Lambda function and makes it a http.HandlerFunc.
ListenAndServe acts like standard http.ListenAndServe().
ListenAndServeMTLS acts like standard http.ListenAndServeTLS().
ListenAndServeTLS acts like standard http.ListenAndServeTLS().
Logger wraps original handler and returns a handler that logs.
Monitor wraps handler function, creating a middleware in a safe and convenient fashion.
NewCertPool adds PEM certificates from given path in a way that is usable at TLS() as RootCAs.
NewClient creates a RESTful client instance.
NewClientWInterface creates a RESTful client instance bound to that network interface.
NewDetailedError creates a new error with specified problem details JSON structure (RFC7807).
NewError creates a new error that contains HTTP status code.
NewH2CClient creates a RESTful client instance, forced to use HTTP2 Cleartext (H2C).
NewH2Client creates a RESTful client instance, forced to use HTTP2 with TLS (H2) (a.k.a.
NewRequestCtx adds request related data to r.Context().
NewRouter creates new Router instance.
NewServer creates a new Server instance.
NewTestCtx helps creating tests.
Patch partially updates a resource.
PingList sends a HTTP GET requests to a list of URLs and expects 2xx responses for each.
Post sends a POST request.
Put updates a resource.
SanitizeJSONBytes clears empty entries from byte array of JSON.
SanitizeJSONString clears empty entries from string of JSON.
SendEmptyResponse sends an empty HTTP response.
SendJSONResponse sends an HTTP response with an optionally sanitized JSON data.
SendLocationResponse sends an empty "201 Created" HTTP response with Location header.
SendProblemDetails adds detailed problem description to JSON body, if available.
SendProblemResponse sends response with problem text, and extends it to problem+json format if it is a plain string.
SendResp sends an HTTP response with data.
SendResponse sends an HTTP response with a JSON data.
SetOTel enables/disables Open Telemetry.
SetOTelGrpc enables Open Telemetry.
SetServerName allows settings a server name.
SetTrace can enable/disable HTTP tracing.
Start starts serving on port 8080 (AddrHTTP).
StartTLS starts serving for TLS on port 8443 (AddrHTTPS) and for cleartext on port 8080 (AddrHTTP), if allowed.
# Constants
ContentType strings.
ContentType strings.
ContentType strings.
ContentType strings.
ContentType strings.
ContentType strings.
RFC 7386.
ContentType strings.
ContentType strings.
RFC 6902.
RFC 7807.
GrantClientCredentials represents oauth2 client credentials grant.
GrantPasswordCredentials represents oauth2 password credentials grant.
GrantRefreshToken represents oauth2 refresh token grant.
Kind is a string representation of what kind the client is.
Kind is a string representation of what kind the client is.
Kind is a string representation of what kind the client is.
# Variables
AddrHTTP is the listening address / port of HTTP for Start / StartTLS.
AddrHTTPS is the listening address / port of HTTPS for StartTLS.
ClientCAs is a path of client certificate authorities, to be verified by the server on StartTLS on mTLS.
DefaultServeMux is the default HTTP mux served.
DefaultTokenClient is an http.Client used to obtain OAuth2 token.
ErrNonHTTPSURL means that using non-https URL not allowed.
ErrUnexpectedContentType is returned if content-type is unexpected.
HealthCheckPath is the path of health checking, such as liveness and readiness probes.
LambdaMaxBytesToParse defines the maximum length of request content allowed to be parsed.
LambdaSanitizeJSON defines whether to sanitize JSON of Lambda return or SendResp.
LambdaValidator tells if incoming request is to be validated.
LivenessProbePath is the path of liveness probes.
OwnTLSCert is the own TLS certificate used by server on StartTLS.
OwnTLSKey is the own TLS private key used by servert on StartTLS.
ReadinessProbePath is the path of readiess probes.
ServerReadHeaderTimeout is the amount of time allowed to read request headers.
ServerReadTimeout is the amount of time allowed to read request body.
# Structs
Client is an instance of RESTful client.
HTTPSConfig contains some flags that control what kind of URLs to be allowed to be used.
InvalidParam is the common InvalidParam object defined in 3GPP TS 29.571.
ProblemDetails is a structure defining fields for RFC 7807 error responses.
Route ...
Router routes requests to lambda functions.
Server represents a server instance.
# Type aliases
ClientMonitorFuncPost is a type of user defined function to be called after the response is received.
ClientMonitorFuncPre is a type of user defined function to be called before the request is sent.
Grant represents the flow how oauth2 access tokens are retreived.
MonitorFuncPost is a type of user defined function to be called after the request was served.
MonitorFuncPre is a type of user defined function to be called before the request is served.