|
|
|
@ -1,26 +1,26 @@
|
|
|
|
|
package shrt
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"bytes"
|
|
|
|
|
"crypto/sha256"
|
|
|
|
|
"errors"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"net/url"
|
|
|
|
|
"os"
|
|
|
|
|
"io"
|
|
|
|
|
"bytes"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Storage struct {
|
|
|
|
|
Token map[string]*shrt `json:"token"`
|
|
|
|
|
Token map[string]*Shrt `json:"token"`
|
|
|
|
|
Url map[string]string `json:"url"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type shrt struct {
|
|
|
|
|
type Shrt struct {
|
|
|
|
|
URL string `json:"url"`
|
|
|
|
|
Datum string `json:"datum"`
|
|
|
|
|
Count int64 `json:"count"`
|
|
|
|
|
Date string `json:"datum"`
|
|
|
|
|
Clicks int64 `json:"clicks"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
@ -31,7 +31,7 @@ var (
|
|
|
|
|
|
|
|
|
|
// Open up
|
|
|
|
|
func Open(path string) (*Storage, error) {
|
|
|
|
|
s := Storage{Token: make(map[string]*shrt), Url: make(map[string]string)}
|
|
|
|
|
s := Storage{Token: make(map[string]*Shrt), Url: make(map[string]string)}
|
|
|
|
|
// Open db ore create if not exist!
|
|
|
|
|
if db, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644); err == nil {
|
|
|
|
|
json.Unmarshal(StreamToByte(db), &s)
|
|
|
|
@ -42,12 +42,12 @@ func Open(path string) (*Storage, error){
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Storage) Short(URL string, value *string) (string, error) {
|
|
|
|
|
func (s *Storage) Short(URL string, value *string) error {
|
|
|
|
|
// Check if it is a valide Url found on:
|
|
|
|
|
// http://stackoverflow.com/questions/31480710/validate-url-with-standard-package-in-go
|
|
|
|
|
_, err := url.ParseRequestURI(URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", ErrNoUrl
|
|
|
|
|
return ErrNoUrl
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a sha256 Hash from the URL
|
|
|
|
@ -55,7 +55,8 @@ func (s *Storage) Short(URL string, value *string) (string, error) {
|
|
|
|
|
|
|
|
|
|
// Test if the URL alraedy exist and return the key
|
|
|
|
|
if val, ok := s.Url[hash]; ok {
|
|
|
|
|
return val, nil
|
|
|
|
|
value = &val
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
// Iterate to the length of hash to get the shortest output
|
|
|
|
|
for hashShortestLen := 1; hashShortestLen <= 32; hashShortestLen++ {
|
|
|
|
@ -63,18 +64,18 @@ func (s *Storage) Short(URL string, value *string) (string, error) {
|
|
|
|
|
if _, ok := s.Token[hash[:hashShortestLen]]; !ok {
|
|
|
|
|
token := hash[:hashShortestLen]
|
|
|
|
|
t := time.Now()
|
|
|
|
|
s.Token[token] = &shrt{
|
|
|
|
|
s.Token[token] = &Shrt{
|
|
|
|
|
URL: URL,
|
|
|
|
|
Datum: t.String(),
|
|
|
|
|
Date: t.String(),
|
|
|
|
|
}
|
|
|
|
|
s.Url[hash] = token
|
|
|
|
|
*value = s.Url[hash]
|
|
|
|
|
s.Save()
|
|
|
|
|
return token, nil
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "", ErrCreateToken
|
|
|
|
|
return ErrCreateToken
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Storage) Add(URL string, value *string) (string, error) {
|
|
|
|
@ -98,9 +99,9 @@ func (s *Storage) Add(URL string, value *string) (string, error) {
|
|
|
|
|
if _, ok := s.Token[hash[:hashShortestLen]]; !ok {
|
|
|
|
|
token := hash[:hashShortestLen]
|
|
|
|
|
t := time.Now()
|
|
|
|
|
s.Token[token] = &shrt{
|
|
|
|
|
s.Token[token] = &Shrt{
|
|
|
|
|
URL: URL,
|
|
|
|
|
Datum: t.String(),
|
|
|
|
|
Date: t.String(),
|
|
|
|
|
}
|
|
|
|
|
s.Url[hash] = token
|
|
|
|
|
*value = s.Url[hash]
|
|
|
|
@ -120,15 +121,24 @@ func (s *Storage) Remove(URL string) error {
|
|
|
|
|
func (s *Storage) Get(token string) (string, bool) {
|
|
|
|
|
if shrt, ok := s.Token[token]; ok {
|
|
|
|
|
fmt.Printf("Url mit dem Token %s gefunden: %s \n", token, shrt.URL)
|
|
|
|
|
s.Token[token].Count += 1
|
|
|
|
|
s.Token[token].Clicks += 1
|
|
|
|
|
s.Save()
|
|
|
|
|
return shrt.URL, true
|
|
|
|
|
}
|
|
|
|
|
return "", false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get returns the URL for the given token
|
|
|
|
|
func (s *Storage) GetAPI(token string, value *Shrt) bool {
|
|
|
|
|
if shrt, ok := s.Token[token]; ok {
|
|
|
|
|
*value = *shrt
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get all entries
|
|
|
|
|
func (s *Storage) All() (string) {
|
|
|
|
|
func (s *Storage) All() string {
|
|
|
|
|
b, err := json.Marshal(&s)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ""
|
|
|
|
|