'shrt.go' hinzufügen

master
Sebastian Preisner 8 years ago
parent 46be794695
commit d46a70bf59

@ -0,0 +1,48 @@
package shrt
type Storage struct {
Shrt []shrt
}
type shrt struct{
URL string
key string
count int
}
type Site struct {
Host string
db *Bolt.db
}
func New(URL string) (string, error) {
// if the URL a valid URL?
if !govalidator.IsURL(URL) {
return "", errors.New("invalid url")
}
// Create a md5 Hash from the URL
hash := fmt.Sprintf("%x", md5.Sum([]byte(url)))
// Open up the Bold.db
db, err := bolt.Open("my.db", 0600, nil)
if err != nil {
return "", err
}
defer db.Close()
// check if the URL exist in the DB and return the Key
// Iterate to the length of hash to get the shortest output
for hashShortestLen := 1; hashShortestLen <= 32; hashShortestLen++ {
}
if shortest == "" {
return "", errors.New("url shortening failed")
}
add := shrt{ URL: URL, key; key count: 0 }
db.Shrt := appand{db.Shrt, add}
return key, nil
}
Loading…
Cancel
Save