libGin

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: 22 Imported by: 0

Documentation

Overview

Package libGin provides a Gin web framework adapter for requestCore.

Index

Constants

This section is empty.

Variables

View Source
var (
	// TraceIDKey is the slog attribute key for the OpenTelemetry trace ID.
	TraceIDKey = "trace_id"
	// SpanIDKey is the slog attribute key for the OpenTelemetry span ID.
	SpanIDKey = "span_id"
	// RequestIDKey is the slog attribute key for the request ID.
	RequestIDKey = "id"

	// RequestBodyMaxSize is the maximum number of bytes captured from the request body.
	RequestBodyMaxSize = 64 * 1024 // 64KB
	// ResponseBodyMaxSize is the maximum number of bytes captured from the response body.
	ResponseBodyMaxSize = 64 * 1024 // 64KB

	// HiddenRequestHeaders is the set of request headers excluded from logs.
	HiddenRequestHeaders = map[string]struct{}{
		"authorization": {},
		"cookie":        {},
		"set-cookie":    {},
		"x-auth-token":  {},
		"x-csrf-token":  {},
		"x-xsrf-token":  {},
	}
	// HiddenResponseHeaders is the set of response headers excluded from logs.
	HiddenResponseHeaders = map[string]struct{}{
		"set-cookie": {},
	}

	// RequestIDHeaderKey is the canonical HTTP header key for the request ID.
	RequestIDHeaderKey = "X-Request-Id"
	// RequestIDContextKey is the gin context key for storing the request ID.
	RequestIDContextKey = "slog-gin.request-id"
)

Functions

func AddCustomAttributes added in v0.22.2

func AddCustomAttributes(c *gin.Context, attrs ...slog.Attr)

AddCustomAttributes adds custom attributes to the request context.

func AddSpanAttribute added in v0.18.0

func AddSpanAttribute(c *gin.Context, key, value string)

AddSpanAttribute adds an attribute to the current span in Gin context

func AddSpanAttributes added in v0.18.0

func AddSpanAttributes(c *gin.Context, attrs map[string]string)

AddSpanAttributes adds multiple attributes to the current span

func AddSpanEvent added in v0.18.0

func AddSpanEvent(c *gin.Context, name string, attrs map[string]string)

AddSpanEvent adds an event to the current span

func CustomTracingMiddleware added in v0.18.0

func CustomTracingMiddleware(tm *libTracing.TracingManager) gin.HandlerFunc

CustomTracingMiddleware creates a custom Gin middleware with more control

func GetRequestID added in v0.22.2

func GetRequestID(c *gin.Context) string

GetRequestID returns the request identifier.

func GetSpanFromContext added in v0.18.0

func GetSpanFromContext(c *gin.Context) trace.Span

GetSpanFromContext gets the span from Gin context

func Gin

func Gin(handler any) gin.HandlerFunc

Gin adapts a requestCore handler into a Gin HandlerFunc.

func New added in v0.22.2

func New(logger *slog.Logger) gin.HandlerFunc

New returns a gin.HandlerFunc (middleware) that logs requests using slog.

Requests with errors are logged using slog.Error(). Requests without errors are logged using slog.Info().

func NewWithConfig added in v0.22.2

func NewWithConfig(logger *slog.Logger, config Config) gin.HandlerFunc

NewWithConfig returns a gin.HandlerFunc (middleware) that logs requests using slog.

func RecordSpanError added in v0.18.0

func RecordSpanError(c *gin.Context, err error, attrs map[string]string)

RecordSpanError records an error in the current span

func TracingMiddleware added in v0.18.0

func TracingMiddleware() gin.HandlerFunc

TracingMiddleware creates Gin middleware for OpenTelemetry tracing

Types

type Config added in v0.22.2

type Config struct {
	DefaultLevel     slog.Level
	ClientErrorLevel slog.Level
	ServerErrorLevel slog.Level

	WithUserAgent      bool
	WithRequestID      bool
	WithRequestBody    bool
	WithRequestHeader  bool
	WithResponseBody   bool
	WithResponseHeader bool
	WithSpanID         bool
	WithTraceID        bool
	WithClientIP       bool

	HandleGinDebug bool

	Filters []Filter
}

Config holds the configuration for the slog Gin middleware.

type Filter added in v0.22.2

type Filter func(ctx *gin.Context) bool

Filter is a function that decides whether a request should be logged.

func Accept added in v0.22.2

func Accept(filter Filter) Filter

Accept returns the given filter unchanged.

func AcceptHost added in v0.22.2

func AcceptHost(hosts ...string) Filter

AcceptHost returns a filter that accepts requests whose host matches any of the given hosts.

func AcceptHostContains added in v0.22.2

func AcceptHostContains(parts ...string) Filter

AcceptHostContains returns a filter that accepts requests whose host contains any of the given parts.

func AcceptHostMatch added in v0.22.2

func AcceptHostMatch(regs ...regexp.Regexp) Filter

AcceptHostMatch returns a filter that accepts requests whose host matches any of the given regexps.

func AcceptHostPrefix added in v0.22.2

func AcceptHostPrefix(prefixs ...string) Filter

AcceptHostPrefix returns a filter that accepts requests whose host starts with any of the given prefixes.

func AcceptHostSuffix added in v0.22.2

func AcceptHostSuffix(prefixs ...string) Filter

AcceptHostSuffix returns a filter that accepts requests whose host ends with any of the given suffixes.

func AcceptMethod added in v0.22.2

func AcceptMethod(methods ...string) Filter

AcceptMethod returns a filter that accepts requests matching any of the given HTTP methods.

func AcceptPath added in v0.22.2

func AcceptPath(urls ...string) Filter

AcceptPath returns a filter that accepts requests whose path matches any of the given URLs.

func AcceptPathContains added in v0.22.2

func AcceptPathContains(parts ...string) Filter

AcceptPathContains returns a filter that accepts requests whose path contains any of the given parts.

func AcceptPathMatch added in v0.22.2

func AcceptPathMatch(regs ...regexp.Regexp) Filter

AcceptPathMatch returns a filter that accepts requests whose path matches any of the given regexps.

func AcceptPathPrefix added in v0.22.2

func AcceptPathPrefix(prefixs ...string) Filter

AcceptPathPrefix returns a filter that accepts requests whose path starts with any of the given prefixes.

func AcceptPathSuffix added in v0.22.2

func AcceptPathSuffix(prefixs ...string) Filter

AcceptPathSuffix returns a filter that accepts requests whose path ends with any of the given suffixes.

func AcceptStatus added in v0.22.2

func AcceptStatus(statuses ...int) Filter

AcceptStatus returns a filter that accepts responses matching any of the given statuses.

func AcceptStatusGreaterThan added in v0.22.2

func AcceptStatusGreaterThan(status int) Filter

AcceptStatusGreaterThan returns a filter that accepts responses with status greater than the given value.

func AcceptStatusGreaterThanOrEqual added in v0.22.2

func AcceptStatusGreaterThanOrEqual(status int) Filter

AcceptStatusGreaterThanOrEqual returns a filter that accepts responses with status greater than or equal to the given value.

func AcceptStatusLessThan added in v0.22.2

func AcceptStatusLessThan(status int) Filter

AcceptStatusLessThan returns a filter that accepts responses with status less than the given value.

func AcceptStatusLessThanOrEqual added in v0.22.2

func AcceptStatusLessThanOrEqual(status int) Filter

AcceptStatusLessThanOrEqual returns a filter that accepts responses with status less than or equal to the given value.

func Ignore added in v0.22.2

func Ignore(filter Filter) Filter

Ignore returns a filter that negates the given filter.

func IgnoreHost added in v0.22.2

func IgnoreHost(hosts ...string) Filter

IgnoreHost returns a filter that rejects requests whose host matches any of the given hosts.

func IgnoreHostContains added in v0.22.2

func IgnoreHostContains(parts ...string) Filter

IgnoreHostContains returns a filter that rejects requests whose host contains any of the given parts.

func IgnoreHostMatch added in v0.22.2

func IgnoreHostMatch(regs ...regexp.Regexp) Filter

IgnoreHostMatch returns a filter that rejects requests whose host matches any of the given regexps.

func IgnoreHostPrefix added in v0.22.2

func IgnoreHostPrefix(prefixs ...string) Filter

IgnoreHostPrefix returns a filter that rejects requests whose host starts with any of the given prefixes.

func IgnoreHostSuffix added in v0.22.2

func IgnoreHostSuffix(suffixs ...string) Filter

IgnoreHostSuffix returns a filter that rejects requests whose host ends with any of the given suffixes.

func IgnoreMethod added in v0.22.2

func IgnoreMethod(methods ...string) Filter

IgnoreMethod returns a filter that rejects requests matching any of the given methods.

func IgnorePath added in v0.22.2

func IgnorePath(urls ...string) Filter

IgnorePath returns a filter that rejects requests whose path matches any of the given URLs.

func IgnorePathContains added in v0.22.2

func IgnorePathContains(parts ...string) Filter

IgnorePathContains returns a filter that rejects requests whose path contains any of the given parts.

func IgnorePathMatch added in v0.22.2

func IgnorePathMatch(regs ...regexp.Regexp) Filter

IgnorePathMatch returns a filter that rejects requests whose path matches any of the given regexps.

func IgnorePathPrefix added in v0.22.2

func IgnorePathPrefix(prefixs ...string) Filter

IgnorePathPrefix returns a filter that rejects requests whose path starts with any of the given prefixes.

func IgnorePathSuffix added in v0.22.2

func IgnorePathSuffix(suffixs ...string) Filter

IgnorePathSuffix returns a filter that rejects requests whose path ends with any of the given suffixes.

func IgnoreStatus added in v0.22.2

func IgnoreStatus(statuses ...int) Filter

IgnoreStatus returns a filter that rejects responses matching any of the given statuses.

func IgnoreStatusGreaterThan added in v0.22.2

func IgnoreStatusGreaterThan(status int) Filter

IgnoreStatusGreaterThan returns a filter that rejects responses with status greater than the given value.

func IgnoreStatusGreaterThanOrEqual added in v0.22.2

func IgnoreStatusGreaterThanOrEqual(status int) Filter

IgnoreStatusGreaterThanOrEqual returns a filter that rejects responses with status greater than or equal to the given value.

func IgnoreStatusLessThan added in v0.22.2

func IgnoreStatusLessThan(status int) Filter

IgnoreStatusLessThan returns a filter that rejects responses with status less than the given value.

func IgnoreStatusLessThanOrEqual added in v0.22.2

func IgnoreStatusLessThanOrEqual(status int) Filter

IgnoreStatusLessThanOrEqual returns a filter that rejects responses with status less than or equal to the given value.

type GinParser

type GinParser struct {
	Ctx *gin.Context
}

GinParser wraps a Gin context to implement the webFramework parser interface.

func InitContext

func InitContext(c any) GinParser

InitContext creates a GinParser from a Gin context.

func (GinParser) Abort added in v0.7.0

func (c GinParser) Abort() error

Abort aborts the Gin middleware chain.

func (GinParser) AddCustomAttributes added in v0.13.2

func (c GinParser) AddCustomAttributes(attr slog.Attr)

AddCustomAttributes adds a custom slog attribute to the request context.

func (GinParser) AddSpanAttribute added in v0.18.0

func (c GinParser) AddSpanAttribute(key, value string)

AddSpanAttribute adds a string key-value attribute to the current span.

func (GinParser) AddSpanAttributes added in v0.18.0

func (c GinParser) AddSpanAttributes(attrs map[string]string)

AddSpanAttributes adds multiple string key-value attributes to the current span.

func (GinParser) AddSpanEvent added in v0.18.0

func (c GinParser) AddSpanEvent(name string, attrs map[string]string)

AddSpanEvent records an event with attributes on the current span.

func (GinParser) CheckURLParam added in v0.28.1

func (c GinParser) CheckURLParam(name string) (string, bool)

CheckURLParam returns the URL parameter value and whether it exists.

func (GinParser) FileAttachment added in v0.10.4

func (c GinParser) FileAttachment(path, fileName string)

FileAttachment sends a file as an attachment in the response.

func (GinParser) FormValue added in v0.10.1

func (c GinParser) FormValue(name string) string

FormValue returns the value of the named form field.

func (GinParser) GetArgs

func (c GinParser) GetArgs(args ...any) map[string]string

GetArgs returns a map of common request arguments and additional URL parameters.

func (GinParser) GetBody

func (c GinParser) GetBody(target any) error

GetBody binds the JSON request body to the given target.

func (GinParser) GetContext added in v0.22.0

func (c GinParser) GetContext() context.Context

GetContext returns the context from the Gin request

func (GinParser) GetHTTPHeader added in v0.28.1

func (c GinParser) GetHTTPHeader() http.Header

GetHTTPHeader returns the HTTP request headers.

func (GinParser) GetHeader

func (c GinParser) GetHeader(target webFramework.HeaderInterface) error

GetHeader binds request headers to the given target struct.

func (GinParser) GetHeaderValue

func (c GinParser) GetHeaderValue(name string) string

GetHeaderValue returns the value of the named HTTP request header.

func (GinParser) GetLocal

func (c GinParser) GetLocal(name string) any

GetLocal returns the value stored in the Gin context under the given name.

func (GinParser) GetLocalString

func (c GinParser) GetLocalString(name string) string

GetLocalString returns the string value stored in the Gin context under the given name.

func (GinParser) GetMethod

func (c GinParser) GetMethod() string

GetMethod returns the HTTP method of the request.

func (GinParser) GetPath

func (c GinParser) GetPath() string

GetPath returns the full route path of the request.

func (GinParser) GetRawURLQuery added in v0.28.1

func (c GinParser) GetRawURLQuery() string

GetRawURLQuery returns the raw query string from the request URL.

func (GinParser) GetTraceContext added in v0.18.0

func (c GinParser) GetTraceContext() trace.SpanContext

GetTraceContext returns the trace span context from the Gin request context.

func (GinParser) GetURI added in v0.28.1

func (c GinParser) GetURI(target any) error

GetURI binds URI parameters to the given target.

func (GinParser) GetURLParam added in v0.28.1

func (c GinParser) GetURLParam(name string) string

GetURLParam returns the value of the named URL path parameter.

func (GinParser) GetURLParams added in v0.28.1

func (c GinParser) GetURLParams() map[string]string

GetURLParams returns all URL path parameters as a map.

func (GinParser) GetURLQuery added in v0.28.1

func (c GinParser) GetURLQuery(target any) error

GetURLQuery binds URL query parameters to the given target.

func (GinParser) Next added in v0.7.0

func (c GinParser) Next() error

Next calls the next handler in the Gin middleware chain.

func (GinParser) ParseCommand

func (c GinParser) ParseCommand(command, title string, request webFramework.RecordData, parser webFramework.FieldParser) string

ParseCommand parses a query command using request context values and field parsers.

func (GinParser) RecordSpanError added in v0.18.0

func (c GinParser) RecordSpanError(err error, attrs map[string]string)

RecordSpanError records an error with attributes on the current span.

func (GinParser) SaveFile added in v0.10.1

func (c GinParser) SaveFile(
	formTagName, path string,
) error

SaveFile saves an uploaded file from the form to the given path.

func (GinParser) SendJSONRespBody added in v0.7.0

func (c GinParser) SendJSONRespBody(status int, resp any) error

SendJSONRespBody sends a JSON response with the given status code and body.

func (GinParser) SetContext added in v0.22.0

func (c GinParser) SetContext(ctx context.Context)

SetContext updates the context in the Gin request

func (GinParser) SetLocal

func (c GinParser) SetLocal(name string, value any)

SetLocal stores a value in the Gin context under the given name.

func (GinParser) SetReqHeader added in v0.7.5

func (c GinParser) SetReqHeader(name string, value string)

SetReqHeader sets a value on the HTTP request header.

func (GinParser) SetRespHeader added in v0.10.28

func (c GinParser) SetRespHeader(name string, value string)

SetRespHeader sets a value on the HTTP response header.

func (GinParser) SetTraceContext added in v0.18.0

func (c GinParser) SetTraceContext(_ trace.SpanContext)

SetTraceContext is a no-op for Gin as trace context is handled by the context.

func (GinParser) StartSpan added in v0.18.0

func (c GinParser) StartSpan(_ string, _ ...trace.SpanStartOption) (context.Context, trace.Span)

StartSpan returns the current span from the request context.

Directories

Path Synopsis
Package gininitiator provides Gin engine initialization with middleware setup.
Package gininitiator provides Gin engine initialization with middleware setup.
Package logger provides Gin middleware logging integration.
Package logger provides Gin middleware logging integration.
ginsplunk
Package ginsplunk provides Gin Splunk logging middleware integration.
Package ginsplunk provides Gin Splunk logging middleware integration.

Jump to

Keyboard shortcuts

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