statping: Index | Examples | Files

package utils

import ""

Package utils contains common methods used in most packages in Statping. This package contains multiple function like: Logging, encryption, type conversions, setting utils.Directory as the current directory, running local CMD commands, and creating/deleting files/folder.

You can overwrite the utils.Directory global variable by including STATPING_DIR environment variable to be an absolute path.

More info on:



Package Files

doc.go encryption.go file.go log.go metrics.go perlin.go replacer.go time.go utils.go utils_custom.go


const (
    B   = 0x100
    N   = 0x1000
    BM  = 0xff


var (
    Log = Logger.StandardLogger()

    LastLines   []*logRow
    LockLines   sync.Mutex
    VerboseMode int
var (
    // Directory returns the current path or the STATPING_DIR environment variable
    Directory string
var (
    StartTime = Now()

func CloseLogs Uses

func CloseLogs()

CloseLogs will close the log file correctly on shutdown

func Command Uses

func Command(name string, args ...string) (string, string, error)

Command will run a terminal command with 'sh -c COMMAND' and return stdout and errOut as strings

in, out, err := Command("sass assets/scss assets/css/base.css")

func CreateDirectory Uses

func CreateDirectory(directory string) error

CreateDirectory will attempt to create a directory


func DeleteDirectory Uses

func DeleteDirectory(directory string) error

DeleteDirectory will attempt to delete a directory and all contents inside


func DeleteFile Uses

func DeleteFile(file string) error

DeleteFile will attempt to delete a file


func DirWritable Uses

func DirWritable(path string) (bool, error)

func DurationReadable Uses

func DurationReadable(d time.Duration) string

DurationReadable will return a time.Duration into a human readable string // t := time.Duration(5 * time.Minute) // DurationReadable(t) // returns: 5 minutes


dur, _ := time.ParseDuration("25m")
readable := DurationReadable(dur)


25 minutes

func FileExists Uses

func FileExists(name string) bool

FileExists returns true if a file exists

exists := FileExists("assets/css/base.css")

func FolderExists Uses

func FolderExists(folder string) bool

FolderExists will return true if the folder exists

func FormatDuration Uses

func FormatDuration(d time.Duration) string

FormatDuration converts a time.Duration into a string

func GetLastLine Uses

func GetLastLine() *logRow

GetLastLine returns 1 line for a recent log entry

func Getenv Uses

func Getenv(key string, defaultValue interface{}) interface{}

func HashPassword Uses

func HashPassword(password string) string

HashPassword returns the bcrypt hash of a password string

func HttpRequest Uses

func HttpRequest(url, method string, content interface{}, headers []string, body io.Reader, timeout time.Duration, verifySSL bool) ([]byte, *http.Response, error)

HttpRequest is a global function to send a HTTP request // url - The URL for HTTP request // method - GET, POST, DELETE, PATCH // content - The HTTP request content type (text/plain, application/json, or nil) // headers - An array of Headers to be sent (KEY=VALUE) []string{"Authentication=12345", ...} // body - The body or form data to send with HTTP request // timeout - Specific duration to timeout on. time.Duration(30 * time.Seconds) // You can use a HTTP Proxy if you HTTP_PROXY environment variable

func InitLogs Uses

func InitLogs() error

InitLogs will create the '/logs' directory and creates a file '/logs/statup.log' for application logging

func NewSHA256Hash Uses

func NewSHA256Hash() string

NewSHA1Hash returns a random SHA1 hash based on a specific length

func Now Uses

func Now() time.Time

Now returns the UTC timestamp

func OpenFile Uses

func OpenFile(filePath string) (string, error)

func RandomString Uses

func RandomString(n int) string

RandomString generates a random string of n length

func RenameDirectory Uses

func RenameDirectory(fromDir string, toDir string) error

RenameDirectory will attempt rename a directory to a new name

func ReplaceTemplate Uses

func ReplaceTemplate(tmpl string, data interface{}) string

func SaveFile Uses

func SaveFile(filename string, data []byte) error

SaveFile will create a new file with data inside it

SaveFile("newfile.json", []byte('{"data": "success"}')

func Timezoner Uses

func Timezoner(t time.Time, zone float32) time.Time

Timezoner returns the time.Time with the user set timezone

func ToFields Uses

func ToFields(d ...interface{}) map[string]interface{}

ToFields accepts any amount of interfaces to create a new mapping for log.Fields. You will need to turn on verbose mode by starting Statping with "-v". This function will convert a struct of to the base struct name, and each field into it's own mapping, for example: type "*services.Service", on string field "Name" converts to "service_name=value". There is also an additional field called "_pointer" that will return the pointer hex value.

func ToInt Uses

func ToInt(s interface{}) int64

ToInt converts a int to a string

func ToString Uses

func ToString(s interface{}) string

ToString converts a int to a string


amount := 42



type Metrics Uses

type Metrics struct {
    Requests     int64
    Errors       int64
    Bytes        int64
    Milliseconds int64
    OnlineTime   time.Time

func GetHttpMetrics Uses

func GetHttpMetrics() *Metrics

func (*Metrics) Reset Uses

func (h *Metrics) Reset()

type Perlin Uses

type Perlin struct {
    // contains filtered or unexported fields

Perlin is the noise generator

func NewPerlin Uses

func NewPerlin(alpha, beta float64, n int, seed int64) *Perlin

func NewPerlinRandSource Uses

func NewPerlinRandSource(alpha, beta float64, n int, source rand.Source) *Perlin

func (*Perlin) Noise1D Uses

func (p *Perlin) Noise1D(x float64) float64

type Timestamp Uses

type Timestamp time.Time

func (Timestamp) Ago Uses

func (t Timestamp) Ago() string

Ago returns a human readable timestamp based on the Timestamp (time.Time) interface

type Timestamper Uses

type Timestamper interface {
    Ago() string

Package utils imports 28 packages (graph) and is imported by 10 packages. Updated 2020-03-28. Refresh now. Tools for package owners.