utils

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateCollectionResponse

func CreateCollectionResponse(items interface{}, count int) map[string]interface{}

CreateCollectionResponse creates a standardized collection response with count

func CreateServer

func CreateServer(config *ServerConfig, handler http.Handler) *http.Server

CreateServer creates an HTTP server with the given configuration

func ExtractIDFromPath

func ExtractIDFromPath(r *http.Request, prefix string) (string, error)

func ExtractIDFromPathString

func ExtractIDFromPathString(path string) string

ExtractIDFromPathString extracts the ID from a URL path by taking the last segment

func ExtractQueryParam

func ExtractQueryParam(r *http.Request, param string) (string, error)

func GenericHandler

func GenericHandler(w http.ResponseWriter, r *http.Request, handler func() (interface{}, int, error))

GenericHandler handles generic requests without specific parameter extraction

func GetEnvOrDefault

func GetEnvOrDefault(key, defaultValue string) string

GetEnvOrDefault returns the environment variable value or a default

func HandleError

func HandleError(w http.ResponseWriter, err error, statusCode int, operation string)

Helper functions for common patterns

func HandleSuccess

func HandleSuccess(w http.ResponseWriter, data interface{}, statusCode int, operation string, logData map[string]interface{})

func HealthHandler

func HealthHandler(serviceName string) http.HandlerFunc

HealthHandler creates a health check handler

func JSONHandler

func JSONHandler(w http.ResponseWriter, r *http.Request, req interface{}, handler func() (interface{}, int, error))

JSONHandler handles JSON request/response with error handling

func PanicRecoveryMiddleware

func PanicRecoveryMiddleware(next http.Handler) http.Handler

PanicRecoveryMiddleware provides panic recovery for HTTP handlers

func ParseJSONRequest

func ParseJSONRequest(r *http.Request, target interface{}) error

ParseJSONRequest parses a JSON request body into the target struct

func PathHandler

func PathHandler(w http.ResponseWriter, r *http.Request, prefix string, handler func(string) (interface{}, int, error))

PathHandler handles path-based requests with parameter extraction

func ReadRequestBody

func ReadRequestBody(r *http.Request) ([]byte, error)

ReadRequestBody reads the entire request body as bytes

func RespondWithError

func RespondWithError(w http.ResponseWriter, statusCode int, message string)

RespondWithError sends a JSON error response

func RespondWithJSON

func RespondWithJSON(w http.ResponseWriter, statusCode int, data interface{})

RespondWithJSON sends a JSON response with the given status code and data

func RespondWithSuccess

func RespondWithSuccess(w http.ResponseWriter, statusCode int, data interface{})

RespondWithSuccess sends a JSON success response

func SetupLogging

func SetupLogging(format, level string)

SetupLogging configures logging based on the configuration

func StartServerWithGracefulShutdown

func StartServerWithGracefulShutdown(server *http.Server, serviceName string) error

StartServerWithGracefulShutdown starts an HTTP server and blocks until it stops. On SIGINT or SIGTERM it waits up to 30 seconds for active requests to finish before returning.

func ValidateMethod

func ValidateMethod(w http.ResponseWriter, r *http.Request, allowedMethod string) bool

Types

type ErrorResponse

type ErrorResponse struct {
	Error string `json:"error"`
}

ErrorResponse represents a standard error response structure

type ServerConfig

type ServerConfig struct {
	Port         string
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	IdleTimeout  time.Duration
}

ServerConfig holds configuration for HTTP servers

func DefaultServerConfig

func DefaultServerConfig() *ServerConfig

DefaultServerConfig returns a default server configuration

type SuccessResponse

type SuccessResponse struct {
	Message string      `json:"message,omitempty"`
	Data    interface{} `json:"data,omitempty"`
}

SuccessResponse represents a standard success response structure

Jump to

Keyboard shortcuts

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