Browse Source

storage path integration

master
kreativmonkey 5 years ago
parent
commit
517d6be4c7
  1. 2
      main.go
  2. 26
      shrty/short.go
  3. 8
      shrty/short_test.go
  4. 26
      shrty/shrty.go
  5. 4
      shrty/shrty_test.go

2
main.go

@ -12,7 +12,7 @@ import (
const version = "0.07"
var (
short *shrty.Store
short *shrty.DB
// Global configuration Variable
config = ReadConfig()

26
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 ""
}

8
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,
}

26
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

4
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)

Loading…
Cancel
Save