webFramework

package
v0.28.1 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package webFramework provides the web framework abstraction layer for requestCore.

Index

Constants

View Source
const (
	// LogTagNameFormat is the format string for log tag local storage keys.
	LogTagNameFormat string = "LOG_TAG_%s"
	// LogArrayNameFormat is the format string for log array local storage keys.
	LogArrayNameFormat string = "LOG_ARRAY_%s"
	// HandlerLogTag is the log tag for handler lifecycle logs.
	HandlerLogTag string = "handler"
	// ErrorListLogTag is the log tag for error list logs.
	ErrorListLogTag string = "errors"
)
View Source
const TransactionLoggerLocalKey = "transaction_logger"

TransactionLoggerLocalKey is the per-request local-storage key used to register a TransactionLogger for the duration of a request. Set it via w.Parser.SetLocal(webFramework.TransactionLoggerLocalKey, logger).

Variables

This section is empty.

Functions

func AddLog added in v0.13.4

func AddLog(w WebFramework, title string, log slog.Attr)

AddLog appends a log attribute to the request's log array for the given title.

func AddLogTag added in v0.13.10

func AddLogTag(w WebFramework, title string, log slog.Attr)

AddLogTag appends a log tag attribute to the request's log tags for the given title.

func AddServiceRegistrationLog added in v0.13.13

func AddServiceRegistrationLog(name string)

AddServiceRegistrationLog records a service registration entry in the startup logs.

func AddStartUpLog added in v0.13.13

func AddStartUpLog(log slog.Attr)

AddStartUpLog appends a log attribute to the startup logs.

func AddStartUpLogTag added in v0.13.13

func AddStartUpLogTag(title string, log slog.Attr)

AddStartUpLogTag appends a tagged log attribute to the startup logs.

func CollectLogArrays added in v0.13.10

func CollectLogArrays(w WebFramework, title string)

CollectLogArrays collects log array attributes for the given title and adds them as custom attributes.

func CollectLogTags added in v0.13.10

func CollectLogTags(w WebFramework, title string)

CollectLogTags collects log tag attributes for the given title and adds them as custom attributes.

func CollectServiceRegistrationLogs added in v0.13.13

func CollectServiceRegistrationLogs()

CollectServiceRegistrationLogs emits accumulated service registration logs as a startup log group.

func CollectStartUpLogs added in v0.13.13

func CollectStartUpLogs()

CollectStartUpLogs emits all accumulated startup logs via slog and resets the buffer.

func GetLocalOrDefault added in v0.27.0

func GetLocalOrDefault[T any](w WebFramework, key string, defaultValue T) T

GetLocalOrDefault retrieves a parser-local value by key. It returns defaultValue when w.Parser is nil, the local is absent, or the stored value is not of type T.

GetLocalOrDefault is read-only: it never mutates parser locals and never registers package-level state. It is safe for concurrent use as long as the underlying RequestParser implementation is safe for concurrent reads.

Types

type FakeParser added in v0.9.49

type FakeParser struct {
	Method      string
	Path        string
	Header      HeaderInterface
	HttpHeader  http.Header
	ReqHeader   map[string]string
	RespHeader  map[string]string
	Body        any
	URI         any
	URLQuery    any
	RawUrlQuery string
	Locals      map[string]any
	Args        map[string]string
	Urlparams   map[string]string
	JsonResp    any
}

FakeParser is a test implementation of the RequestParser interface.

func (FakeParser) Abort added in v0.9.49

func (f FakeParser) Abort() error

Abort is a no-op that always returns nil for the fake parser.

func (FakeParser) AddCustomAttributes added in v0.13.2

func (t FakeParser) AddCustomAttributes(attr slog.Attr)

AddCustomAttributes adds a custom slog attribute to the fake parser's local storage.

func (FakeParser) AddSpanAttribute added in v0.18.0

func (f FakeParser) AddSpanAttribute(key, value string)

AddSpanAttribute is a no-op for the fake parser.

func (FakeParser) AddSpanAttributes added in v0.18.0

func (f FakeParser) AddSpanAttributes(attrs map[string]string)

AddSpanAttributes is a no-op for the fake parser.

func (FakeParser) AddSpanEvent added in v0.18.0

func (f FakeParser) AddSpanEvent(name string, attrs map[string]string)

AddSpanEvent is a no-op for the fake parser.

func (FakeParser) CheckURLParam added in v0.28.1

func (f FakeParser) CheckURLParam(name string) (string, bool)

CheckURLParam returns the fake URL parameter and whether it exists.

func (FakeParser) FileAttachment added in v0.10.4

func (f FakeParser) FileAttachment(path, fileName string)

FileAttachment is a no-op for the fake parser.

func (FakeParser) FormValue added in v0.10.1

func (f FakeParser) FormValue(name string) string

FormValue returns the fake form value for the given name.

func (FakeParser) GetArgs added in v0.9.49

func (f FakeParser) GetArgs(args ...any) map[string]string

GetArgs returns the fake request arguments map.

func (FakeParser) GetBody added in v0.9.49

func (f FakeParser) GetBody(target any) error

GetBody is a no-op that always returns nil for the fake parser.

func (FakeParser) GetContext added in v0.22.0

func (f FakeParser) GetContext() context.Context

GetContext returns a background context for testing

func (FakeParser) GetHTTPHeader added in v0.28.1

func (f FakeParser) GetHTTPHeader() http.Header

GetHTTPHeader returns the fake HTTP headers.

func (FakeParser) GetHeader added in v0.9.49

func (f FakeParser) GetHeader(target HeaderInterface) error

GetHeader is a no-op that always returns nil for the fake parser.

func (FakeParser) GetHeaderValue added in v0.9.49

func (f FakeParser) GetHeaderValue(name string) string

GetHeaderValue returns the fake request header value for the given name.

func (FakeParser) GetLocal added in v0.9.49

func (f FakeParser) GetLocal(name string) any

GetLocal returns the fake local value for the given name.

func (FakeParser) GetLocalString added in v0.9.49

func (f FakeParser) GetLocalString(name string) string

GetLocalString returns the fake local value as a string for the given name.

func (FakeParser) GetLogger added in v0.13.1

func (f FakeParser) GetLogger() *slog.Logger

GetLogger returns the slog logger from the fake parser's local storage.

func (FakeParser) GetMethod added in v0.9.49

func (f FakeParser) GetMethod() string

GetMethod returns the fake HTTP method.

func (FakeParser) GetPath added in v0.9.49

func (f FakeParser) GetPath() string

GetPath returns the fake request path.

func (FakeParser) GetRawURLQuery added in v0.28.1

func (f FakeParser) GetRawURLQuery() string

GetRawURLQuery returns the fake raw URL query string.

func (FakeParser) GetTraceContext added in v0.18.0

func (f FakeParser) GetTraceContext() trace.SpanContext

Tracing methods for TestingParser

func (FakeParser) GetURI added in v0.28.1

func (f FakeParser) GetURI(target any) error

GetURI is a no-op that always returns nil for the fake parser.

func (FakeParser) GetURLParam added in v0.28.1

func (f FakeParser) GetURLParam(name string) string

GetURLParam returns the fake URL parameter value for the given name.

func (FakeParser) GetURLParams added in v0.28.1

func (f FakeParser) GetURLParams() map[string]string

GetURLParams returns all fake URL parameters.

func (FakeParser) GetURLQuery added in v0.28.1

func (f FakeParser) GetURLQuery(target any) error

GetURLQuery is a no-op that always returns nil for the fake parser.

func (FakeParser) Next added in v0.9.49

func (f FakeParser) Next() error

Next is a no-op that always returns nil for the fake parser.

func (FakeParser) ParseCommand added in v0.9.49

func (f FakeParser) ParseCommand(command, title string, request RecordData, parser FieldParser) string

ParseCommand returns an empty string for the fake parser.

func (FakeParser) RecordSpanError added in v0.18.0

func (f FakeParser) RecordSpanError(err error, attrs map[string]string)

RecordSpanError is a no-op for the fake parser.

func (FakeParser) SaveFile added in v0.10.1

func (f FakeParser) SaveFile(
	formTagName, path string,
) error

SaveFile is a no-op that always returns nil for the fake parser.

func (FakeParser) SendJSONRespBody added in v0.9.49

func (f FakeParser) SendJSONRespBody(status int, resp any) error

SendJSONRespBody is a no-op that always returns nil for the fake parser.

func (FakeParser) SetContext added in v0.22.0

func (f FakeParser) SetContext(ctx context.Context)

SetContext is a no-op for testing

func (FakeParser) SetLocal added in v0.9.49

func (f FakeParser) SetLocal(name string, value any)

SetLocal stores a value in the fake parser's local storage.

func (FakeParser) SetReqHeader added in v0.9.49

func (f FakeParser) SetReqHeader(name string, value string)

SetReqHeader sets a fake request header value.

func (FakeParser) SetRespHeader added in v0.10.28

func (f FakeParser) SetRespHeader(name string, value string)

SetRespHeader sets a fake response header value.

func (FakeParser) SetTraceContext added in v0.18.0

func (f FakeParser) SetTraceContext(spanCtx trace.SpanContext)

SetTraceContext is a no-op for the fake parser.

func (FakeParser) StartSpan added in v0.18.0

func (f FakeParser) StartSpan(name string, opts ...trace.SpanStartOption) (context.Context, trace.Span)

StartSpan returns a background context and span for the fake parser.

type FieldParser added in v0.7.1

type FieldParser interface {
	Parse(string) string
}

FieldParser defines the interface for parsing field values in command templates.

type HeaderInterface added in v0.8.4

type HeaderInterface interface {
	GetID() string
	GetUser() string
	GetProgram() string
	GetModule() string
	GetMethod() string
	SetUser(string)
	SetProgram(string)
	SetModule(string)
	SetMethod(string)
}

HeaderInterface defines the methods for accessing and mutating request headers.

type RecordData added in v0.7.1

type RecordData interface {
	GetID() string
	GetControlID(string) string
	GetIDList() []any
	SetID(string)
	SetValue(string)
	GetSubCategory() string
	GetValue() any
	GetValueMap() map[string]string
}

RecordData defines the interface for record data used in query and DML operations.

type RequestHandler

type RequestHandler interface {
	Respond(code, status int, message string, data any, abort bool)
	HandleErrorState(err error, status int, message string, data any)
}

RequestHandler defines the interface for responding to HTTP requests.

type RequestParser

type RequestParser interface {
	GetMethod() string
	GetPath() string
	GetHeader(target HeaderInterface) error
	GetHeaderValue(name string) string
	GetHTTPHeader() http.Header
	GetBody(target any) error
	GetURI(target any) error
	GetURLQuery(target any) error
	GetRawURLQuery() string
	GetLocal(name string) any
	GetLocalString(name string) string
	GetURLParam(name string) string
	GetURLParams() map[string]string
	CheckURLParam(name string) (string, bool)
	SetLocal(name string, value any)
	SetReqHeader(name string, value string)
	SetRespHeader(name string, value string)
	GetArgs(args ...any) map[string]string
	ParseCommand(command, title string, request RecordData, parser FieldParser) string
	SendJSONRespBody(status int, resp any) error
	Next() error
	Abort() error
	FormValue(name string) string
	SaveFile(formTagName, path string) error
	FileAttachment(path, fileName string)
	AddCustomAttributes(attr slog.Attr)
	// Tracing methods
	GetTraceContext() trace.SpanContext
	SetTraceContext(spanCtx trace.SpanContext)
	StartSpan(name string, opts ...trace.SpanStartOption) (context.Context, trace.Span)
	AddSpanAttribute(key, value string)
	AddSpanAttributes(attrs map[string]string)
	AddSpanEvent(name string, attrs map[string]string)
	RecordSpanError(err error, attrs map[string]string)
	// Context management for tracing
	GetContext() context.Context
	SetContext(context.Context)
}

RequestParser defines the interface for parsing HTTP requests in a web framework.

type TransactionInfo added in v0.27.0

type TransactionInfo struct {
	ServiceName        string        // param.API.Name
	URL                string        // full request URL (domain + "/" + path + query)
	Endpoint           string        // param.Path
	Method             string        // param.Method (HTTP method)
	StatusCode         int           // actual HTTP status (0 if request failed before getting a response)
	Duration           time.Duration // elapsed time
	Error              error         // nil on success
	Request            any           // param.JSONBody (caller may mask sensitive fields via CallAPIOptions.MaskFunc)
	Response           any           // parsed response (nil on error; caller may mask via MaskFunc)
	ResponseBody       []byte        // raw response body from RemoteCallError on error, nil on success; preserved raw for diagnostics — may contain sensitive data
	MaskedResponseBody any           // MaskFunc(ResponseBody) on error paths when CallAPIOptions.MaskFunc is set; nil when MaskFunc is nil or on success. Transaction loggers should emit this field by default and use ResponseBody only when the raw bytes are explicitly required.
}

TransactionInfo is the canonical generic payload describing a completed remote API call. It is used by TransactionLogger and CallAPIOptions.OnComplete.

type TransactionLogger added in v0.27.0

type TransactionLogger interface {
	LogTransaction(info TransactionInfo)
}

TransactionLogger is a framework-level interface for recording transaction logs after remote API calls. Register an implementation via w.Parser.SetLocal(TransactionLoggerLocalKey, logger) during request initialization. CallAPIJSONWithOpts resolves and invokes it unconditionally on every code path (success, error, timeout) in addition to webFramework.AddLog.

type WebFramework

type WebFramework struct {
	Ctx  context.Context
	Span trace.Span
	//Handler response.ResponseHandler
	Parser RequestParser
}

WebFramework holds the request context, tracing span, and parser for handling HTTP requests.

func (*WebFramework) AddSpanAttribute added in v0.18.0

func (w *WebFramework) AddSpanAttribute(key, value string)

AddSpanAttribute adds a string key-value attribute to the web framework's span.

func (*WebFramework) AddSpanAttributes added in v0.18.0

func (w *WebFramework) AddSpanAttributes(attrs map[string]string)

AddSpanAttributes adds multiple string key-value attributes to the web framework's span.

func (*WebFramework) AddSpanEvent added in v0.18.0

func (w *WebFramework) AddSpanEvent(name string, attrs map[string]string)

AddSpanEvent records an event with attributes on the web framework's span.

func (*WebFramework) GetTraceContext added in v0.18.0

func (w *WebFramework) GetTraceContext() trace.SpanContext

GetTraceContext returns the trace span context from the web framework span.

func (*WebFramework) RecordSpanError added in v0.18.0

func (w *WebFramework) RecordSpanError(err error, attrs map[string]string)

RecordSpanError records an error with attributes on the web framework's span.

func (*WebFramework) SetSpan added in v0.18.0

func (w *WebFramework) SetSpan(span trace.Span)

SetSpan sets the tracing span on the web framework.

func (*WebFramework) StartSpan added in v0.18.0

func (w *WebFramework) StartSpan(name string, opts ...trace.SpanStartOption) (context.Context, trace.Span)

StartSpan starts a new tracing span via the parser.

Jump to

Keyboard shortcuts

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