middleware

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseMiddleware

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

BaseMiddleware provides a default implementation of Middleware Embed this in your custom middleware to only implement the methods you need

func NewBaseMiddleware

func NewBaseMiddleware(name string) *BaseMiddleware

NewBaseMiddleware creates a new base middleware

func (*BaseMiddleware) AfterIndex

func (m *BaseMiddleware) AfterIndex(ctx context.Context, chunks []core.Chunk) error

AfterIndex is a no-op by default

func (*BaseMiddleware) AfterQuery

func (m *BaseMiddleware) AfterQuery(ctx context.Context, response *Response) error

AfterQuery is a no-op by default

func (*BaseMiddleware) BeforeIndex

func (m *BaseMiddleware) BeforeIndex(ctx context.Context, source *Source) error

BeforeIndex is a no-op by default

func (*BaseMiddleware) BeforeQuery

func (m *BaseMiddleware) BeforeQuery(ctx context.Context, query *Query) error

BeforeQuery is a no-op by default

func (*BaseMiddleware) Name

func (m *BaseMiddleware) Name() string

Name returns the middleware name

type Cache

type Cache interface {
	Get(ctx context.Context, key string) (*Response, bool)
	Set(ctx context.Context, key string, response *Response, ttl time.Duration) error
}

Cache defines the cache interface

type CacheMiddleware

type CacheMiddleware struct {
	*BaseMiddleware
	// contains filtered or unexported fields
}

CacheMiddleware provides caching for query results

func NewCacheMiddleware

func NewCacheMiddleware(cache Cache) *CacheMiddleware

NewCacheMiddleware creates a new cache middleware

func (*CacheMiddleware) AfterQuery

func (m *CacheMiddleware) AfterQuery(ctx context.Context, response *Response) error

AfterQuery stores response in cache

func (*CacheMiddleware) BeforeQuery

func (m *CacheMiddleware) BeforeQuery(ctx context.Context, query *Query) error

BeforeQuery checks cache before query

type Chain

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

Chain represents a middleware chain

func NewChain

func NewChain(middlewares ...Middleware) *Chain

NewChain creates a new middleware chain

func (*Chain) Add

func (c *Chain) Add(middleware Middleware)

Add adds a middleware to the chain

func (*Chain) AfterIndex

func (c *Chain) AfterIndex(ctx context.Context, chunks []core.Chunk) error

AfterIndex executes all AfterIndex middleware

func (*Chain) AfterQuery

func (c *Chain) AfterQuery(ctx context.Context, response *Response) error

AfterQuery executes all AfterQuery middleware

func (*Chain) BeforeIndex

func (c *Chain) BeforeIndex(ctx context.Context, source *Source) error

BeforeIndex executes all BeforeIndex middleware

func (*Chain) BeforeQuery

func (c *Chain) BeforeQuery(ctx context.Context, query *Query) error

BeforeQuery executes all BeforeQuery middleware

type Logger

type Logger interface {
	Info(ctx context.Context, message string, fields map[string]interface{})
	Debug(ctx context.Context, message string, fields map[string]interface{})
	Error(ctx context.Context, message string, err error, fields map[string]interface{})
}

Logger defines the logging interface

type LoggingMiddleware

type LoggingMiddleware struct {
	*BaseMiddleware
	// contains filtered or unexported fields
}

LoggingMiddleware logs indexing and query operations

func NewLoggingMiddleware

func NewLoggingMiddleware(logger Logger) *LoggingMiddleware

NewLoggingMiddleware creates a new logging middleware

func (*LoggingMiddleware) AfterIndex

func (m *LoggingMiddleware) AfterIndex(ctx context.Context, chunks []core.Chunk) error

AfterIndex logs after indexing

func (*LoggingMiddleware) AfterQuery

func (m *LoggingMiddleware) AfterQuery(ctx context.Context, response *Response) error

AfterQuery logs after query

func (*LoggingMiddleware) BeforeIndex

func (m *LoggingMiddleware) BeforeIndex(ctx context.Context, source *Source) error

BeforeIndex logs before indexing

func (*LoggingMiddleware) BeforeQuery

func (m *LoggingMiddleware) BeforeQuery(ctx context.Context, query *Query) error

BeforeQuery logs before query

type Metrics

type Metrics interface {
	RecordIndexLatency(ctx context.Context, duration time.Duration)
	RecordIndexCount(ctx context.Context, status string)
	RecordQueryLatency(ctx context.Context, duration time.Duration)
	RecordQueryCount(ctx context.Context, status string)
}

Metrics defines the metrics collection interface

type MetricsMiddleware

type MetricsMiddleware struct {
	*BaseMiddleware
	// contains filtered or unexported fields
}

MetricsMiddleware collects metrics for indexing and query operations

func NewMetricsMiddleware

func NewMetricsMiddleware(metrics Metrics) *MetricsMiddleware

NewMetricsMiddleware creates a new metrics middleware

func (*MetricsMiddleware) AfterIndex

func (m *MetricsMiddleware) AfterIndex(ctx context.Context, chunks []core.Chunk) error

AfterIndex records index metrics

func (*MetricsMiddleware) AfterQuery

func (m *MetricsMiddleware) AfterQuery(ctx context.Context, response *Response) error

AfterQuery records query metrics

func (*MetricsMiddleware) BeforeIndex

func (m *MetricsMiddleware) BeforeIndex(ctx context.Context, source *Source) error

BeforeIndex records index start time

func (*MetricsMiddleware) BeforeQuery

func (m *MetricsMiddleware) BeforeQuery(ctx context.Context, query *Query) error

BeforeQuery records query start time

type Middleware

type Middleware interface {
	// Name returns the middleware name
	Name() string

	// BeforeIndex is called before indexing a document
	BeforeIndex(ctx context.Context, source *Source) error

	// AfterIndex is called after indexing a document
	AfterIndex(ctx context.Context, chunks []core.Chunk) error

	// BeforeQuery is called before executing a query
	BeforeQuery(ctx context.Context, query *Query) error

	// AfterQuery is called after executing a query
	AfterQuery(ctx context.Context, response *Response) error
}

Middleware defines the interface for middleware components Middleware can intercept and modify the indexing and query pipeline

type Query

type Query struct {
	Question string
	Options  map[string]interface{}
}

Query represents a query request

type RateLimitMiddleware

type RateLimitMiddleware struct {
	*BaseMiddleware
	// contains filtered or unexported fields
}

RateLimitMiddleware implements rate limiting

func NewRateLimitMiddleware

func NewRateLimitMiddleware(limiter RateLimiter) *RateLimitMiddleware

NewRateLimitMiddleware creates a new rate limit middleware

func (*RateLimitMiddleware) BeforeQuery

func (m *RateLimitMiddleware) BeforeQuery(ctx context.Context, query *Query) error

BeforeQuery checks rate limit before query

type RateLimiter

type RateLimiter interface {
	Allow(ctx context.Context, key string) (bool, error)
}

RateLimiter defines the rate limiting interface

type Response

type Response struct {
	Answer  string
	Sources []core.Result
}

Response represents a query response

type Source

type Source struct {
	Type    string
	Path    string
	Content string
	Reader  interface{}
}

Source represents a document source for indexing

type TransformMiddleware

type TransformMiddleware struct {
	*BaseMiddleware
	// contains filtered or unexported fields
}

TransformMiddleware transforms queries and responses

func NewTransformMiddleware

func NewTransformMiddleware(
	queryTransformer func(ctx context.Context, query *Query) error,
	responseTransformer func(ctx context.Context, response *Response) error,
) *TransformMiddleware

NewTransformMiddleware creates a new transform middleware

func (*TransformMiddleware) AfterQuery

func (m *TransformMiddleware) AfterQuery(ctx context.Context, response *Response) error

AfterQuery transforms response

func (*TransformMiddleware) BeforeQuery

func (m *TransformMiddleware) BeforeQuery(ctx context.Context, query *Query) error

BeforeQuery transforms query

type ValidationMiddleware

type ValidationMiddleware struct {
	*BaseMiddleware
	// contains filtered or unexported fields
}

ValidationMiddleware validates input before processing

func NewValidationMiddleware

func NewValidationMiddleware(maxQueryLength int, maxFileSize int64) *ValidationMiddleware

NewValidationMiddleware creates a new validation middleware

func (*ValidationMiddleware) BeforeIndex

func (m *ValidationMiddleware) BeforeIndex(ctx context.Context, source *Source) error

BeforeIndex validates source before indexing

func (*ValidationMiddleware) BeforeQuery

func (m *ValidationMiddleware) BeforeQuery(ctx context.Context, query *Query) error

BeforeQuery validates query before execution

Jump to

Keyboard shortcuts

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