-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
44 lines (37 loc) · 1.04 KB
/
database.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
package main
import (
"math/rand"
"github.com/shomali11/xredis"
)
var client *xredis.Client
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const keyLen = 16
// Redirection struct to hold onto the data given
type Redirection struct {
URL string `json:"url" form:"url" query:"url"`
}
// Connect starts a connection to Redis and binds the client to the global var
// it also runs a Ping to make sure it works
func Connect() (string, error) {
client = xredis.DefaultClient()
return client.Ping()
}
// Save a new Url
func Save(url string) (string, error) {
key := GetRandKey()
_, err := client.SetNx(key, url)
return key, err
}
// GetRandKey gives us a random string
func GetRandKey() string {
b := make([]byte, keyLen)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}
// Find checks for the given redirect key and returns its validity and value
func Find(key string) (bool, string) {
url, keyExists, _ := client.Get(key)
return keyExists, url
}