From 517d6be4c79447be05247887ef9d9316c3ee659a Mon Sep 17 00:00:00 2001 From: kreativmonkey Date: Sat, 15 Jul 2017 23:19:12 +0200 Subject: [PATCH] storage path integration --- main.go | 2 +- shrty/short.go | 26 +++++++++++++------------- shrty/short_test.go | 8 ++++---- shrty/shrty.go | 26 ++++++++++++++++++-------- shrty/shrty_test.go | 4 +++- 5 files changed, 39 insertions(+), 27 deletions(-) diff --git a/main.go b/main.go index 4b2e015..409dad0 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,7 @@ import ( const version = "0.07" var ( - short *shrty.Store + short *shrty.DB // Global configuration Variable config = ReadConfig() diff --git a/shrty/short.go b/shrty/short.go index de9df7b..81ac313 100644 --- a/shrty/short.go +++ b/shrty/short.go @@ -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 "" } diff --git a/shrty/short_test.go b/shrty/short_test.go index 4f9be3e..4b1cbfa 100644 --- a/shrty/short_test.go +++ b/shrty/short_test.go @@ -36,17 +36,17 @@ func TestStorage_Short(t *testing.T) { FavIconLink:"", HTTPStatusCode: 200, Category:"", - Created:s.Token[token].Created, + Created:s.Store.Token[token].Created, Clicks:0, } assert.NoError(t, err) - assert.Equal(t, expected, s.Token[token]) + assert.Equal(t, expected, s.Store.Token[token]) // If already exist the same Token will be return err = s.Short("http://bit.ly/2scBYES", &token) assert.NoError(t, err) - assert.Equal(t, expected, s.Token[token]) + assert.Equal(t, expected, s.Store.Token[token]) } // Whats wrong with this Test? @@ -83,7 +83,7 @@ func TestStorage_Get(t *testing.T) { FavIconLink:"", HTTPStatusCode: 200, Category:"", - Created:s.Token[token].Created, + Created:s.Store.Token[token].Created, Clicks:0, } diff --git a/shrty/shrty.go b/shrty/shrty.go index 6e0b9b1..6766602 100644 --- a/shrty/shrty.go +++ b/shrty/shrty.go @@ -8,6 +8,11 @@ import ( "os" ) +type DB struct { + Path string + Store *Store +} + type Store struct { Token map[string]*Data `json:"token"` // [token]*Data Url map[string]string `json:"url"` // [hash]token @@ -20,11 +25,16 @@ var ( ) // Open up -func Open(path string) (*Store, error) { - s := Store{Token: make(map[string]*Data), Url: make(map[string]string)} +func Open(path string) (*DB, error) { + s := DB{Path: path, + Store: &Store{ + Token: make(map[string]*Data), + 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) + if db, err := os.OpenFile(s.Path, os.O_RDWR|os.O_CREATE, 0644); err == nil { + json.Unmarshal(StreamToByte(db), &s.Store) db.Close() return &s, nil } else { @@ -32,13 +42,13 @@ func Open(path string) (*Store, error) { } } -func (s *Store) Remove(URL string) error { +func (s *DB) Remove(URL string) error { return nil } -func (s *Store) Save() error { - if db, err := os.OpenFile("./test.db", os.O_RDWR|os.O_CREATE, 0644); err == nil { - b, err := json.Marshal(&s) +func (s *DB) Save() error { + if db, err := os.OpenFile(s.Path, os.O_RDWR|os.O_CREATE, 0644); err == nil { + b, err := json.Marshal(s.Store) db.Write(b) db.Close() return err diff --git a/shrty/shrty_test.go b/shrty/shrty_test.go index 8bcf5b0..9ccb886 100644 --- a/shrty/shrty_test.go +++ b/shrty/shrty_test.go @@ -10,7 +10,9 @@ func TestOpen(t *testing.T) { s, err := Open(path) defer os.Remove(path) - expected := Store{Token: make(map[string]*Data), Url: make(map[string]string)} + expected := DB{Path: "short_test.db", + Store: Store{Token: make(map[string]*Data), Url: make(map[string]string)}, + } assert.NoError(t, err) assert.Equal(t, &expected, s)