Documentation
¶
Overview ¶
Package libNetHttp provides a net/http web framework adapter for requestCore.
Index ¶
- func AddSpanAttribute(ctx context.Context, key, value string)
- func AddSpanAttributes(ctx context.Context, attrs map[string]string)
- func AddSpanEvent(ctx context.Context, name string, attrs map[string]string)
- func CustomTracingMiddleware(tm *libTracing.TracingManager) func(http.Handler) http.Handler
- func GetSpanFromContext(ctx context.Context) trace.Span
- func NetHTTPErrorHandler(path, title string, handler ContextInitiator) http.HandlerFunc
- func NetHTTPHandler(handler any) http.HandlerFunc
- func RecordSpanError(ctx context.Context, err error, attrs map[string]string)
- func RequestFromContext(ctx context.Context) (*http.Request, bool)
- func ResponseWriterFromContext(ctx context.Context) (http.ResponseWriter, bool)
- func TracingMiddleware() func(http.Handler) http.Handler
- func URLParamsFromRequest(r *http.Request) map[string]string
- func WithRequestResponse(ctx context.Context, r *http.Request, w http.ResponseWriter) context.Context
- func WithURLParams(r *http.Request, params map[string]string) *http.Request
- type ContextInitiator
- type Middleware
- type NetHTTPParser
- func (c NetHTTPParser) Abort() error
- func (c NetHTTPParser) AddCustomAttributes(attr slog.Attr)
- func (c *NetHTTPParser) AddParam(key, value string)
- func (c NetHTTPParser) AddSpanAttribute(key, value string)
- func (c NetHTTPParser) AddSpanAttributes(attrs map[string]string)
- func (c NetHTTPParser) AddSpanEvent(name string, attrs map[string]string)
- func (c NetHTTPParser) CheckURLParam(name string) (string, bool)
- func (c NetHTTPParser) FileAttachment(path, fileName string)
- func (c NetHTTPParser) FormValue(name string) string
- func (c NetHTTPParser) GetArgs(args ...any) map[string]string
- func (c NetHTTPParser) GetBody(target any) error
- func (c NetHTTPParser) GetContext() context.Context
- func (c NetHTTPParser) GetCookie(name string) (*http.Cookie, error)
- func (c NetHTTPParser) GetCookies() []*http.Cookie
- func (c NetHTTPParser) GetFormValue(key string) string
- func (c NetHTTPParser) GetFormValues(key string) []string
- func (c NetHTTPParser) GetHTTPHeader() http.Header
- func (c NetHTTPParser) GetHeader(target webFramework.HeaderInterface) error
- func (c NetHTTPParser) GetHeaderValue(name string) string
- func (c NetHTTPParser) GetLocal(name string) any
- func (c NetHTTPParser) GetLocalString(name string) string
- func (c NetHTTPParser) GetMethod() string
- func (c NetHTTPParser) GetPath() string
- func (c NetHTTPParser) GetPostFormValue(key string) string
- func (c NetHTTPParser) GetPostFormValues(key string) []string
- func (c NetHTTPParser) GetRawURLQuery() string
- func (c NetHTTPParser) GetTraceContext() trace.SpanContext
- func (c NetHTTPParser) GetURI(target any) error
- func (c NetHTTPParser) GetURLParam(name string) string
- func (c NetHTTPParser) GetURLParams() map[string]string
- func (c NetHTTPParser) GetURLQuery(target any) error
- func (c NetHTTPParser) Next() error
- func (c NetHTTPParser) ParseCommand(command, title string, request webFramework.RecordData, ...) string
- func (c NetHTTPParser) ParseForm() error
- func (c NetHTTPParser) ParseMultipartForm(maxMemory int64) error
- func (c NetHTTPParser) RecordSpanError(err error, attrs map[string]string)
- func (c NetHTTPParser) Redirect(url string, statusCode int)
- func (c NetHTTPParser) SaveFile(formTagName, path string) error
- func (c NetHTTPParser) SendJSONRespBody(status int, resp any) error
- func (c NetHTTPParser) ServeContent(name string, modtime time.Time, content io.ReadSeeker)
- func (c NetHTTPParser) ServeFile(name string)
- func (c *NetHTTPParser) SetContext(ctx context.Context)
- func (c NetHTTPParser) SetCookie(cookie *http.Cookie)
- func (c NetHTTPParser) SetLocal(name string, value any)
- func (c *NetHTTPParser) SetParams(params map[string]string)
- func (c NetHTTPParser) SetReqHeader(name string, value string)
- func (c NetHTTPParser) SetRespHeader(name string, value string)
- func (c NetHTTPParser) SetTraceContext(_ trace.SpanContext)
- func (c NetHTTPParser) StartSpan(_ string, _ ...trace.SpanStartOption) (context.Context, trace.Span)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddSpanAttribute ¶ added in v0.18.0
AddSpanAttribute adds an attribute to the current span
func AddSpanAttributes ¶ added in v0.18.0
AddSpanAttributes adds multiple attributes to the current span
func AddSpanEvent ¶ added in v0.18.0
AddSpanEvent adds an event to the current span
func CustomTracingMiddleware ¶ added in v0.18.0
func CustomTracingMiddleware(tm *libTracing.TracingManager) func(http.Handler) http.Handler
CustomTracingMiddleware creates a custom net/http middleware with more control
func GetSpanFromContext ¶ added in v0.18.0
GetSpanFromContext gets the span from context
func NetHTTPErrorHandler ¶ added in v0.28.1
func NetHTTPErrorHandler(path, title string, handler ContextInitiator) http.HandlerFunc
NetHTTPErrorHandler returns an http.HandlerFunc that handles common HTTP errors for the given path.
func NetHTTPHandler ¶ added in v0.28.1
func NetHTTPHandler(handler any) http.HandlerFunc
NetHTTPHandler wraps a handler function to work with net/http
func RecordSpanError ¶ added in v0.18.0
RecordSpanError records an error in the current span
func RequestFromContext ¶ added in v0.23.0
RequestFromContext retrieves the HTTP request stored in the context.
func ResponseWriterFromContext ¶ added in v0.23.0
func ResponseWriterFromContext(ctx context.Context) (http.ResponseWriter, bool)
ResponseWriterFromContext retrieves the HTTP response writer stored in the context.
func TracingMiddleware ¶ added in v0.18.0
TracingMiddleware creates net/http middleware for OpenTelemetry tracing
func URLParamsFromRequest ¶ added in v0.23.0
URLParamsFromRequest extracts URL parameters from the request context.
func WithRequestResponse ¶ added in v0.23.0
func WithRequestResponse(ctx context.Context, r *http.Request, w http.ResponseWriter) context.Context
WithRequestResponse stores the HTTP request and response writer in the context.
Types ¶
type ContextInitiator ¶
type ContextInitiator interface {
InitContext(r *http.Request, w http.ResponseWriter) webFramework.WebFramework
Respond(int, int, string, any, bool, webFramework.WebFramework)
}
ContextInitiator defines the interface for initializing a web framework context and responding.
type Middleware ¶
Middleware function type for net/http
func CORSMiddleware ¶
func CORSMiddleware() Middleware
CORSMiddleware returns a net/http middleware that sets CORS headers and handles preflight requests.
func ChainMiddleware ¶
func ChainMiddleware(middlewares ...Middleware) Middleware
ChainMiddleware chains multiple middleware functions
func LoggingMiddleware ¶
func LoggingMiddleware() Middleware
LoggingMiddleware returns a net/http middleware that logs each incoming request.
func RecoveryMiddleware ¶
func RecoveryMiddleware() Middleware
RecoveryMiddleware returns a net/http middleware that recovers from panics.
type NetHTTPParser ¶ added in v0.28.1
type NetHTTPParser struct {
Request *http.Request
Response http.ResponseWriter
Locals map[string]any
Params map[string]string
}
NetHTTPParser implements the webFramework.RequestParser interface for net/http.
func InitContext ¶
func InitContext(r *http.Request, w http.ResponseWriter) *NetHTTPParser
InitContext creates a new NetHTTPParser from the given HTTP request and response writer.
func (NetHTTPParser) Abort ¶ added in v0.28.1
func (c NetHTTPParser) Abort() error
Abort stops the middleware chain by writing an internal-server-error status.
func (NetHTTPParser) AddCustomAttributes ¶ added in v0.28.1
func (c NetHTTPParser) AddCustomAttributes(attr slog.Attr)
AddCustomAttributes stores a custom slog attribute in the parser's local map.
func (*NetHTTPParser) AddParam ¶ added in v0.28.1
func (c *NetHTTPParser) AddParam(key, value string)
AddParam adds a single URL parameter
func (NetHTTPParser) AddSpanAttribute ¶ added in v0.28.1
func (c NetHTTPParser) AddSpanAttribute(key, value string)
AddSpanAttribute adds a single string attribute to the current tracing span.
func (NetHTTPParser) AddSpanAttributes ¶ added in v0.28.1
func (c NetHTTPParser) AddSpanAttributes(attrs map[string]string)
AddSpanAttributes adds multiple string attributes to the current tracing span.
func (NetHTTPParser) AddSpanEvent ¶ added in v0.28.1
func (c NetHTTPParser) AddSpanEvent(name string, attrs map[string]string)
AddSpanEvent adds an event with attributes to the current tracing span.
func (NetHTTPParser) CheckURLParam ¶ added in v0.28.1
func (c NetHTTPParser) CheckURLParam(name string) (string, bool)
CheckURLParam returns a URL path parameter by name and whether it exists.
func (NetHTTPParser) FileAttachment ¶ added in v0.28.1
func (c NetHTTPParser) FileAttachment(path, fileName string)
FileAttachment sends a file as an HTTP attachment with the given filename.
func (NetHTTPParser) FormValue ¶ added in v0.28.1
func (c NetHTTPParser) FormValue(name string) string
FormValue returns the first form value for the given field name.
func (NetHTTPParser) GetArgs ¶ added in v0.28.1
func (c NetHTTPParser) GetArgs(args ...any) map[string]string
GetArgs returns a map of common request arguments including user, app, action, and path.
func (NetHTTPParser) GetBody ¶ added in v0.28.1
func (c NetHTTPParser) GetBody(target any) error
GetBody reads and unmarshals the request body into the target.
func (NetHTTPParser) GetContext ¶ added in v0.28.1
func (c NetHTTPParser) GetContext() context.Context
GetContext returns the context from the HTTP request
func (NetHTTPParser) GetCookie ¶ added in v0.28.1
func (c NetHTTPParser) GetCookie(name string) (*http.Cookie, error)
GetCookie gets a cookie by name
func (NetHTTPParser) GetCookies ¶ added in v0.28.1
func (c NetHTTPParser) GetCookies() []*http.Cookie
GetCookies gets all cookies
func (NetHTTPParser) GetFormValue ¶ added in v0.28.1
func (c NetHTTPParser) GetFormValue(key string) string
GetFormValue gets form value
func (NetHTTPParser) GetFormValues ¶ added in v0.28.1
func (c NetHTTPParser) GetFormValues(key string) []string
GetFormValues gets all form values for a key
func (NetHTTPParser) GetHTTPHeader ¶ added in v0.28.1
func (c NetHTTPParser) GetHTTPHeader() http.Header
GetHTTPHeader returns the full HTTP header map from the request.
func (NetHTTPParser) GetHeader ¶ added in v0.28.1
func (c NetHTTPParser) GetHeader(target webFramework.HeaderInterface) error
GetHeader populates the target struct with header values from the request.
func (NetHTTPParser) GetHeaderValue ¶ added in v0.28.1
func (c NetHTTPParser) GetHeaderValue(name string) string
GetHeaderValue returns the value of a single request header by name.
func (NetHTTPParser) GetLocal ¶ added in v0.28.1
func (c NetHTTPParser) GetLocal(name string) any
GetLocal returns a value stored in the parser's local map by name.
func (NetHTTPParser) GetLocalString ¶ added in v0.28.1
func (c NetHTTPParser) GetLocalString(name string) string
GetLocalString returns a string value stored in the parser's local map by name.
func (NetHTTPParser) GetMethod ¶ added in v0.28.1
func (c NetHTTPParser) GetMethod() string
GetMethod returns the HTTP method of the request.
func (NetHTTPParser) GetPath ¶ added in v0.28.1
func (c NetHTTPParser) GetPath() string
GetPath returns the URL path of the request.
func (NetHTTPParser) GetPostFormValue ¶ added in v0.28.1
func (c NetHTTPParser) GetPostFormValue(key string) string
GetPostFormValue gets POST form value
func (NetHTTPParser) GetPostFormValues ¶ added in v0.28.1
func (c NetHTTPParser) GetPostFormValues(key string) []string
GetPostFormValues gets all POST form values for a key
func (NetHTTPParser) GetRawURLQuery ¶ added in v0.28.1
func (c NetHTTPParser) GetRawURLQuery() string
GetRawURLQuery returns the raw query string from the request URL.
func (NetHTTPParser) GetTraceContext ¶ added in v0.28.1
func (c NetHTTPParser) GetTraceContext() trace.SpanContext
GetTraceContext returns the trace span context from the net/http request context.
func (NetHTTPParser) GetURI ¶ added in v0.28.1
func (c NetHTTPParser) GetURI(target any) error
GetURI parses URL path parameters into the target struct.
func (NetHTTPParser) GetURLParam ¶ added in v0.28.1
func (c NetHTTPParser) GetURLParam(name string) string
GetURLParam returns a single URL path parameter by name.
func (NetHTTPParser) GetURLParams ¶ added in v0.28.1
func (c NetHTTPParser) GetURLParams() map[string]string
GetURLParams returns all URL path parameters as a map.
func (NetHTTPParser) GetURLQuery ¶ added in v0.28.1
func (c NetHTTPParser) GetURLQuery(target any) error
GetURLQuery parses URL query parameters into the target struct.
func (NetHTTPParser) Next ¶ added in v0.28.1
func (c NetHTTPParser) Next() error
Next advances to the next middleware in the chain (no-op for net/http).
func (NetHTTPParser) ParseCommand ¶ added in v0.28.1
func (c NetHTTPParser) ParseCommand(command, title string, request webFramework.RecordData, parser webFramework.FieldParser) string
ParseCommand parses a DML command template using local values and the provided request data.
func (NetHTTPParser) ParseForm ¶ added in v0.28.1
func (c NetHTTPParser) ParseForm() error
ParseForm parses form data
func (NetHTTPParser) ParseMultipartForm ¶ added in v0.28.1
func (c NetHTTPParser) ParseMultipartForm(maxMemory int64) error
ParseMultipartForm parses multipart form data ParseMultipartForm parses multipart form data from the request with the given max memory.
func (NetHTTPParser) RecordSpanError ¶ added in v0.28.1
func (c NetHTTPParser) RecordSpanError(err error, attrs map[string]string)
RecordSpanError records an error with attributes on the current tracing span.
func (NetHTTPParser) Redirect ¶ added in v0.28.1
func (c NetHTTPParser) Redirect(url string, statusCode int)
Redirect redirects to a URL
func (NetHTTPParser) SaveFile ¶ added in v0.28.1
func (c NetHTTPParser) SaveFile(formTagName, path string) error
SaveFile saves an uploaded file from the given form tag to the specified path.
func (NetHTTPParser) SendJSONRespBody ¶ added in v0.28.1
func (c NetHTTPParser) SendJSONRespBody(status int, resp any) error
SendJSONRespBody writes a JSON response with the given HTTP status code.
func (NetHTTPParser) ServeContent ¶ added in v0.28.1
func (c NetHTTPParser) ServeContent(name string, modtime time.Time, content io.ReadSeeker)
ServeContent serves content
func (NetHTTPParser) ServeFile ¶ added in v0.28.1
func (c NetHTTPParser) ServeFile(name string)
ServeFile serves a file
func (*NetHTTPParser) SetContext ¶ added in v0.28.1
func (c *NetHTTPParser) SetContext(ctx context.Context)
SetContext updates the context in the HTTP request. It uses a pointer receiver because http.Request.WithContext returns a new *http.Request and the mutation must be visible to callers.
func (NetHTTPParser) SetCookie ¶ added in v0.28.1
func (c NetHTTPParser) SetCookie(cookie *http.Cookie)
SetCookie sets a cookie
func (NetHTTPParser) SetLocal ¶ added in v0.28.1
func (c NetHTTPParser) SetLocal(name string, value any)
SetLocal stores a value in the parser's local map by name.
func (*NetHTTPParser) SetParams ¶ added in v0.28.1
func (c *NetHTTPParser) SetParams(params map[string]string)
SetParams sets URL parameters (useful for routing)
func (NetHTTPParser) SetReqHeader ¶ added in v0.28.1
func (c NetHTTPParser) SetReqHeader(name string, value string)
SetReqHeader sets a request header value by name.
func (NetHTTPParser) SetRespHeader ¶ added in v0.28.1
func (c NetHTTPParser) SetRespHeader(name string, value string)
SetRespHeader sets a response header value by name.
func (NetHTTPParser) SetTraceContext ¶ added in v0.28.1
func (c NetHTTPParser) SetTraceContext(_ trace.SpanContext)
SetTraceContext sets the trace span context on the request (no-op for net/http).
func (NetHTTPParser) StartSpan ¶ added in v0.28.1
func (c NetHTTPParser) StartSpan(_ string, _ ...trace.SpanStartOption) (context.Context, trace.Span)
StartSpan starts a new tracing span from the request context.