add oauth flow routes
This commit is contained in:
+24
-9
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"sync"
|
||||
|
||||
"git.trj.tw/golang/utils"
|
||||
)
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
type Storage struct {
|
||||
m map[string]interface{}
|
||||
filePath string
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
var s *Storage
|
||||
@@ -30,24 +32,31 @@ func New() *Storage {
|
||||
func Get() *Storage { return s }
|
||||
|
||||
// Load storage data from file
|
||||
func (p *Storage) Load(filePath string) (err error) {
|
||||
func (p *Storage) Load(filePath string, ignoreNotExist bool) (err error) {
|
||||
filePath = utils.ParsePath(filePath)
|
||||
if !utils.CheckExists(filePath, false) {
|
||||
exist := utils.CheckExists(filePath, false)
|
||||
if !exist && !ignoreNotExist {
|
||||
return errors.New("storage file not found")
|
||||
}
|
||||
p.filePath = filePath
|
||||
|
||||
b, err := ioutil.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if exist {
|
||||
b, err := ioutil.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(b, &p.m)
|
||||
if err := json.Unmarshal(b, &p.m); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (p *Storage) Write() (err error) {
|
||||
p.Lock()
|
||||
defer p.Unlock()
|
||||
b, err := json.Marshal(p.m)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -64,7 +73,13 @@ func (p *Storage) Get(key string) (data interface{}, ok bool) {
|
||||
}
|
||||
|
||||
// Set data by key
|
||||
func (p *Storage) Set(key string, value interface{}) { p.m[key] = value }
|
||||
func (p *Storage) Set(key string, value interface{}) {
|
||||
p.m[key] = value
|
||||
_ = p.Write()
|
||||
}
|
||||
|
||||
// Delete data by key
|
||||
func (p *Storage) Delete(key string) { delete(p.m, key) }
|
||||
func (p *Storage) Delete(key string) {
|
||||
delete(p.m, key)
|
||||
_ = p.Write()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user