Documentation ¶
Overview ¶
interacting jsonrpc in http family specs, currently jlibhttp provides 3 mechanisms: the classical http/1.1, websocket and http/2 wire protocol.
Index ¶
- Constants
- Variables
- func ListenAndServe(rootCtx context.Context, bind string, handler http.Handler, ...) error
- func Logger(r *http.Request) *log.Entry
- type Actor
- func (self *Actor) AddChild(child *Actor)
- func (self *Actor) Feed(req *RPCRequest) (jlib.Message, error)
- func (self Actor) GetSchema(method string) (jlibschema.Schema, bool)
- func (self *Actor) HandleClose(r *http.Request, session RPCSession)
- func (self Actor) Has(method string) bool
- func (self Actor) MethodList() []string
- func (self *Actor) Off(method string)
- func (self *Actor) On(method string, callback MsgCallback, setters ...HandlerSetter)
- func (self *Actor) OnClose(handler CloseCallback) error
- func (self *Actor) OnContext(method string, callback ContextedMsgCallback, setters ...HandlerSetter)
- func (self *Actor) OnMissing(handler MissingCallback) error
- func (self *Actor) OnRequest(method string, callback RequestCallback, setters ...HandlerSetter) error
- func (self *Actor) OnTyped(method string, typedHandler interface{}, setters ...HandlerSetter)
- func (self *Actor) OnTypedContext(method string, typedHandler interface{}, setters ...HandlerSetter) error
- func (self *Actor) OnTypedRequest(method string, typedHandler interface{}, setters ...HandlerSetter) error
- type AuthConfig
- type AuthHandler
- type AuthInfo
- type BasicAuthConfig
- type BearerAuthConfig
- type Client
- type ClientOptions
- type CloseCallback
- type CloseHandler
- type ConnectedHandler
- type ContextSpec
- type ContextedMsgCallback
- type FirstArgSpec
- type GatewayHandler
- type H1Client
- func (self *H1Client) Call(rootCtx context.Context, reqmsg *jlib.RequestMessage) (jlib.Message, error)
- func (self *H1Client) IsStreaming() bool
- func (self *H1Client) Send(rootCtx context.Context, msg jlib.Message) error
- func (self *H1Client) ServerURL() *url.URL
- func (self *H1Client) SetClientTLSConfig(cfg *tls.Config)
- func (self *H1Client) SetExtraHeader(h http.Header)
- func (self *H1Client) UnwrapCall(rootCtx context.Context, reqmsg *jlib.RequestMessage, output interface{}) error
- type H1Handler
- type H2Client
- type H2Handler
- type H2Session
- type HandlerSetter
- type HeaderFlags
- type JwtAuthConfig
- type MessageHandler
- type MethodHandler
- type MissingCallback
- type MsgCallback
- type RPCRequest
- type RPCSession
- type ReqSpec
- type RequestCallback
- type SimpleResponse
- type Streamable
- type StreamingClient
- func (self *StreamingClient) Call(rootCtx context.Context, reqmsg *jlib.RequestMessage) (jlib.Message, error)
- func (self *StreamingClient) ClientTLSConfig() *tls.Config
- func (self *StreamingClient) Close()
- func (self *StreamingClient) CloseChannel() chan error
- func (self *StreamingClient) Connect(rootCtx context.Context) error
- func (self *StreamingClient) Connected() bool
- func (self *StreamingClient) InitStreaming(serverUrl *url.URL, transport Transport)
- func (self *StreamingClient) IsStreaming() bool
- func (self *StreamingClient) Log() *log.Entry
- func (self *StreamingClient) OnClose(handler CloseHandler) error
- func (self *StreamingClient) OnConnected(handler ConnectedHandler) error
- func (self *StreamingClient) OnMessage(handler MessageHandler) error
- func (self *StreamingClient) Reset(err error)
- func (self *StreamingClient) Send(rootCtx context.Context, msg jlib.Message) error
- func (self *StreamingClient) ServerURL() *url.URL
- func (self *StreamingClient) SetClientTLSConfig(cfg *tls.Config)
- func (self *StreamingClient) SetExtraHeader(h http.Header)
- func (self *StreamingClient) UnwrapCall(rootCtx context.Context, reqmsg *jlib.RequestMessage, output interface{}) error
- func (self *StreamingClient) Wait() error
- type TLSConfig
- type Transport
- type WSClient
- type WSHandler
- type WSSession
- type WrappedResponse
Constants ¶
const ( TransportHTTP = "http" TransportWebsocket = "websocket" TransportHTTP2 = "http2" )
Variables ¶
var TransportClosed = errors.New("streaming closed")
var TransportConnectFailed = errors.New("connect refused")
errors
Functions ¶
func ListenAndServe ¶
Types ¶
type Actor ¶
type Actor struct { ValidateSchema bool RecoverFromPanic bool // contains filtered or unexported fields }
func (*Actor) Feed ¶
func (self *Actor) Feed(req *RPCRequest) (jlib.Message, error)
give the actor a request message
func (*Actor) HandleClose ¶
func (self *Actor) HandleClose(r *http.Request, session RPCSession)
call the close handler if possible
func (Actor) MethodList ¶
func (*Actor) On ¶
func (self *Actor) On(method string, callback MsgCallback, setters ...HandlerSetter)
register a method handler
func (*Actor) OnClose ¶
func (self *Actor) OnClose(handler CloseCallback) error
OnClose handler is called when the stream beneath the actor is closed
func (*Actor) OnContext ¶ added in v0.3.10
func (self *Actor) OnContext(method string, callback ContextedMsgCallback, setters ...HandlerSetter)
func (*Actor) OnMissing ¶
func (self *Actor) OnMissing(handler MissingCallback) error
register a callback called when no hander to handle a request message or non-request message met
func (*Actor) OnRequest ¶ added in v0.3.0
func (self *Actor) OnRequest(method string, callback RequestCallback, setters ...HandlerSetter) error
func (*Actor) OnTyped ¶
func (self *Actor) OnTyped(method string, typedHandler interface{}, setters ...HandlerSetter)
register a typed method handler
func (*Actor) OnTypedContext ¶ added in v0.3.10
func (self *Actor) OnTypedContext(method string, typedHandler interface{}, setters ...HandlerSetter) error
func (*Actor) OnTypedRequest ¶ added in v0.3.0
func (self *Actor) OnTypedRequest(method string, typedHandler interface{}, setters ...HandlerSetter) error
type AuthConfig ¶
type AuthConfig struct { Basic []BasicAuthConfig `yaml:"basic,omitempty" json:"basic,omitempty"` Bearer []BearerAuthConfig `yaml:"bearer,omitempty" json:"bearer,omitempty"` Jwt *JwtAuthConfig `yaml:"jwt,omitempty" json:"jwt,omitempty"` }
type AuthHandler ¶
type AuthHandler struct {
// contains filtered or unexported fields
}
Auth handler
func NewAuthHandler ¶
func NewAuthHandler(authConfig *AuthConfig, next http.Handler) *AuthHandler
func (*AuthHandler) ServeHTTP ¶
func (self *AuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type BasicAuthConfig ¶
type BearerAuthConfig ¶
type Client ¶
type Client interface { // Returns the server URL ServerURL() *url.URL // Call a Request message and expect a Result|Error message. Call(ctx context.Context, reqmsg *jlib.RequestMessage) (jlib.Message, error) // Call a Request message and unwrap the result message into a // given structure, when an Error message comes it is turned // into a golang error object typed *jlib.ErrorBody UnwrapCall(ctx context.Context, reqmsg *jlib.RequestMessage, output interface{}) error // Send a JSONRPC message(usually a notify) to server without // expecting any result. Send(ctx context.Context, msg jlib.Message) error // Set the client tls config SetClientTLSConfig(cfg *tls.Config) // Set http header SetExtraHeader(h http.Header) // Is streaming IsStreaming() bool }
Client is an abstract interface a client type must implement
func NewClient ¶
func NewClient(serverUrl string, optlist ...ClientOptions) (Client, error)
NewClient returns an JSONRPC client whose type depends on the server url it wants to connect to. Currently there are 3 types of supported url schemes: the HTTP/1.1 client, the websocket based client and HTTP2 base client, the latter two types are streaming clients which can accept server push messages.
type ClientOptions ¶ added in v0.4.2
type ClientOptions struct { // client request timeout Timeout int `json:"timeout" yaml:"timeout"` }
type CloseCallback ¶
type CloseCallback func(r *http.Request, session RPCSession)
type CloseHandler ¶
type CloseHandler func()
type ConnectedHandler ¶ added in v0.2.6
type ConnectedHandler func()
type ContextSpec ¶ added in v0.3.10
type ContextSpec struct{}
func (ContextSpec) Check ¶ added in v0.3.10
func (self ContextSpec) Check(firstArgType reflect.Type) bool
func (ContextSpec) String ¶ added in v0.3.10
func (self ContextSpec) String() string
func (ContextSpec) Value ¶ added in v0.3.10
func (self ContextSpec) Value(req *RPCRequest) interface{}
type ContextedMsgCallback ¶ added in v0.3.10
type FirstArgSpec ¶ added in v0.3.10
type FirstArgSpec interface { Check(firstArgType reflect.Type) bool Value(req *RPCRequest) interface{} String() string }
type GatewayHandler ¶
type GatewayHandler struct { Actor *Actor // contains filtered or unexported fields }
shared handler serve http1/http2/websocket server over the same port using http protocol detection.
NOTE: gateway handler must work over TLS to serve h2
func NewGatewayHandler ¶
func NewGatewayHandler(serverCtx context.Context, actor *Actor, insecure bool) *GatewayHandler
func (*GatewayHandler) ServeHTTP ¶
func (self *GatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type H1Client ¶
type H1Client struct {
// contains filtered or unexported fields
}
func NewH1Client ¶
func NewH1Client(serverUrl *url.URL, optlist ...ClientOptions) *H1Client
func (*H1Client) IsStreaming ¶
func (*H1Client) SetClientTLSConfig ¶
func (*H1Client) SetExtraHeader ¶
func (*H1Client) UnwrapCall ¶
type H2Client ¶
type H2Client struct { StreamingClient // use h2c UseH2C bool // contains filtered or unexported fields }
func NewH2Client ¶
func (*H2Client) HTTPClient ¶
type H2Handler ¶
type H2Handler struct { Actor *Actor // options SpawnGoroutine bool UseH2C bool // contains filtered or unexported fields }
func (*H2Handler) FallbackHandler ¶
func (*H2Handler) H2CHandler ¶
type HandlerSetter ¶
type HandlerSetter func(h *MethodHandler)
func WithSchema ¶
func WithSchema(s jlibschema.Schema) HandlerSetter
func WithSchemaJson ¶
func WithSchemaJson(jsonSchema string) HandlerSetter
func WithSchemaYaml ¶
func WithSchemaYaml(yamlSchema string) HandlerSetter
type HeaderFlags ¶
type HeaderFlags []string
func (*HeaderFlags) Set ¶
func (self *HeaderFlags) Set(value string) error
func (*HeaderFlags) String ¶
func (self *HeaderFlags) String() string
type JwtAuthConfig ¶
type JwtAuthConfig struct {
Secret string `yaml:"secret" json:"secret"`
}
type MessageHandler ¶
type MethodHandler ¶
type MethodHandler struct {
// contains filtered or unexported fields
}
With method handler
type MissingCallback ¶
type MissingCallback func(req *RPCRequest) (interface{}, error)
type MsgCallback ¶ added in v0.3.0
type MsgCallback func(params []interface{}) (interface{}, error)
type RPCRequest ¶
type RPCRequest struct {
// contains filtered or unexported fields
}
http rpc quest structure
func NewRPCRequest ¶
func (RPCRequest) Context ¶
func (self RPCRequest) Context() context.Context
func (RPCRequest) Data ¶
func (self RPCRequest) Data() interface{}
func (RPCRequest) HttpRequest ¶
func (self RPCRequest) HttpRequest() *http.Request
func (RPCRequest) Log ¶
func (self RPCRequest) Log() *log.Entry
func (RPCRequest) Msg ¶
func (self RPCRequest) Msg() jlib.Message
func (RPCRequest) Session ¶
func (self RPCRequest) Session() RPCSession
type RPCSession ¶
type ReqSpec ¶ added in v0.3.10
type ReqSpec struct{}
func (ReqSpec) Value ¶ added in v0.3.10
func (self ReqSpec) Value(req *RPCRequest) interface{}
type RequestCallback ¶ added in v0.3.0
type RequestCallback func(req *RPCRequest, params []interface{}) (interface{}, error)
handler func
type SimpleResponse ¶
Simple HTTP response to instant return
func (SimpleResponse) Error ¶
func (self SimpleResponse) Error() string
type Streamable ¶
type Streamable interface { Client Connect(ctx context.Context) error OnConnected(handler ConnectedHandler) error OnMessage(handler MessageHandler) error OnClose(handler CloseHandler) error Wait() error }
type StreamingClient ¶
type StreamingClient struct {
// contains filtered or unexported fields
}
func (*StreamingClient) Call ¶
func (self *StreamingClient) Call(rootCtx context.Context, reqmsg *jlib.RequestMessage) (jlib.Message, error)
func (*StreamingClient) ClientTLSConfig ¶
func (self *StreamingClient) ClientTLSConfig() *tls.Config
func (*StreamingClient) Close ¶
func (self *StreamingClient) Close()
func (*StreamingClient) CloseChannel ¶
func (self *StreamingClient) CloseChannel() chan error
func (*StreamingClient) Connect ¶
func (self *StreamingClient) Connect(rootCtx context.Context) error
func (*StreamingClient) Connected ¶
func (self *StreamingClient) Connected() bool
func (*StreamingClient) InitStreaming ¶
func (self *StreamingClient) InitStreaming(serverUrl *url.URL, transport Transport)
func (*StreamingClient) IsStreaming ¶
func (self *StreamingClient) IsStreaming() bool
func (*StreamingClient) Log ¶
func (self *StreamingClient) Log() *log.Entry
func (*StreamingClient) OnClose ¶
func (self *StreamingClient) OnClose(handler CloseHandler) error
func (*StreamingClient) OnConnected ¶ added in v0.2.6
func (self *StreamingClient) OnConnected(handler ConnectedHandler) error
func (*StreamingClient) OnMessage ¶
func (self *StreamingClient) OnMessage(handler MessageHandler) error
func (*StreamingClient) Reset ¶
func (self *StreamingClient) Reset(err error)
func (*StreamingClient) ServerURL ¶
func (self *StreamingClient) ServerURL() *url.URL
func (*StreamingClient) SetClientTLSConfig ¶
func (self *StreamingClient) SetClientTLSConfig(cfg *tls.Config)
func (*StreamingClient) SetExtraHeader ¶
func (self *StreamingClient) SetExtraHeader(h http.Header)
func (*StreamingClient) UnwrapCall ¶
func (self *StreamingClient) UnwrapCall(rootCtx context.Context, reqmsg *jlib.RequestMessage, output interface{}) error
func (*StreamingClient) Wait ¶
func (self *StreamingClient) Wait() error
wait connection close and return error
type TLSConfig ¶
func (*TLSConfig) ValidateValues ¶
type Transport ¶
type Transport interface { Connect(rootCtx context.Context, serverUrl *url.URL, header http.Header) error Close() Connected() bool ReadMessage() (msg jlib.Message, readed bool, err error) WriteMessage(msg jlib.Message) error }
the underline transport, currently there are websocket and gRPC implementations
type WSHandler ¶
type WrappedResponse ¶
errors non standard Response returned by endpoints
func (WrappedResponse) Error ¶
func (self WrappedResponse) Error() string