|
|
|
@ -30,7 +30,7 @@ type Meta struct{
|
|
|
|
|
Type string `json:"type"meta:"og:type"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Store) Short(URL string, value *string) error {
|
|
|
|
|
func (s *DB) Short(URL string, value *string) error {
|
|
|
|
|
d := Data{
|
|
|
|
|
URL: URL,
|
|
|
|
|
}
|
|
|
|
@ -52,12 +52,12 @@ func (s *Store) Short(URL string, value *string) error {
|
|
|
|
|
// Iterate to the length of the 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[d.Hash[:hashShortestLen]]; !ok {
|
|
|
|
|
if _, ok := s.Store.Token[d.Hash[:hashShortestLen]]; !ok {
|
|
|
|
|
d.Token = d.Hash[:hashShortestLen]
|
|
|
|
|
d.Created = time.Now().String()
|
|
|
|
|
|
|
|
|
|
s.Token[d.Token] = &d
|
|
|
|
|
s.Url[d.Hash] = d.Token
|
|
|
|
|
s.Store.Token[d.Token] = &d
|
|
|
|
|
s.Store.Url[d.Hash] = d.Token
|
|
|
|
|
*value = d.Token
|
|
|
|
|
|
|
|
|
|
s.Save()
|
|
|
|
@ -69,17 +69,17 @@ func (s *Store) Short(URL string, value *string) error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// URL already Exist in the
|
|
|
|
|
func (s *Store) Exist(d *Data) bool {
|
|
|
|
|
if token, ok := s.Url[d.Hash]; ok {
|
|
|
|
|
*d = *s.Token[token]
|
|
|
|
|
func (s *DB) Exist(d *Data) bool {
|
|
|
|
|
if token, ok := s.Store.Url[d.Hash]; ok {
|
|
|
|
|
*d = *s.Store.Token[token]
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns the URL for the given token
|
|
|
|
|
func (s *Store) Redirect(token string) (string, bool) {
|
|
|
|
|
if data, ok := s.Token[token]; ok {
|
|
|
|
|
func (s *DB) Redirect(token string) (string, bool) {
|
|
|
|
|
if data, ok := s.Store.Token[token]; ok {
|
|
|
|
|
data.Clicks += 1
|
|
|
|
|
s.Save()
|
|
|
|
|
return data.URLFetched, true
|
|
|
|
@ -88,8 +88,8 @@ func (s *Store) Redirect(token string) (string, bool) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get Data for the given Token
|
|
|
|
|
func (s *Store) Get(token string, value *Data) bool {
|
|
|
|
|
if data, ok := s.Token[token]; ok {
|
|
|
|
|
func (s *DB) Get(token string, value *Data) bool {
|
|
|
|
|
if data, ok := s.Store.Token[token]; ok {
|
|
|
|
|
*value = *data
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
@ -97,8 +97,8 @@ func (s *Store) Get(token string, value *Data) bool {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get all entries
|
|
|
|
|
func (s *Store) GetAll() string {
|
|
|
|
|
b, err := json.Marshal(&s)
|
|
|
|
|
func (s *DB) GetAll() string {
|
|
|
|
|
b, err := json.Marshal(&s.Store)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|