Documentation
¶
Overview ¶
Package srpc implements a simple RPC-over-HTTP library.
Index ¶
- Constants
- Variables
- type Codec
- type Endpoint
- func NewEndpoint[Response, Request any](method, path string, resc Codec[Response], reqc Codec[Request]) Endpoint[Response, Request]
- func NewEndpointJSON[Response, Request any](method, path string) Endpoint[Response, Request]
- func NewEndpontSeq[Response, Request any](method, path string) Endpoint[iter.Seq2[Response, error], Request]
- type EndpointR
- type EndpointW
- type ErrorResponse
- type Mux
- type Procedure
- type ProcedureR
- type ProcedureW
- type Transport
- type Validable
- type WireError
Constants ¶
const QueryKey = "srpc"
QueryKey is the key for the query parameter that sRPC will use to issue state-preserving requests.
Variables ¶
var ErrBadOrigin = errors.New("bad connector origin")
ErrBadOrigin is returned when a bad origin is given to construct a Connector.
Functions ¶
This section is empty.
Types ¶
type Codec ¶
type Codec[T any] struct { // ContentType is the HTTP Content-Type header to use for responses. ContentType string // KeepOpen tells this library to not close streams after client calls return. KeepOpen bool // Co encodes the given value to the returned io.Reader. // // Implementers have the guarantee that the returned reader will be copied with // io.Copy to the response writer, or used as request body/query on the client side. Co func(ctx context.Context, t T) (io.Reader, error) // Dec decodes data from a stream. Dec func(ctx context.Context, r io.Reader) (T, error) }
Codec implements functions to enCode and Decode requests and responses.
Ideally
var value T wire, _ := myCodec.Co(ctx, value) got := myCodec.Dec(ctx, wire) got == value
Should be true for every possible value of T.
func NewCodecJSON ¶
NewCodecJSON creates a new Codec that uses JSON as wire format.
type Endpoint ¶
type Endpoint[Response, Request any] struct { // contains filtered or unexported fields }
Endpoint represents a single procedure.
Both the client side and the server side can be constructed from this type.
func NewEndpoint ¶
func NewEndpoint[Response, Request any](method, path string, resc Codec[Response], reqc Codec[Request]) Endpoint[Response, Request]
NewEndpoint constructs a new endpoint with the given codecs.
func NewEndpointJSON ¶
NewEndpointJSON constructs an endpoint with the JSON codec.
func NewEndpontSeq ¶
func NewEndpontSeq[Response, Request any](method, path string) Endpoint[iter.Seq2[Response, error], Request]
NewEndpontSeq constructs and endpoint with JSON request and Seq response.
func (*Endpoint[Response, Request]) Register ¶
Register registers the endpoint on the mux, implemented by the procedure.
func (*Endpoint[Response, Request]) Remote ¶
Remote returns the remote procedure, ready to be called.
The endpoint needs to be registered and served on the remote server.
func (*Endpoint[Response, Request]) RemoteWithOrigin ¶
RemoteWithOrigin is like Remote, but it creates a transport for the given origin.
If the origin is invalid, RemoteWithOrigin panics.
type EndpointR ¶
EndpointR is like Endpoint for functions that take no inputs.
func (*EndpointR[Response]) Register ¶
Register is like Endpoint.Register for EndpointR.
func (*EndpointR[Response]) Remote ¶
Remote is like Endpoint.Remote for EndpointR.
func (*EndpointR[Response]) RemoteWithOrigin ¶
RemoteWithOrigin is like Endpoint.RemoteWithOrigin for EndpointR.
type EndpointW ¶
EndpointW is like Endpoint but for functions that return no content.
func (*EndpointW[Request]) Register ¶
Register is like Endpoint.Register for EndpointW.
func (*EndpointW[Request]) Remote ¶
Remote is like Endpoint.Remote for EndpointW.
func (*EndpointW[Request]) RemoteWithOrigin ¶
RemoteWithOrigin is like Endpoint.RemoteWithOrigin for EndpointW.
type ErrorResponse ¶
ErrorResponse can be returned by handlers to control the error code when returning an error.
type Mux ¶
type Mux interface {
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
}
Mux is the subset of *http.ServeMux that srpc requires to work.
type ProcedureR ¶
ProcedureR is like Procedure for functions that take no parameters.
type ProcedureW ¶
ProcedureW is like Procedure, but for functions that return no value.
type Transport ¶
type Transport struct {
// contains filtered or unexported fields
}
Transport can be used to connect to a remote Endpoint.
type Validable ¶
type Validable interface {
Validate() error
}
Validable represents all requests that can be validated.
All requests that implement this and that are not valid will be automatically rejected.
IMPORTANT NOTE: the validation error is returned to the client, so it should be telling enough for a user to understand what is wrong, but it should not contain internal or secret information.