response

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2025 License: GPL-3.0 Imports: 3 Imported by: 0

README

Package response

This package provides utility functions to handle HTTP responses in Go applications, including sending JSON data, handling internal server errors, bad requests, and health checks.

Functions

1. SendJsonResponse

Sends a JSON response with the specified payload.

Parameters:
  • w http.ResponseWriter: The response writer interface.
  • payload interface{}: The payload to encode in the response body as JSON.
Example Usage:
var data map[string]interface{} = map[string]interface{}{
    "key": "value",
}
SendJsonResponse(w, data)
2. SendInternalServerError

Sends a generic internal server error response (HTTP status code 500).

Parameters:
  • w http.ResponseWriter: The response writer interface.
Example Usage:
SendInternalServerError(w)
3. SendBadRequest

Sends a bad request response with an optional message (HTTP status code 400).

Parameters:
  • w http.ResponseWriter: The response writer interface.
  • msg string: Optional additional error message to include in the response body.
Example Usage:
SendBadRequest(w, "Invalid input data")
4. HealthCheckHandler

Sends a health check response with an HTTP status code of 200 OK and a body containing "OK".

Parameters:
  • w http.ResponseWriter: The response writer interface.
  • r *http.Request: A pointer to the request information received from the client.
Example Usage:
func someRouteHandler(w http.ResponseWriter, r *http.Request) {
    if r.URL.Path == "/health" {
        HealthCheckHandler(w, r)
    }
}

Notes:

  • The package utilizes the alert function from the github.com/Miskamyasa/utils/alerts package to send alerts in case of errors.
  • Ensure that you have the necessary imports and a running HTTP server to test these functions.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HealthCheckHandler

func HealthCheckHandler(w http.ResponseWriter, r *http.Request)

HealthCheckHandler is a simple health check handler that responds with a 200 OK status.

func SendBadRequest

func SendBadRequest(w http.ResponseWriter, msg string)

SendBadRequest sends a 400 Bad Request response with a custom message.

func SendInternalServerError

func SendInternalServerError(w http.ResponseWriter)

SendInternalServerError sends a 500 Internal Server Error response.

func SendJsonResponse

func SendJsonResponse(w http.ResponseWriter, payload interface{})

SendJsonResponse sends a JSON response with the given payload and sets the Content-Type header to application/json.

Types

type Response added in v0.1.5

type Response struct {
	// contains filtered or unexported fields
}

func NewResponse added in v0.1.5

func NewResponse[T any](message string, payload T) Response

NewResponse creates a new Response instance with type parameter T for payload

Jump to

Keyboard shortcuts

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