network

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2025 License: MIT Imports: 26 Imported by: 1

Documentation

Overview

Package network This code is adapted from the "github.com/labstack/echo" project, specifically the file "router.go," which is licensed under the MIT License.

Index

Constants

View Source
const (
	KeyHTTPRequest        = "__http.Request__"
	KeyHTTPResponseWriter = "__http.ResponseWriter__"
)
View Source
const KindHTTP = "http"
View Source
const KindListener = "listener"
View Source
const KindRouter = "router"
View Source
const KindUpgrader = "upgrader"
View Source
const KindWebSocket = "websocket"
View Source
const ProtocolHTTP = "http"
View Source
const ProtocolWebsocket = "websocket"

Variables

View Source
var ErrInvalidProtocol = errors.New("protocol is invalid")

Functions

func AddToHook

func AddToHook() hook.Register

AddToHook returns a function that adds hook to the provided hook.

func AddToScheme

func AddToScheme() scheme.Register

AddToScheme returns a function that adds node types and codecs to the provided spec.

func NewHTTPNodeCodec

func NewHTTPNodeCodec() scheme.Codec

NewHTTPNodeCodec creates a new codec for HTTPNode.

func NewListenNodeCodec

func NewListenNodeCodec() scheme.Codec

NewListenNodeCodec creates a new codec for ListenNodeSpec.

func NewRouteNodeCodec

func NewRouteNodeCodec() scheme.Codec

NewRouteNodeCodec creates a new codec for RouteNodeSpec.

func NewUpgradeNodeCodec added in v0.13.0

func NewUpgradeNodeCodec() scheme.Codec

NewUpgradeNodeCodec creates a new codec for UpgradeNodeSpec.

func NewWebSocketNodeCodec

func NewWebSocketNodeCodec() scheme.Codec

NewWebSocketNodeCodec creates a new codec for WebSocketNodeSpec.

Types

type HTTPListenNode

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

HTTPListenNode represents a Node for handling HTTP requests.

func NewHTTPListenNode

func NewHTTPListenNode(address string) *HTTPListenNode

NewHTTPListenNode creates a new HTTPListenNode with the specified address.

func (*HTTPListenNode) Address

func (n *HTTPListenNode) Address() net.Addr

Address returns the listener address if available.

func (*HTTPListenNode) Close

func (n *HTTPListenNode) Close() error

Close closes all ports and stops the HTTP server.

func (*HTTPListenNode) In

func (n *HTTPListenNode) In(_ string) *port.InPort

In returns the input port with the specified name.

func (*HTTPListenNode) Listen

func (n *HTTPListenNode) Listen() error

Listen starts the HTTP server.

func (*HTTPListenNode) Out

func (n *HTTPListenNode) Out(name string) *port.OutPort

Out returns the output port with the specified name.

func (*HTTPListenNode) ServeHTTP

func (n *HTTPListenNode) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles HTTP requests.

func (*HTTPListenNode) Shutdown

func (n *HTTPListenNode) Shutdown() error

Shutdown shuts down the HTTPListenNode by closing the server and its associated listener.

func (*HTTPListenNode) TLS added in v0.10.0

func (n *HTTPListenNode) TLS(cert, key string) error

TLS configures the HTTP server to use TLS with the provided certificate and key.

type HTTPNode

type HTTPNode struct {
	*node.OneToOneNode
	// contains filtered or unexported fields
}

HTTPNode represents a node for making HTTP client requests.

func NewHTTPNode

func NewHTTPNode(client *http.Client) *HTTPNode

NewHTTPNode creates a new HTTPNode instance.

func (*HTTPNode) SetTimeout

func (n *HTTPNode) SetTimeout(timeout time.Duration)

SetTimeout sets the timeout duration for the HTTP request.

func (*HTTPNode) SetURL added in v0.13.0

func (n *HTTPNode) SetURL(url *url.URL)

SetURL sets the URL for the HTTP request.

type HTTPNodeSpec

type HTTPNodeSpec struct {
	spec.Meta `map:",inline"`
	URL       string        `map:"url" validate:"required,url"`
	Timeout   time.Duration `map:"timeout,omitempty"`
}

HTTPNodeSpec defines the specifications for creating an HTTPNode.

type HTTPPayload

type HTTPPayload struct {
	Method   string      `map:"method,omitempty"`
	Scheme   string      `map:"scheme,omitempty"`
	Host     string      `map:"host,omitempty"`
	Path     string      `map:"path,omitempty"`
	Query    url.Values  `map:"query,omitempty"`
	Protocol string      `map:"protocol,omitempty"`
	Header   http.Header `map:"header,omitempty"`
	Body     types.Value `map:"body,omitempty"`
	Status   int         `map:"status"`
}

HTTPPayload is the payload structure for HTTP requests and responses.

func NewHTTPPayload

func NewHTTPPayload(status int, bodies ...types.Value) *HTTPPayload

NewHTTPPayload creates a new HTTPPayload with the given HTTP status code and optional body.

type ListenNodeSpec

type ListenNodeSpec struct {
	spec.Meta `map:",inline"`
	Protocol  string `map:"protocol" validate:"required"`
	Host      string `map:"host,omitempty" validate:"omitempty,hostname|ip"`
	Port      int    `map:"port" validate:"required"`
	TLS       TLS    `map:"tls"`
}

ListenNodeSpec defines the specifications for creating a ListenNode.

type Route

type Route struct {
	Method string `map:"method" validate:"required"`
	Path   string `map:"path" validate:"required"`
	Port   string `map:"port" validate:"required"`
}

Route represents a routing configuration with a specific HTTP method, path, and port.

type RouteNode

type RouteNode struct {
	*node.OneToManyNode
	// contains filtered or unexported fields
}

RouteNode represents a node for routing based on HTTP method, path, and port.

func NewRouteNode

func NewRouteNode() *RouteNode

NewRouteNode creates a new RouteNode.

func (*RouteNode) Add

func (n *RouteNode) Add(method, path, port string)

Add adds a new route to the routing tree for the specified HTTP method, path, and port.

func (*RouteNode) Find

func (n *RouteNode) Find(method, path string) (string, map[string]string)

Find searches for a matching route based on the provided HTTP method and path.

type RouteNodeSpec

type RouteNodeSpec struct {
	spec.Meta `map:",inline"`
	Routes    []Route `map:"routes" validate:"required"`
}

RouteNodeSpec defines the specification for configuring a RouteNode.

type TLS added in v0.13.0

type TLS struct {
	Cert string `map:"cert,omitempty"`
	Key  string `map:"key,omitempty"`
}

type UpgradeNodeSpec added in v0.13.0

type UpgradeNodeSpec struct {
	spec.Meta `map:",inline"`
	Protocol  string        `map:"protocol" validate:"required"`
	Timeout   time.Duration `map:"timeout,omitempty"`
	Buffer    int           `map:"buffer,omitempty"`
}

UpgradeNodeSpec defines the specifications for creating a UpgradeNode.

type WebSocketConnNode

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

WebSocketConnNode represents a node for handling WebSocket connection.

func NewWebSocketConnNode

func NewWebSocketConnNode(action func(*process.Process, *packet.Packet) (*websocket.Conn, error)) *WebSocketConnNode

NewWebSocketConnNode creates a new WebSocketConnNode.

func (*WebSocketConnNode) Close

func (n *WebSocketConnNode) Close() error

Close closes all ports of the WebSocketConnNode.

func (*WebSocketConnNode) In

func (n *WebSocketConnNode) In(name string) *port.InPort

In returns the input port with the specified name.

func (*WebSocketConnNode) Out

func (n *WebSocketConnNode) Out(name string) *port.OutPort

Out returns the output port with the specified name.

type WebSocketNode

type WebSocketNode struct {
	*WebSocketConnNode
	// contains filtered or unexported fields
}

WebSocketNode represents a node for establishing WebSocket client connection.

func NewWebSocketNode

func NewWebSocketNode(url *url.URL) *WebSocketNode

NewWebSocketNode creates a new WebSocketNode.

func (*WebSocketNode) SetTimeout

func (n *WebSocketNode) SetTimeout(timeout time.Duration)

SetTimeout sets the handshake timeout for WebSocket conns.

type WebSocketNodeSpec

type WebSocketNodeSpec struct {
	spec.Meta `map:",inline"`
	URL       string        `map:"url" validate:"required,url"`
	Timeout   time.Duration `map:"timeout,omitempty"`
}

WebSocketNodeSpec defines the specifications for creating a WebSocketNode.

type WebSocketPayload

type WebSocketPayload struct {
	Type int         `map:"type"`
	Data types.Value `map:"data,omitempty"`
}

WebSocketPayload represents the payload structure for WebSocket messages.

type WebSocketUpgradeNode

type WebSocketUpgradeNode struct {
	*WebSocketConnNode
	// contains filtered or unexported fields
}

WebSocketUpgradeNode is a node for upgrading an HTTP connection to a WebSocket connection.

func NewWebSocketUpgradeNode

func NewWebSocketUpgradeNode() *WebSocketUpgradeNode

NewWebSocketUpgradeNode creates a new WebSocketUpgradeNode.

func (*WebSocketUpgradeNode) ReadBufferSize

func (n *WebSocketUpgradeNode) ReadBufferSize() int

ReadBufferSize returns the read buffer size.

func (*WebSocketUpgradeNode) SetReadBufferSize

func (n *WebSocketUpgradeNode) SetReadBufferSize(size int)

SetReadBufferSize sets the read buffer size.

func (*WebSocketUpgradeNode) SetTimeout

func (n *WebSocketUpgradeNode) SetTimeout(timeout time.Duration)

SetTimeout sets the timeout duration.

func (*WebSocketUpgradeNode) SetWriteBufferSize

func (n *WebSocketUpgradeNode) SetWriteBufferSize(size int)

SetWriteBufferSize sets the write buffer size.

func (*WebSocketUpgradeNode) Timeout

func (n *WebSocketUpgradeNode) Timeout() time.Duration

Timeout returns the timeout duration.

func (*WebSocketUpgradeNode) WriteBufferSize

func (n *WebSocketUpgradeNode) WriteBufferSize() int

WriteBufferSize returns the write buffer size.

Jump to

Keyboard shortcuts

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