add oauth flow routes

This commit is contained in:
Jay
2019-12-28 14:38:38 +00:00
parent 369e8d3f6e
commit 6ff437be99
10 changed files with 367 additions and 13 deletions
+24 -9
View File
@@ -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()
}