-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
42 lines (37 loc) · 842 Bytes
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package martiniframework
import (
"crypto/md5"
"encoding/json"
"fmt"
"log"
"time"
)
var (
// IsRunning specifies whether or not the application is running.
// It governs the SleepFor function.
IsRunning = true
)
// Md5hash is a convenience function creating and returing an MD5 hash
// for a specified string.
func Md5hash(orig string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(orig)))
}
func SleepFor(sec int64) {
for i := 0; i < int(sec); i++ {
if !IsRunning {
return
}
time.Sleep(time.Second)
}
}
// JsonEncode is a convenience function wrapping the serialization of an
// interface{} to its JSON byte equivalent. If an error occurs, "false"
// is returned.
func JsonEncode(o interface{}) []byte {
b, err := json.Marshal(o)
if err != nil {
log.Print(err.Error())
return []byte("false")
}
return b
}