-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·134 lines (103 loc) · 3.4 KB
/
main.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package util
import (
"crypto/sha256"
"database/sql"
"encoding/hex"
"fmt"
"go/build"
"net/http"
"os"
"strings"
"time"
"github.com/metakeule/fmtdate"
)
const APIURL = "https://mynsb.nsbvisions.com"
var TIMEZONE *time.Location
func init() {
TIMEZONE, _ = time.LoadLocation("Australia/Sydney")
}
// ExistsString takes an array and an entry string and determines if that entry string resides in that array
func ExistsString(array []string, entry string) bool {
for _, b := range array {
if b == entry {
return true
}
}
return false
}
// ParseDate takes a date string, parses it and returns a time object
func ParseDate(date string) (time.Time, error){
return fmtdate.Parse("DD-MM-YYYY", date)
}
// ParseDateTime takes a datetime string, parses it and returns a time object
func ParseDateTime(datetime string) (time.Time, error) {
return fmtdate.Parse("DD-MM-YYYY hh:mm", datetime)
}
// GetGOPATH returns the system's first GOPATH variable
func GetGOPATH() string {
// NOTE... This is a hack, as soon as you can find a better option... use it PLEASE!!!
gopath := strings.Split(os.Getenv("GOPATH"), string(os.PathListSeparator))
if len(gopath) == 0 {
gopath = append(gopath, build.Default.GOPATH)
} else if gopath[0] == "" {
gopath[0] = build.Default.GOPATH
}
return gopath[0]
}
// NonNull determines if a string is empty, honestly just a code styling thing
func NonNull(thing string) bool {
return thing != ""
}
// IsSet behaves much like php's isset and takes a list of variables and determins if they are null
func IsSet(vars ...string) bool {
for _, varVal := range vars {
if !NonNull(varVal) {
return false
}
}
return true
}
// HashString takes an input string and hashes it with the sha256 algorithm, unfortunately not the one you would see in your hashtables ;) too big :p
func HashString(toHash string) string {
// Create a hasher
h := sha256.New()
// Write our data to it for hashing, in this case it is the string
h.Write([]byte(toHash))
// Create the final hash
sha256Hash := hex.EncodeToString(h.Sum(nil))
return sha256Hash
}
// IsSubset takes two arrays of strings and determines if the second array is a subset of the first
func IsSubset(first, second []string) bool {
for _, val := range first {
if !ExistsString(second, val) {
return false
}
}
return true
}
// HTTPResponse function for returning array based responses
func HTTPResponse(status int, statusMessage string, body string, title string, w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/json")
w.WriteHeader(status)
w.Write([]byte(fmt.Sprintf(`{"Status":{"Code": %d, "Status Message":"%s"},"Message": {"Title":"%s", "Body":[%s]}}`, status, statusMessage, title, body)))
}
// HTTPResponse function for returning object based responses
func HTTPResponseArr(status int, statusMessage string, body string, title string, w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/json")
w.WriteHeader(status)
w.Write([]byte(fmt.Sprintf(`{"Status":{"Code": %d, "Status Message":"%s"},"Message": {"Title":"%s", "Body":"%s"}}`, status, statusMessage, title, body)))
}
// NumResults takes a db and a query and determines how many results were returned from that query
func NumResults(db *sql.DB, query string, args ...interface{}) (int, error) {
rows, err := db.Query(query, args...)
if err != nil {
return 0, err
}
counter := 0
for rows.Next() {
counter += 1
}
rows.Next()
return counter, nil
}