-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: simplify store implementation (resolve #5)
fix: make store thread-safe (resolve #1)
- Loading branch information
Showing
13 changed files
with
184 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/muety/webhook2telegram/store" | ||
) | ||
|
||
var storeInstance store.Store | ||
|
||
func GetStore() store.Store { | ||
if storeInstance == nil { | ||
storeInstance = store.NewGobStore(Get().GetStorePath()) | ||
} | ||
return storeInstance | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package services | ||
|
||
import ( | ||
"github.com/muety/webhook2telegram/model" | ||
"github.com/muety/webhook2telegram/store" | ||
"strconv" | ||
) | ||
|
||
type UserService struct { | ||
store store.Store | ||
} | ||
|
||
func NewUserService(store store.Store) *UserService { | ||
return &UserService{store: store} | ||
} | ||
|
||
func (s *UserService) InvalidateToken(userChatId int) { | ||
for k, v := range s.store.GetItems() { | ||
entry, ok := v.(model.StoreObject) | ||
if ok && entry.ChatId == userChatId { | ||
s.store.Delete(k) | ||
} | ||
} | ||
} | ||
|
||
func (s *UserService) ResolveToken(token string) string { | ||
value := s.store.Get(token) | ||
if value != nil { | ||
return strconv.Itoa((value.(model.StoreObject)).ChatId) | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package store | ||
|
||
import ( | ||
"encoding/gob" | ||
"github.com/muety/webhook2telegram/model" | ||
"github.com/orcaman/concurrent-map" | ||
"log" | ||
"os" | ||
) | ||
|
||
type GobStore struct { | ||
data cmap.ConcurrentMap | ||
filePath string | ||
} | ||
|
||
func NewGobStore(filePath string) *GobStore { | ||
//gob.Register(model.StoreObject{}) | ||
//gob.Register(model.StoreMessageObject{}) | ||
|
||
// Backwards compatibility | ||
gob.RegisterName("main.StoreObject", model.StoreObject{}) | ||
gob.RegisterName("main.StoreMessageObject", model.StoreMessageObject{}) | ||
|
||
store := &GobStore{ | ||
data: cmap.New(), | ||
filePath: filePath, | ||
} | ||
|
||
if err := store.load(); err == nil { | ||
log.Println("read existing gob store from file") | ||
} | ||
|
||
return store | ||
} | ||
|
||
func (s *GobStore) load() error { | ||
file, err := os.Open(s.filePath) | ||
defer file.Close() | ||
if err != nil { | ||
log.Printf("error: failed to read store from %s\n", s.filePath) | ||
return nil | ||
} | ||
|
||
var rawData map[string]interface{} | ||
if err := gob.NewDecoder(file).Decode(&rawData); err != nil { | ||
log.Printf("error: failed to decode store data from %s (%v)\n", s.filePath, err) | ||
return nil | ||
} | ||
|
||
s.data = cmap.New() | ||
for k, v := range rawData { | ||
s.data.Set(k, v) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *GobStore) dump() error { | ||
file, err := os.Create(s.filePath) | ||
defer file.Close() | ||
if err != nil { | ||
log.Printf("error: failed to dump store to %s (%v)", s.filePath, err) | ||
return err | ||
} | ||
|
||
return gob.NewEncoder(file).Encode(s.data.Items()) | ||
} | ||
|
||
func (s *GobStore) Get(key string) interface{} { | ||
if v, ok := s.data.Get(key); ok { | ||
return v | ||
} | ||
return nil | ||
} | ||
|
||
func (s *GobStore) Put(key string, value interface{}) { | ||
s.data.Set(key, value) | ||
go s.dump() | ||
} | ||
|
||
func (s *GobStore) Delete(key string) { | ||
s.data.Remove(key) | ||
go s.dump() | ||
} | ||
|
||
func (s *GobStore) GetItems() map[string]interface{} { | ||
return s.data.Items() | ||
} | ||
|
||
func (s *GobStore) Flush() error { | ||
return s.dump() | ||
} |
Oops, something went wrong.