|
|
@ -9,17 +9,18 @@ import (
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"io"
|
|
|
|
"io"
|
|
|
|
"bytes"
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Storage struct {
|
|
|
|
type Storage struct {
|
|
|
|
Token map[string]*shrt
|
|
|
|
Token map[string]*shrt `json:"token"`
|
|
|
|
Url map[string]string
|
|
|
|
Url map[string]string `json:"url"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type shrt struct {
|
|
|
|
type shrt struct {
|
|
|
|
URL string
|
|
|
|
URL string `json:"url"`
|
|
|
|
Datum string
|
|
|
|
Datum string `json:"datum"`
|
|
|
|
Count int64
|
|
|
|
Count int64 `json:"count"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
var (
|
|
|
@ -28,10 +29,11 @@ var (
|
|
|
|
ErrNoUrl = errors.New("String is no Url!")
|
|
|
|
ErrNoUrl = errors.New("String is no Url!")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func Open() (*Storage, error){
|
|
|
|
// 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!
|
|
|
|
// Open db ore create if not exist!
|
|
|
|
if db, err := os.OpenFile("./test.db", os.O_RDWR|os.O_CREATE, 0644); err == nil {
|
|
|
|
if db, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644); err == nil {
|
|
|
|
json.Unmarshal(StreamToByte(db), &s)
|
|
|
|
json.Unmarshal(StreamToByte(db), &s)
|
|
|
|
db.Close()
|
|
|
|
db.Close()
|
|
|
|
return &s, nil
|
|
|
|
return &s, nil
|
|
|
@ -40,7 +42,7 @@ func Open() (*Storage, error){
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Storage) Add(URL string, value interface{}) (string, error) {
|
|
|
|
func (s *Storage) Short(URL string, value *string) (string, error) {
|
|
|
|
// Check if it is a valide Url found on:
|
|
|
|
// Check if it is a valide Url found on:
|
|
|
|
// http://stackoverflow.com/questions/31480710/validate-url-with-standard-package-in-go
|
|
|
|
// http://stackoverflow.com/questions/31480710/validate-url-with-standard-package-in-go
|
|
|
|
_, err := url.ParseRequestURI(URL)
|
|
|
|
_, err := url.ParseRequestURI(URL)
|
|
|
@ -60,9 +62,48 @@ func (s *Storage) Add(URL string, value interface{}) (string, error) {
|
|
|
|
// Test if the Token not exist and return the new generated token
|
|
|
|
// Test if the Token not exist and return the new generated token
|
|
|
|
if _, ok := s.Token[hash[:hashShortestLen]]; !ok {
|
|
|
|
if _, ok := s.Token[hash[:hashShortestLen]]; !ok {
|
|
|
|
token := hash[:hashShortestLen]
|
|
|
|
token := hash[:hashShortestLen]
|
|
|
|
s.Token[token] = &shrt{URL: URL}
|
|
|
|
t := time.Now()
|
|
|
|
|
|
|
|
s.Token[token] = &shrt{
|
|
|
|
|
|
|
|
URL: URL,
|
|
|
|
|
|
|
|
Datum: t.String(),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Url[hash] = token
|
|
|
|
|
|
|
|
*value = s.Url[hash]
|
|
|
|
|
|
|
|
s.Save()
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return "", ErrCreateToken
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Storage) Add(URL string, value *string) (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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create a sha256 Hash from the URL
|
|
|
|
|
|
|
|
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(URL)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Test if the URL alraedy exist and return the key
|
|
|
|
|
|
|
|
if val, ok := s.Url[hash]; ok {
|
|
|
|
|
|
|
|
return val, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate to the length of hash to get the shortest output
|
|
|
|
|
|
|
|
for hashShortestLen := 1; hashShortestLen <= 32; hashShortestLen++ {
|
|
|
|
|
|
|
|
// Test if the Token not exist and return the new generated token
|
|
|
|
|
|
|
|
if _, ok := s.Token[hash[:hashShortestLen]]; !ok {
|
|
|
|
|
|
|
|
token := hash[:hashShortestLen]
|
|
|
|
|
|
|
|
t := time.Now()
|
|
|
|
|
|
|
|
s.Token[token] = &shrt{
|
|
|
|
|
|
|
|
URL: URL,
|
|
|
|
|
|
|
|
Datum: t.String(),
|
|
|
|
|
|
|
|
}
|
|
|
|
s.Url[hash] = token
|
|
|
|
s.Url[hash] = token
|
|
|
|
value = s.Url[hash]
|
|
|
|
*value = s.Url[hash]
|
|
|
|
s.Save()
|
|
|
|
s.Save()
|
|
|
|
return token, nil
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -86,6 +127,7 @@ func (s *Storage) Get(token string) (string, bool) {
|
|
|
|
return "", false
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get all entries
|
|
|
|
func (s *Storage) All() (string) {
|
|
|
|
func (s *Storage) All() (string) {
|
|
|
|
b, err := json.Marshal(&s)
|
|
|
|
b, err := json.Marshal(&s)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|