jlibhttp

package
v0.4.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 2, 2023 License: MIT Imports: 25 Imported by: 0

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

View Source
const (
	TransportHTTP      = "http"
	TransportWebsocket = "websocket"
	TransportHTTP2     = "http2"
)

Variables

View Source
var TransportClosed = errors.New("streaming closed")
View Source
var TransportConnectFailed = errors.New("connect refused")

errors

Functions

func ListenAndServe

func ListenAndServe(rootCtx context.Context, bind string, handler http.Handler, tlsConfigs ...*TLSConfig) error

func Logger

func Logger(r *http.Request) *log.Entry

log attaching remoteAddr

Types

type Actor

type Actor struct {
	ValidateSchema   bool
	RecoverFromPanic bool
	// contains filtered or unexported fields
}

func NewActor

func NewActor() *Actor

func (*Actor) AddChild

func (self *Actor) AddChild(child *Actor)

func (*Actor) Feed

func (self *Actor) Feed(req *RPCRequest) (jlib.Message, error)

give the actor a request message

func (Actor) GetSchema

func (self Actor) GetSchema(method string) (jlibschema.Schema, bool)

get the schema of a method

func (*Actor) HandleClose

func (self *Actor) HandleClose(r *http.Request, session RPCSession)

call the close handler if possible

func (Actor) Has

func (self Actor) Has(method string) bool

returns there is a handler for a method

func (Actor) MethodList

func (self Actor) MethodList() []string

func (*Actor) Off

func (self *Actor) Off(method string)

Off unregister the method from handlers

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"`
}

func (*AuthConfig) ValidateValues

func (self *AuthConfig) ValidateValues() error

Auth config

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)

func (AuthHandler) TryAuth

func (self AuthHandler) TryAuth(r *http.Request) (*AuthInfo, bool)

type AuthInfo

type AuthInfo struct {
	Username string
	Settings map[string]interface{}
}

func AuthInfoFromContext

func AuthInfoFromContext(ctx context.Context) (*AuthInfo, bool)

type BasicAuthConfig

type BasicAuthConfig struct {
	Username string                 `yaml:"username" json:"username"`
	Password string                 `yaml:"password" json:"password"`
	Settings map[string]interface{} `yaml:"settings,omitempty" json:"settings.omitempty"`
}

type BearerAuthConfig

type BearerAuthConfig struct {
	Token string `yaml:"token" json:"token"`

	// username attached to request when token authorized
	Username string                 `yaml:"username,omitempty" json:"username,omitempty"`
	Settings map[string]interface{} `yaml:"settings,omitempty" json:"settings.omitempty"`
}

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 ContextedMsgCallback func(ctx context.Context, params []interface{}) (interface{}, error)

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) Call

func (self *H1Client) Call(rootCtx context.Context, reqmsg *jlib.RequestMessage) (jlib.Message, error)

func (*H1Client) IsStreaming

func (self *H1Client) IsStreaming() bool

func (*H1Client) Send

func (self *H1Client) Send(rootCtx context.Context, msg jlib.Message) error

func (*H1Client) ServerURL

func (self *H1Client) ServerURL() *url.URL

func (*H1Client) SetClientTLSConfig

func (self *H1Client) SetClientTLSConfig(cfg *tls.Config)

func (*H1Client) SetExtraHeader

func (self *H1Client) SetExtraHeader(h http.Header)

func (*H1Client) UnwrapCall

func (self *H1Client) UnwrapCall(rootCtx context.Context, reqmsg *jlib.RequestMessage, output interface{}) error

type H1Handler

type H1Handler struct {
	Actor *Actor
}

func NewH1Handler

func NewH1Handler(actor *Actor) *H1Handler

func (*H1Handler) ServeHTTP

func (self *H1Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type H2Client

type H2Client struct {
	StreamingClient

	// use h2c
	UseH2C bool
	// contains filtered or unexported fields
}

func NewH2Client

func NewH2Client(serverUrl *url.URL) *H2Client

func (*H2Client) HTTPClient

func (self *H2Client) HTTPClient() *http.Client

func (*H2Client) String

func (self *H2Client) String() string

type H2Handler

type H2Handler struct {
	Actor *Actor

	// options
	SpawnGoroutine bool
	UseH2C         bool
	// contains filtered or unexported fields
}

func NewH2Handler

func NewH2Handler(serverCtx context.Context, actor *Actor) *H2Handler

func (*H2Handler) FallbackHandler

func (self *H2Handler) FallbackHandler() *H1Handler

func (*H2Handler) H2CHandler

func (self *H2Handler) H2CHandler() http.Handler

func (*H2Handler) ServeHTTP

func (self *H2Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type H2Session

type H2Session struct {
	// contains filtered or unexported fields
}

func (*H2Session) Send

func (self *H2Session) Send(msg jlib.Message)

func (H2Session) SessionID

func (self H2Session) SessionID() string

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) Parse

func (self *HeaderFlags) Parse() (http.Header, error)

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 MessageHandler func(msg jlib.Message)

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 NewRPCRequest(ctx context.Context, msg jlib.Message, transportType string, r *http.Request) *RPCRequest

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 RPCSession interface {
	Send(msg jlib.Message)
	SessionID() string
}

type ReqSpec added in v0.3.10

type ReqSpec struct{}

func (ReqSpec) Check added in v0.3.10

func (self ReqSpec) Check(firstArgType reflect.Type) bool

func (ReqSpec) String added in v0.3.10

func (self ReqSpec) String() string

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

type SimpleResponse struct {
	Code int
	Body []byte
}

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) Send

func (self *StreamingClient) Send(rootCtx context.Context, msg jlib.Message) 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

type TLSConfig struct {
	Certfile string
	Keyfile  string
}

func (*TLSConfig) ValidateValues

func (self *TLSConfig) ValidateValues() error

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 WSClient

type WSClient struct {
	StreamingClient
}

func NewWSClient

func NewWSClient(serverUrl *url.URL) *WSClient

func (*WSClient) String

func (self *WSClient) String() string

type WSHandler

type WSHandler struct {
	Actor *Actor

	// options
	SpawnGoroutine bool
	// contains filtered or unexported fields
}

func NewWSHandler

func NewWSHandler(serverCtx context.Context, actor *Actor) *WSHandler

func (*WSHandler) ServeHTTP

func (self *WSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type WSSession

type WSSession struct {
	// contains filtered or unexported fields
}

func (*WSSession) Send

func (self *WSSession) Send(msg jlib.Message)

func (WSSession) SessionID

func (self WSSession) SessionID() string

type WrappedResponse

type WrappedResponse struct {
	Response *http.Response
	Body     []byte
}

errors non standard Response returned by endpoints

func (WrappedResponse) Error

func (self WrappedResponse) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL