relay

package
v1.138.1 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package relay securely relays model API requests to a trusted upstream gateway.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Credentials

type Credentials func(header http.Header, token string)

Credentials applies an upstream token to a request.

type Option

type Option func(*Relay)

Option configures a Relay.

func WithClient

func WithClient(client *http.Client) Option

WithClient sets the outbound client. Its redirect policy is always replaced.

func WithCredentials

func WithCredentials(credentials Credentials) Option

WithCredentials sets how the upstream token is represented. By default it is sent only as an Authorization bearer token.

func WithMaxRequestBytes

func WithMaxRequestBytes(n int64) Option

WithMaxRequestBytes sets the request body limit.

func WithMaxStreamLineBytes

func WithMaxStreamLineBytes(n int) Option

WithMaxStreamLineBytes sets the maximum SSE line size.

func WithRequestHeaders

func WithRequestHeaders(headers ...string) Option

WithRequestHeaders replaces the request header allowlist.

func WithResponseHeaders

func WithResponseHeaders(headers ...string) Option

WithResponseHeaders replaces the response header allowlist.

func WithStreamInterruptedEvent

func WithStreamInterruptedEvent(event []byte) Option

WithStreamInterruptedEvent sets bytes appended when a streamed response is incomplete.

func WithStreamObserver

func WithStreamObserver(newObserver func() StreamObserver) Option

WithStreamObserver enables SSE observation and completion validation.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the timeout for one upstream call.

type Relay

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

Relay forwards validated requests to a models gateway.

func New

func New(target Target, path string, tokens TokenSource, validator Validator, opts ...Option) (*Relay, error)

New creates a relay with restrictive defaults. The target, path, token source, and request validator are required.

func (*Relay) Serve

func (r *Relay) Serve(w http.ResponseWriter, req *http.Request) (Result, error)

Serve forwards one POST. It always writes a client response; returned errors are intended for server logs and may contain upstream details.

type RequestInfo

type RequestInfo struct {
	Stream bool
	Values map[string]string
}

RequestInfo describes the validated request fields needed by the relay.

type Result

type Result struct {
	Status int
	Info   RequestInfo
}

Result reports the upstream response and validator metadata.

type StreamObserver

type StreamObserver interface {
	Observe(data []byte)
	Complete() bool
}

StreamObserver inspects SSE data fields and verifies stream completion.

type Target

type Target func(*http.Request) (*url.URL, error)

Target resolves a trusted upstream base URL for a request. Implementations must not return unvalidated user input.

type TokenSource

type TokenSource interface {
	Token(ctx context.Context) (string, error)
	Invalidate(token string)
}

TokenSource supplies and invalidates upstream credentials.

type Validator

type Validator interface {
	Validate(body []byte, query url.Values) (RequestInfo, error)
}

Validator validates the request body and query before credentials are used.

Jump to

Keyboard shortcuts

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