Documentation ¶
Overview ¶
interacting jsonrpc in http family specs, currently jsoffnet 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) (jsoff.Message, error)
- func (self Actor) GetSchema(method string) (jsoffschema.Schema, bool)
- func (self *Actor) HandleClose(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 HandlerSetter
- type HeaderFlags
- type Http1Client
- func (self *Http1Client) Call(rootCtx context.Context, reqmsg *jsoff.RequestMessage) (jsoff.Message, error)
- func (self *Http1Client) IsStreaming() bool
- func (self *Http1Client) Send(rootCtx context.Context, msg jsoff.Message) error
- func (self *Http1Client) ServerURL() *url.URL
- func (self *Http1Client) SetClientTLSConfig(cfg *tls.Config)
- func (self *Http1Client) SetExtraHeader(h http.Header)
- func (self *Http1Client) UnwrapCall(rootCtx context.Context, reqmsg *jsoff.RequestMessage, output interface{}) error
- type Http1Handler
- type Http2Client
- type Http2Handler
- type Http2Session
- type JwtAuthConfig
- type MessageHandler
- type MethodHandler
- type MissingCallback
- type MsgCallback
- type RPCRequest
- func (self RPCRequest) Context() context.Context
- func (self RPCRequest) Data() interface{}
- func (self RPCRequest) HttpRequest() *http.Request
- func (self RPCRequest) Log() *log.Entry
- func (self RPCRequest) Msg() jsoff.Message
- func (self RPCRequest) Session() RPCSession
- func (self *RPCRequest) WithHTTPRequest(r *http.Request) *RPCRequest
- func (self *RPCRequest) WithSession(session RPCSession) *RPCRequest
- type RPCSession
- type ReqSpec
- type RequestCallback
- type SimpleResponse
- type Streamable
- type StreamingClient
- func (self *StreamingClient) Call(rootCtx context.Context, reqmsg *jsoff.RequestMessage) (jsoff.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 jsoff.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 *jsoff.RequestMessage, output interface{}) error
- func (self *StreamingClient) Wait() error
- type TCPClient
- type TCPServer
- type TCPSession
- type TLSConfig
- type Transport
- type VsockClient
- type VsockServer
- type VsockSession
- type WSClient
- type WSHandler
- type WSSession
- type WrappedResponse
Constants ¶
const ( TransportHTTP = "http" TransportWebsocket = "websocket" TransportHTTP2 = "http2" TransportTCP = "tcp" TransportVsock = "vsock" )
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) (jsoff.Message, error)
give the actor a request message
func (*Actor) HandleClose ¶
func (self *Actor) HandleClose(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 ¶
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 ¶
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 ¶
func (self *Actor) OnTypedContext(method string, typedHandler interface{}, setters ...HandlerSetter) error
func (*Actor) OnTypedRequest ¶
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 *jsoff.RequestMessage) (jsoff.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 *jsoff.ErrorBody UnwrapCall(ctx context.Context, reqmsg *jsoff.RequestMessage, output interface{}) error // Send a JSONRPC message(usually a notify) to server without // expecting any result. Send(ctx context.Context, msg jsoff.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 ¶
type ClientOptions struct { // client request timeout Timeout int `json:"timeout" yaml:"timeout"` }
type CloseCallback ¶
type CloseCallback func(session RPCSession)
type CloseCallback func(r *http.Request, session RPCSession)
type CloseHandler ¶
type CloseHandler func()
type ConnectedHandler ¶
type ConnectedHandler func()
type ContextSpec ¶
type ContextSpec struct{}
func (ContextSpec) String ¶
func (self ContextSpec) String() string
func (ContextSpec) Value ¶
func (self ContextSpec) Value(req *RPCRequest) interface{}
type ContextedMsgCallback ¶
type FirstArgSpec ¶
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 HandlerSetter ¶
type HandlerSetter func(h *MethodHandler)
func WithSchema ¶
func WithSchema(s jsoffschema.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 Http1Client ¶
type Http1Client struct {
// contains filtered or unexported fields
}
func NewHttp1Client ¶
func NewHttp1Client(serverUrl *url.URL, optlist ...ClientOptions) *Http1Client
func (*Http1Client) Call ¶
func (self *Http1Client) Call(rootCtx context.Context, reqmsg *jsoff.RequestMessage) (jsoff.Message, error)
func (*Http1Client) IsStreaming ¶
func (self *Http1Client) IsStreaming() bool
func (*Http1Client) ServerURL ¶
func (self *Http1Client) ServerURL() *url.URL
func (*Http1Client) SetClientTLSConfig ¶
func (self *Http1Client) SetClientTLSConfig(cfg *tls.Config)
func (*Http1Client) SetExtraHeader ¶
func (self *Http1Client) SetExtraHeader(h http.Header)
func (*Http1Client) UnwrapCall ¶
func (self *Http1Client) UnwrapCall(rootCtx context.Context, reqmsg *jsoff.RequestMessage, output interface{}) error
type Http1Handler ¶
type Http1Handler struct {
Actor *Actor
}
func NewHttp1Handler ¶
func NewHttp1Handler(actor *Actor) *Http1Handler
func (*Http1Handler) ServeHTTP ¶
func (self *Http1Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
func (Http1Handler) WriteMessage ¶ added in v0.5.8
func (self Http1Handler) WriteMessage(w http.ResponseWriter, msg jsoff.Message, code int)
type Http2Client ¶
type Http2Client struct { StreamingClient // use h2c UseHttp2C bool // contains filtered or unexported fields }
func NewHttp2Client ¶
func NewHttp2Client(serverUrl *url.URL) *Http2Client
func (*Http2Client) HTTPClient ¶
func (self *Http2Client) HTTPClient() *http.Client
func (*Http2Client) String ¶
func (self *Http2Client) String() string
type Http2Handler ¶
type Http2Handler struct { Actor *Actor // options SpawnGoroutine bool UseHttp2C bool // contains filtered or unexported fields }
func NewHttp2Handler ¶
func NewHttp2Handler(serverCtx context.Context, actor *Actor) *Http2Handler
func (*Http2Handler) FallbackHandler ¶
func (self *Http2Handler) FallbackHandler() *Http1Handler
func (*Http2Handler) Http2CHandler ¶
func (self *Http2Handler) Http2CHandler() http.Handler
func (*Http2Handler) ServeHTTP ¶
func (self *Http2Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type Http2Session ¶
type Http2Session struct {
// contains filtered or unexported fields
}
func (Http2Session) Context ¶ added in v0.5.4
func (self Http2Session) Context() context.Context
func (*Http2Session) Send ¶
func (self *Http2Session) Send(msg jsoff.Message)
func (Http2Session) SessionID ¶
func (self Http2Session) SessionID() 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 ¶
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() jsoff.Message
func (RPCRequest) Session ¶
func (self RPCRequest) Session() RPCSession
func (*RPCRequest) WithHTTPRequest ¶
func (self *RPCRequest) WithHTTPRequest(r *http.Request) *RPCRequest
func (*RPCRequest) WithSession ¶
func (self *RPCRequest) WithSession(session RPCSession) *RPCRequest
type RPCSession ¶
type ReqSpec ¶
type ReqSpec struct{}
func (ReqSpec) Value ¶
func (self ReqSpec) Value(req *RPCRequest) interface{}
type RequestCallback ¶
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 *jsoff.RequestMessage) (jsoff.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 ¶
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 *jsoff.RequestMessage, output interface{}) error
func (*StreamingClient) Wait ¶
func (self *StreamingClient) Wait() error
wait connection close and return error
type TCPSession ¶
type TCPSession struct {
// contains filtered or unexported fields
}
tcp session implements RPCSession
func (TCPSession) Context ¶ added in v0.5.4
func (self TCPSession) Context() context.Context
func (*TCPSession) Send ¶
func (self *TCPSession) Send(msg jsoff.Message)
func (TCPSession) SessionID ¶
func (self TCPSession) SessionID() string
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 jsoff.Message, readed bool, err error) WriteMessage(msg jsoff.Message) error }
the underline transport, currently there are websocket, h2 and net socket implementations
type VsockClient ¶
type VsockClient struct {
StreamingClient
}
func NewVsockClient ¶
func NewVsockClient(serverUrl *url.URL) *VsockClient
func (*VsockClient) String ¶
func (self *VsockClient) String() string
type VsockServer ¶
type VsockServer struct { Actor *Actor // contains filtered or unexported fields }
func NewVsockServer ¶
func NewVsockServer(serverCtx context.Context, actor *Actor) *VsockServer
func (*VsockServer) Start ¶
func (self *VsockServer) Start(rootCtx context.Context, port uint32) error
func (*VsockServer) Stop ¶ added in v0.5.7
func (self *VsockServer) Stop()
type VsockSession ¶
type VsockSession struct {
// contains filtered or unexported fields
}
vsock session implements RPCSession
func (VsockSession) Context ¶ added in v0.5.4
func (self VsockSession) Context() context.Context
func (*VsockSession) Send ¶
func (self *VsockSession) Send(msg jsoff.Message)
func (VsockSession) SessionID ¶
func (self VsockSession) SessionID() string
type WSHandler ¶
type WrappedResponse ¶
errors non standard Response returned by endpoints
func (WrappedResponse) Error ¶
func (self WrappedResponse) Error() string