utils

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2019 License: MIT Imports: 10 Imported by: 0

README

go-utils

Golang Utils

Documentation

Index

Constants

View Source
const (
	SuccessStatusCode             = 200
	CreatedStatusCode             = 201
	FoundStatusCode               = 302
	BadRequestStatusCode          = 400
	UnauthorizedStatusCode        = 401
	NotFoundStatusCode            = 404
	MethodNotAllowedStatusCode    = 405
	InternalServerErrorStatusCode = 500

	SuccessStatus             = "200 OK"
	CreatedStatus             = "201 Created"
	FoundStatus               = "302 Found"
	BadReqStatus              = "400 Bad Request"
	UnauthorizedStatus        = "401 Unauthorized"
	NotFoundStatus            = "404 Not Found"
	MethodNotAllowed          = "405 Method Not Allowed"
	InternalServerErrorStatus = "500 Internal Server Error"

	JsonMarshalErrorStr   = "JSON Marshal Error"
	JsonUnmarshalErrorStr = "JSON Unmarshal Error"
	ApiAuthErrorStr       = "401 unauthorized. Please pass username and password to the API"
)

Variables

This section is empty.

Functions

func BasicAuthCheck

func BasicAuthCheck(r *http.Request) error

BasicAuthCheck check if basic authentication credentials are provided while making a http request The method return an error if something goes wrong

func NewError

func NewError(errStr string) error

NewError creates and returns a new error and returns it

func ReadRequestBody

func ReadRequestBody(r *http.Request, out interface{}) error

ReadRequestBody reads request body in json format, unmarshal's the json writes the data into the address

func WriteErrorResp

func WriteErrorResp(w http.ResponseWriter, r *http.Request, statusCode int, err error)

WriteErrResp writes an http error response and logs the outcome

func WriteHTTPResp

func WriteHTTPResp(w http.ResponseWriter, r *http.Request, responseCode int, response interface{})

WriteHTTPResp writes an http response

func WriteInfoResp

func WriteInfoResp(w http.ResponseWriter, r *http.Request, statusCode int, response string)

WriteInfoResp writes an http info response and logs the outcome

func WriteWarnResp

func WriteWarnResp(w http.ResponseWriter, r *http.Request, statusCode int, response string)

WriteWarnResp writes an http warn response and logs the outcome

Types

type ErrResponse

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

ErrResponse represents an error response

type Error added in v1.0.2

type Error struct {
	ErrStr string
	ErrMsg string
}

Error represents the error format

func (Error) NewError added in v1.0.2

func (e Error) NewError() error

NewError formats and returns a error

type Logger

type Logger struct {
	Request *http.Request
	Message interface{}
}

Logger represents the output log message

func (Logger) EnableDebug added in v1.0.1

func (l Logger) EnableDebug() bool

EnableDebug checks if the DEBUG_LOGS env variable is set to true

func (Logger) Error

func (l Logger) Error()

Error writes error logs

func (Logger) Info

func (l Logger) Info()

Info writes information logs

func (Logger) Warn

func (l Logger) Warn()

Warn writes warning logs

type Response

type Response struct {
	Message string `json:"message"`
}

Response represents a http response message

type StringSlice

type StringSlice []string

func (StringSlice) EntryExists

func (s StringSlice) EntryExists(entry string) bool

EntryExists checks if a string exists in a slice of string

Jump to

Keyboard shortcuts

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