-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
56 lines (45 loc) · 1.14 KB
/
storage.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
package main
import (
"strings"
"github.com/nu7hatch/gouuid"
)
// Loads the database into memory and populates the autocomplete structure
func LoadDatabase() {
db.load()
for host := range db.Hosts {
index.Put(host)
}
}
// Saves the database to a JSON file
func Save() {
go db.save()
}
// Automatically dumps the database to JSON every so often
func StartAutoSave() {
go db.autoSave()
}
// Returns up to 10 hostnames starting with the given prefix
func FindHostsByPrefix(prefix string) []string {
return index.Find(strings.ToLower(prefix), 10)
}
// Returns whether or not the given hostname is found in the data
func HasHost(hostname string) bool {
_, exists := GetHost(strings.ToLower(hostname))
return exists
}
// Gets all the data about a hostname from the database
func GetHost(hostname string) (Host, bool) {
host, exists := db.Hosts[strings.ToLower(hostname)]
return host, exists
}
// Returns a Version 4 UUID string
func UuidStr() string {
u, _ := uuid.NewV4()
return u.String()
}
var (
// The database holds all the app's data
db Database
// This autocomplete index makes it easy to suggest hosts by prefix
index Set = NewSet()
)