Documentation
¶
Index ¶
- func CreateCollectionResponse(items interface{}, count int) map[string]interface{}
- func CreateServer(config *ServerConfig, handler http.Handler) *http.Server
- func ExtractIDFromPath(r *http.Request, prefix string) (string, error)
- func ExtractIDFromPathString(path string) string
- func ExtractQueryParam(r *http.Request, param string) (string, error)
- func GenericHandler(w http.ResponseWriter, r *http.Request, ...)
- func GetEnvOrDefault(key, defaultValue string) string
- func HandleError(w http.ResponseWriter, err error, statusCode int, operation string)
- func HandleSuccess(w http.ResponseWriter, data interface{}, statusCode int, operation string, ...)
- func HealthHandler(serviceName string) http.HandlerFunc
- func JSONHandler(w http.ResponseWriter, r *http.Request, req interface{}, ...)
- func PanicRecoveryMiddleware(next http.Handler) http.Handler
- func ParseJSONRequest(r *http.Request, target interface{}) error
- func PathHandler(w http.ResponseWriter, r *http.Request, prefix string, ...)
- func ReadRequestBody(r *http.Request) ([]byte, error)
- func RespondWithError(w http.ResponseWriter, statusCode int, message string)
- func RespondWithJSON(w http.ResponseWriter, statusCode int, data interface{})
- func RespondWithSuccess(w http.ResponseWriter, statusCode int, data interface{})
- func SetupLogging(format, level string)
- func StartServerWithGracefulShutdown(server *http.Server, serviceName string) error
- func ValidateMethod(w http.ResponseWriter, r *http.Request, allowedMethod string) bool
- type ErrorResponse
- type ServerConfig
- type SuccessResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateCollectionResponse ¶
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 ExtractIDFromPathString ¶
ExtractIDFromPathString extracts the ID from a URL path by taking the last segment
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 ¶
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 ¶
PanicRecoveryMiddleware provides panic recovery for HTTP handlers
func ParseJSONRequest ¶
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 ¶
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 ¶
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 ¶
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