You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
908 B
Go
49 lines
908 B
Go
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
|
|
}
|