package utils import ( "crypto/hmac" "crypto/sha256" "encoding/hex" "fmt" "log" "math/rand" "os" "runtime" "strconv" "strings" "time" ) var root string func SetDevRoot(path string) { root = path + "/" } func Cwd(i int) string { _, fp, _, _ := runtime.Caller(i) slice := strings.Split(fp, "/") slice = slice[:len(slice)-1] path := strings.Join(slice, "/") path = strings.Replace(path, root, "", 1) return path } func Err(err error) { _, fp, line, _ := runtime.Caller(1) errStr := fmt.Sprintf("Error at %s:%d: %s\n", fp, line, err) fmt.Printf("%s", errStr) log, _ := os.OpenFile("error.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0655) defer log.Close() errLogStr := fmt.Sprintf("%s %s", time.Now(), errStr) log.WriteString(errLogStr) } func Hash(key string, data string) string { bKey := []byte(key) bData := []byte(data) h := hmac.New(sha256.New, bKey) h.Write(bData) return hex.EncodeToString(h.Sum(nil)) } func Salt(length int) string { const charset = "1234567890-=!@#$%^&*()_+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" b := make([]byte, length) for i := range b { b[i] = charset[rand.Intn(len(charset))] } return string(b) } func CheckArgs() { if len(os.Args) < 2 { log.Fatalf("ERROR: port number required: eg ./bin 3000") os.Exit(1) } _, err := strconv.Atoi(os.Args[1]) if err != nil { log.Fatalf("ERROR: port must be a number") os.Exit(1) } }