Documentation
¶
Overview ¶
Package shared provides configuration, logging, error helpers, and constants used across dployrd. Shared, cross-cutting utilities live here to avoid duplication in other packages, including config for sync intervals and task deduplication TTLs.
Index ¶
- Constants
- Variables
- func EnrichContext(ctx context.Context) context.Context
- func Exec(ctx context.Context, cmd string, workDir string) error
- func GetToken() (string, error)
- func LoadTomlFile(path string) error
- func LogErrF(name, dir string, err error) error
- func LogInfoF(name, dir, message string) error
- func LogWarnF(name, dir, message string) error
- func RequestFromContext(ctx context.Context) (string, error)
- func RequestID(ctx context.Context) string
- func SanitizeSyncInterval(v time.Duration) time.Duration
- func TraceFromContext(ctx context.Context) (string, error)
- func TraceID(ctx context.Context) string
- func User(ctx context.Context) *store.User
- func UserFromContext(ctx context.Context) (*store.User, error)
- func WithRequest(ctx context.Context, id string) context.Context
- func WithTrace(ctx context.Context, id string) context.Context
- func WithUser(ctx context.Context, id string) context.Context
- func WriteError(w http.ResponseWriter, status int, code, message string, details any)
- func WriteJSON(w http.ResponseWriter, status int, v any)
- type AppError
- type Config
- type ContextKey
- type ContextMeta
- type ErrorCode
- type ErrorDescriptor
- type ErrorResponse
- type Logger
Constants ¶
const (
ServerHome string = "/usr/local/bin/dployr"
)
Variables ¶
var Errors = struct { Request struct { MissingParams ErrorDescriptor BadRequest ErrorDescriptor MethodNotAllowed ErrorDescriptor } Auth struct { Unauthorized ErrorDescriptor Forbidden ErrorDescriptor } Resource struct { NotFound ErrorDescriptor } Instance struct { RegistrationFailed ErrorDescriptor } Runtime struct { InternalServer ErrorDescriptor } }{ Request: struct { MissingParams ErrorDescriptor BadRequest ErrorDescriptor MethodNotAllowed ErrorDescriptor }{ MissingParams: ErrorDescriptor{Code: ErrorRequestMissingParams, HTTPStatus: http.StatusBadRequest, Message: "Missing required parameters"}, BadRequest: ErrorDescriptor{Code: ErrorRequestBadRequest, HTTPStatus: http.StatusBadRequest, Message: "Bad request"}, MethodNotAllowed: ErrorDescriptor{Code: ErrorRequestMethodNotAllowed, HTTPStatus: http.StatusMethodNotAllowed, Message: "Method not allowed"}, }, Auth: struct { Unauthorized ErrorDescriptor Forbidden ErrorDescriptor }{ Unauthorized: ErrorDescriptor{Code: ErrorAuthUnauthorized, HTTPStatus: http.StatusUnauthorized, Message: "Unauthorized"}, Forbidden: ErrorDescriptor{Code: ErrorAuthForbidden, HTTPStatus: http.StatusForbidden, Message: "Forbidden"}, }, Resource: struct { NotFound ErrorDescriptor }{ NotFound: ErrorDescriptor{Code: ErrorResourceNotFound, HTTPStatus: http.StatusNotFound, Message: "Resource not found"}, }, Runtime: struct { InternalServer ErrorDescriptor }{ InternalServer: ErrorDescriptor{Code: ErrorRuntimeInternalServer, HTTPStatus: http.StatusInternalServerError, Message: "Internal server error"}, }, Instance: struct { RegistrationFailed ErrorDescriptor }{ RegistrationFailed: ErrorDescriptor{Code: ErrorInstanceRegistrationFailed, HTTPStatus: http.StatusInternalServerError, Message: "Instance registration failed"}, }, }
Errors is a structured catalog of application errors that are currently used across the HTTP handlers.
Functions ¶
func LoadTomlFile ¶
func SanitizeSyncInterval ¶
SanitizeSyncInterval clamps a duration to safe sync bounds.
func WriteError ¶
func WriteError(w http.ResponseWriter, status int, code, message string, details any)
Types ¶
type Config ¶
type Config struct {
Address string
Port int
MaxWorkers int
BaseURL string
SyncInterval time.Duration
WSCertPath string
WSMaxMessageSize int64
TaskDedupTTL time.Duration
}
func LoadConfig ¶
type ContextKey ¶
type ContextKey string
const ( CtxUserIDKey ContextKey = "user_id" CtxRequestIDKey ContextKey = "request_id" CtxTraceIDKey ContextKey = "trace_id" )
type ContextMeta ¶
func Meta ¶
func Meta(ctx context.Context) *ContextMeta
type ErrorCode ¶
type ErrorCode string
ErrorCode is an identifier for an error that can be surfaced to clients and logged across services.
const ( // Request-level errors ErrorRequestMissingParams ErrorCode = "request.missing_params" ErrorRequestBadRequest ErrorCode = "request.bad_request" ErrorRequestMethodNotAllowed ErrorCode = "request.method_not_allowed" // Auth errors ErrorAuthForbidden ErrorCode = "auth.forbidden" // Resource errors ErrorResourceNotFound ErrorCode = "resource.not_found" // Runtime / internal errors ErrorRuntimeInternalServer ErrorCode = "runtime.internal_server_error" ErrorInstanceRegistrationFailed ErrorCode = "instance.registration_failed" )
type ErrorDescriptor ¶
ErrorDescriptor defines a specific error, with its code, HTTP status and human-readable message. It's the source of truth for HTTP responses and OpenAPI error objects.
type ErrorResponse ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger wraps slog with multi-sink (stdout + file) and context-aware logging.
func LogWithContext ¶
LogWithContext is a legacy helper; use logger.WithContext(ctx) instead. Kept for backward compatibility during migration.
func NewLogger ¶
func NewLogger() *Logger
NewLogger creates a unified logger that writes structured JSON to both stdout and a file.