util

package
v0.5.7 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: Apache-2.0 Imports: 26 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithLogger

func ContextWithLogger(ctx context.Context, l *log.Entry) context.Context

ContextWithLogger creates a new context, which will use the given logger.

func Copy

func Copy(src, dst string) (int64, error)

func CopyFile

func CopyFile(from, to string) error

CopyFile will copy the file to a other destination

func Decrypt

func Decrypt(key, text []byte) ([]byte, error)

func Encrypt

func Encrypt(key, text []byte) ([]byte, error)

func GenUUID

func GenUUID() (string, error)

func GenerateRandomBytes

func GenerateRandomBytes(n int) ([]byte, error)

GetRandomByte generate a random number

func GenerateRandomString

func GenerateRandomString(nBytes int) (string, error)

GetRandomString generate a random string with the size of nBytes

func GetFileContentType

func GetFileContentType(filePath string) (string, error)

GetFileContentType give out the mimetype of a file

func GetFileHash

func GetFileHash(filePath string) (string, error)

GetFileHash return a sha256 hashvalue of a file

func GetLogger

func GetLogger(ctx context.Context) *log.Entry

GetLogger retrieves the logrus logger from the supplied context. Always returns a logger, even if there wasn't one originally supplied.

func GetRequestID

func GetRequestID(ctx context.Context) string

GetRequestID returns the request ID associated with this context, or the empty string if one is not associated with this context.

func Getenv

func Getenv(key, fallback string) string

Getenv with default value

func MakeJSONAPI

func MakeJSONAPI(handler JSONRequestHandler) http.HandlerFunc

MakeJSONAPI creates an HTTP handler which always responds to incoming requests with JSON responses. Incoming http.Requests will have a logger (with a request ID/method/path logged) attached to the Context. This can be accessed via GetLogger(Context).

func MarkdownRender

func MarkdownRender(content string) string

MarkdownRender will render a markdown text to html

func MoveFile

func MoveFile(from, to string) error

MoveFile will move the file to a other destination

func PrettyJSON

func PrettyJSON(data []byte) string

func PrintValue

func PrintValue(message *string, length int) string

PrintValue this function will add spaces to a string, until the length of the string is like we need it thats useful to make the output more pretty

func PrintValueStr

func PrintValueStr(message string, length int) string

PrintValueStr a wrapper fo printValue to use strings and not string pointers

func Protect

func Protect(handler http.HandlerFunc) http.HandlerFunc

Protect panicking HTTP requests from taking down the entire process, and log them using the correct logger, returning a 500 with a JSON response rather than abruptly closing the connection. The http.Request MUST have a ctxValueLogger.

func RandomString

func RandomString(n int) string

RandomString generates a pseudo-random string of length n.

func RequestWithLogging

func RequestWithLogging(req *http.Request) *http.Request

RequestWithLogging sets up standard logging for http.Requests. http.Requests will have a logger (with a request ID/method/path logged) attached to the Context. This can be accessed via GetLogger(Context).

func SetCORSHeaders

func SetCORSHeaders(w http.ResponseWriter)

SetCORSHeaders sets unrestricted origin Access-Control headers on the response writer

func SetLogging

func SetLogging(logLevel string, enableSyslog bool, name string)

SetLogging sets the loglevel and text formating

func SortAndUnique

func SortAndUnique(data sort.Interface) int

SortAndUnique sorts a list and removes duplicate entries in place. Takes the same interface as sort.Sort Returns the length of the data without duplicates Uses the last occurrence of a duplicate. O(nlog(n))

func SplitString

func SplitString(src string) []string

SplitString will split a string or give just one value back src = the string to split return = array of all strings

func Unique

func Unique(data sort.Interface) int

Unique removes duplicate items from a sorted list in place. Takes the same interface as sort.Sort Returns the length of the data without duplicates Uses the last occurrence of a duplicate. O(n).

func UniqueStrings

func UniqueStrings(strings []string) []string

UniqueStrings turns a list of strings into a sorted list of unique strings. O(nlog(n))

func WithCORSOptions

func WithCORSOptions(handler http.HandlerFunc) http.HandlerFunc

WithCORSOptions intercepts all OPTIONS requests and responds with CORS headers. The request handler is not invoked when this happens.

Types

type JSONRequestHandler

type JSONRequestHandler interface {
	OnIncomingRequest(req *http.Request) JSONResponse
}

JSONRequestHandler represents an interface that must be satisfied in order to respond to incoming HTTP requests with JSON.

func NewJSONRequestHandler

func NewJSONRequestHandler(f func(req *http.Request) JSONResponse) JSONRequestHandler

NewJSONRequestHandler converts the given OnIncomingRequest function into a JSONRequestHandler

type JSONResponse

type JSONResponse struct {
	// HTTP status code.
	Code int
	// JSON represents the JSON that should be serialized and sent to the client
	JSON interface{}
	// Headers represent any headers that should be sent to the client
	Headers map[string]string
}

JSONResponse represents an HTTP response which contains a JSON body.

func ErrorResponse

func ErrorResponse(err error) JSONResponse

ErrorResponse returns an HTTP 500 JSONResponse with the stringified form of the given error.

func MessageResponse

func MessageResponse(code int, msg string) JSONResponse

MessageResponse returns a JSONResponse with a 'message' key containing the given text.

func RedirectResponse

func RedirectResponse(location string) JSONResponse

RedirectResponse returns a JSONResponse which 302s the client to the given location.

func (JSONResponse) Is2xx

func (r JSONResponse) Is2xx() bool

Is2xx returns true if the Code is between 200 and 299.

Jump to

Keyboard shortcuts

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