util

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2018 License: MIT Imports: 15 Imported by: 5

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 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 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