appcontext

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 5 Imported by: 2

README

appcontext — request-scoped context value helpers

import "github.com/downsized-devs/sdk-go/appcontext"

Stability: Stable — see STABILITY.md

Typed get/set helpers for the values logger, audit, and others expect to find on context.Context — request ID, user ID, accept-language, service version, device type, app response code.

Features

  • Setter/getter pairs with private context keys (no collisions with other packages)
  • Read by logger for automatic field enrichment
  • Zero external dependencies

Installation

go get github.com/downsized-devs/sdk-go/appcontext

Quick Start

package main

import (
    "context"

    "github.com/downsized-devs/sdk-go/appcontext"
)

func main() {
    ctx := context.Background()
    ctx = appcontext.SetRequestId(ctx, "req-abc")
    ctx = appcontext.SetUserId(ctx, 42)

    _ = appcontext.GetRequestId(ctx) // "req-abc"
    _ = appcontext.GetUserId(ctx)    // 42
}

API Reference

Method Purpose
SetRequestId / GetRequestId Request correlation ID.
SetUserId / GetUserId Authenticated user identifier.
SetAcceptLanguage / GetAcceptLanguage Negotiated locale for response rendering.
SetServiceVersion / GetServiceVersion Service version stamp (build tag, git sha).
SetDeviceType / GetDeviceType Client device class.
SetAppResponseCode / GetAppResponseCode Pinned response code for late middleware.

All Set* return a new context.Context; the original is not mutated.

Examples

Gin middleware that populates everything from headers
func ContextEnricher() gin.HandlerFunc {
    return func(c *gin.Context) {
        ctx := c.Request.Context()
        ctx = appcontext.SetRequestId(ctx, c.GetHeader(header.RequestID))
        ctx = appcontext.SetAcceptLanguage(ctx, c.GetHeader(header.AcceptLanguage))
        ctx = appcontext.SetServiceVersion(ctx, buildSHA)
        c.Request = c.Request.WithContext(ctx)
        c.Next()
    }
}

Error Handling

Getters return the zero value when the key is unset; no error is returned. Callers that require a value should validate at their own boundary.

Dependencies

Testing

go test ./appcontext/...

Contributing

See CONTRIBUTING.md. Adding a new key is additive — pair every Set* with a Get* and update logger if the field should appear in log lines automatically.

  • logger — primary consumer.
  • header — string constants for HTTP header names.
  • translator — reads AcceptLanguage.
  • audit — reads RequestId and UserId.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAcceptLanguage

func GetAcceptLanguage(ctx context.Context) string

func GetAppErrorMessage

func GetAppErrorMessage(ctx context.Context) string

func GetAppResponseCode

func GetAppResponseCode(ctx context.Context) codes.Code

func GetAuthToken

func GetAuthToken(ctx context.Context) string

func GetCacheControl

func GetCacheControl(ctx context.Context) bool

func GetDeviceType

func GetDeviceType(ctx context.Context) string

func GetEventDescription

func GetEventDescription(ctx context.Context) string

func GetEventName

func GetEventName(ctx context.Context) string

func GetRequestBody

func GetRequestBody(ctx context.Context) any

func GetRequestIP

func GetRequestIP(ctx context.Context) string

func GetRequestId

func GetRequestId(ctx context.Context) string

func GetRequestMethod

func GetRequestMethod(ctx context.Context) string

func GetRequestQuery

func GetRequestQuery(ctx context.Context) string

func GetRequestStartTime

func GetRequestStartTime(ctx context.Context) time.Time

func GetRequestURI

func GetRequestURI(ctx context.Context) string

func GetResponseHttpCode

func GetResponseHttpCode(ctx context.Context) int

func GetServiceName

func GetServiceName(ctx context.Context) string

func GetServiceVersion

func GetServiceVersion(ctx context.Context) string

func GetUserAgent

func GetUserAgent(ctx context.Context) string

func GetUserId

func GetUserId(ctx context.Context) int

func SetAcceptLanguage

func SetAcceptLanguage(ctx context.Context, lang string) context.Context

func SetAppErrorMessage

func SetAppErrorMessage(ctx context.Context, errMsg string) context.Context

func SetAppResponseCode

func SetAppResponseCode(ctx context.Context, code codes.Code) context.Context

func SetAuthToken

func SetAuthToken(ctx context.Context, token string) context.Context

func SetCacheControl

func SetCacheControl(ctx context.Context, cache string) context.Context

func SetDeviceType

func SetDeviceType(ctx context.Context, platform string) context.Context

func SetEventDescription

func SetEventDescription(ctx context.Context, description string) context.Context

func SetEventName

func SetEventName(ctx context.Context, name string) context.Context

func SetRequestBody

func SetRequestBody(ctx context.Context, body any) context.Context

func SetRequestIP

func SetRequestIP(ctx context.Context, ip string) context.Context

func SetRequestId

func SetRequestId(ctx context.Context, rid string) context.Context

func SetRequestMethod

func SetRequestMethod(ctx context.Context, method string) context.Context

func SetRequestQuery

func SetRequestQuery(ctx context.Context, query string) context.Context

func SetRequestStartTime

func SetRequestStartTime(ctx context.Context, t time.Time) context.Context

func SetRequestURI

func SetRequestURI(ctx context.Context, uri string) context.Context

func SetResponseHttpCode

func SetResponseHttpCode(ctx context.Context, code int) context.Context

func SetServiceName

func SetServiceName(ctx context.Context, val string) context.Context

func SetServiceVersion

func SetServiceVersion(ctx context.Context, version string) context.Context

func SetUserAgent

func SetUserAgent(ctx context.Context, ua string) context.Context

func SetUserId

func SetUserId(ctx context.Context, ui int) context.Context

Types

This section is empty.

Jump to

Keyboard shortcuts

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