util

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2023 License: MIT Imports: 17 Imported by: 4

README

Util

Documentation Go Report Card

A set of frequently used Go utility functions.

# download library
$: go get github.com/faryon93/util

# place on top of your go file
import "github.com/faryon93/util"

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyMustBePEMEncoded = errors.New("must be PEM encoded PKCS1 or PKCS8 private key")
	ErrNotRSAPrivateKey    = errors.New("not a valid RSA private key")
)
View Source
var (
	ErrInvalidContentType = errors.New("invalid content type")
)

Functions

func GetHostname added in v1.0.1

func GetHostname(r *http.Request) string

GetHostname return the Host Header of an http request. The Host is stripped from any port.

func GetRemoteAddr

func GetRemoteAddr(r *http.Request) string

GetRemoteAddr returns the remote address of an http request. If an X-Forwarded-For Header is present the headers content is returned. Otherwise the src host of the ip packet is returned.

func IsAlphaNum added in v1.0.2

func IsAlphaNum(s string) bool

IsAlphaNum returns true if the given string is composed of letters and digits only. Otherwise false is returned.

func Jsonify

func Jsonify(w http.ResponseWriter, v interface{})

Jsonify writes the JSON representation of v to the supplied http.ResposeWriter. If an error occours while marshalling the http response will be an internal server error.

func ParseBody

func ParseBody(r *http.Request, v interface{}) error

ParseBody reads the body of the request and parses it into v.

func RandomString

func RandomString(n int) string

RandomString string returns a random string with the length n. Stolen from https://stackoverflow.com/questions/22892120/how-to-generate-a-random-tring-of-a-fixed-length-in-golang

func ReadRSAPemKey added in v1.0.1

func ReadRSAPemKey(path string) (*rsa.PrivateKey, error)

ReadRSAPemKey read the PEM encoded RSA private key from the filesystem.

func StripPort added in v1.0.1

func StripPort(hostport string) string

StripPort removes the port (:XXXXX) part of a hostname.

func WaitSignal

func WaitSignal(sig ...os.Signal)

WaitSignal blocks until one of the given OS signals occoures.

Types

type FlagArray added in v1.1.0

type FlagArray []string

FlagArray is a type which could be used with the flag package, to obtain a list from multiple flags with the same name but different values. e.g.: ./test --foo=bar --foo=test --foo=x

func (*FlagArray) Set added in v1.1.0

func (f *FlagArray) Set(value string) error

func (*FlagArray) String added in v1.1.0

func (f *FlagArray) String() string

type RequestBody added in v1.0.1

type RequestBody struct {
	*bytes.Buffer
}

RequestBody represents the body of a request which was saved with SaveRequestBody().

func SaveRequestBody added in v1.0.1

func SaveRequestBody(r *http.Request) *RequestBody

SaveRequestBody saves the body of a request in order to restore it later.

func (*RequestBody) Restore added in v1.0.1

func (b *RequestBody) Restore(r *http.Request) *http.Request

Restore reinserts the saved body into the given request.

Jump to

Keyboard shortcuts

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