https

package
v0.0.0-...-5d322a5 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2023 License: MIT Imports: 7 Imported by: 0

README

https

The package contains a set of functions that helps to standarize returning HTTP responses to the client.

Every succeess message will return a response in the following JSON:

{
  "data" : "your data goes here"
}

The field data may contain everything that is marshallable: strings, numbers, structs, maps and arrays.

For error responses the output JSON will be in format

{
  "message": "your message"
}

A proper HTTP status code will be sent to the client as well.

Usage

To return an response with 200 status code use the https.OK() method.

https.OK(w, response)
Internal server error
https.InternalError(w, msg)
Not found error
https.NotFound(w, msg)
Any other error

To return any custom error (different from described above) you can use the Error() function as shown below.

https.Error(w, msg, http.StatusMethodNotAllowed)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BadRequest

func BadRequest(w http.ResponseWriter, errType string, title string)

func CORS

func CORS(w http.ResponseWriter)

func EmptyHandler

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

func Error

func Error(w http.ResponseWriter, resp ErrorResponse, code int)

func InternalError

func InternalError(w http.ResponseWriter, errType string, title string)

func NoContent

func NoContent(w http.ResponseWriter)

func NotFound

func NotFound(w http.ResponseWriter, errType string, title string)

func OK

func OK(w http.ResponseWriter, msg interface{})

func SessionID

func SessionID() string

Generate a random session ID

func Unauthorized

func Unauthorized(w http.ResponseWriter, errType string, title string)

func WrapPanic

func WrapPanic(fn http.HandlerFunc) http.HandlerFunc

Types

type ErrorResponse

type ErrorResponse struct {
	Type   string `json:"type,omitempty"`
	Detail string `json:"detail,omitempty"`
	Title  string `json:"title,omitempty"`
}

Jump to

Keyboard shortcuts

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