From d46a70bf597eb034e42029b542760ad5172de0d8 Mon Sep 17 00:00:00 2001 From: Sebastian Preisner Date: Fri, 5 May 2017 07:37:03 +0000 Subject: [PATCH] =?UTF-8?q?'shrt.go'=20hinzuf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shrt.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 shrt.go diff --git a/shrt.go b/shrt.go new file mode 100644 index 0000000..c99a333 --- /dev/null +++ b/shrt.go @@ -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 +}