A bit of cleaning up
This commit is contained in:
parent
b74036343f
commit
dd50c4f385
@ -37,7 +37,7 @@ type TagList struct {
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
func minArticleList() *ArticleList {
|
||||
func initArticleList() *ArticleList {
|
||||
return &ArticleList{
|
||||
addCh: make(chan *Article),
|
||||
delCh: make(chan uuid.UUID),
|
||||
@ -46,7 +46,7 @@ func minArticleList() *ArticleList {
|
||||
}
|
||||
}
|
||||
|
||||
func minTagList() *TagList {
|
||||
func initTagList() *TagList {
|
||||
return &TagList{
|
||||
addCh: make(chan string),
|
||||
getCh: make(chan []string),
|
||||
@ -89,7 +89,7 @@ func (tl *TagList) start() {
|
||||
}
|
||||
|
||||
func NewArticleList() *ArticleList {
|
||||
list := minArticleList()
|
||||
list := initArticleList()
|
||||
list.articles = []*Article{}
|
||||
|
||||
list.wg.Add(1)
|
||||
@ -106,6 +106,7 @@ func (al *ArticleList) Add(a *Article) {
|
||||
func (al *ArticleList) Release(uuid uuid.UUID) (*Article, bool) {
|
||||
al.delCh <- uuid
|
||||
article := <-al.retCh
|
||||
|
||||
if article == nil {
|
||||
return nil, false
|
||||
}
|
||||
@ -123,10 +124,8 @@ func (al *ArticleList) Save(filename string) error {
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
encoder := gob.NewEncoder(file)
|
||||
articles := al.Get()
|
||||
err = encoder.Encode(articles)
|
||||
if err != nil {
|
||||
if err = gob.NewEncoder(file).Encode(articles); err != nil {
|
||||
return fmt.Errorf("error ecoding key: %v", err)
|
||||
}
|
||||
|
||||
@ -139,10 +138,8 @@ func LoadArticleList(filename string) (*ArticleList, error) {
|
||||
return nil, fmt.Errorf("error opening key file: %v", err)
|
||||
}
|
||||
|
||||
decoder := gob.NewDecoder(file)
|
||||
articleList := NewArticleList()
|
||||
err = decoder.Decode(&articleList.articles)
|
||||
if err != nil {
|
||||
if err = gob.NewDecoder(file).Decode(&articleList.articles); err != nil {
|
||||
return nil, fmt.Errorf("error decoding key: %v", err)
|
||||
}
|
||||
|
||||
@ -150,7 +147,7 @@ func LoadArticleList(filename string) (*ArticleList, error) {
|
||||
}
|
||||
|
||||
func NewTagList() *TagList {
|
||||
list := minTagList()
|
||||
list := initTagList()
|
||||
list.tags = []string{}
|
||||
|
||||
list.wg.Add(1)
|
||||
@ -175,10 +172,8 @@ func (tl *TagList) Save(filename string) error {
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
encoder := gob.NewEncoder(file)
|
||||
tags := tl.Get()
|
||||
err = encoder.Encode(tags)
|
||||
if err != nil {
|
||||
if err = gob.NewEncoder(file).Encode(tags); err != nil {
|
||||
return fmt.Errorf("error ecoding key: %v", err)
|
||||
}
|
||||
|
||||
@ -190,11 +185,10 @@ func LoadTagList(filename string) (*TagList, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error opening key file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
decoder := gob.NewDecoder(file)
|
||||
tagList := NewTagList()
|
||||
err = decoder.Decode(&tagList.tags)
|
||||
if err != nil {
|
||||
if err = gob.NewDecoder(file).Decode(&tagList.tags); err != nil {
|
||||
return nil, fmt.Errorf("error decoding key: %v", err)
|
||||
}
|
||||
|
||||
|
@ -45,8 +45,7 @@ func (db *DB) AddUser(user *User, pass string) error {
|
||||
(username, password, first_name, last_name, role)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
`
|
||||
_, err = db.Exec(query, user.UserName, string(hashedPass), user.FirstName, user.LastName, user.Role)
|
||||
if err != nil {
|
||||
if _, err = db.Exec(query, user.UserName, string(hashedPass), user.FirstName, user.LastName, user.Role); err != nil {
|
||||
return fmt.Errorf("error inserting user into DB: %v", err)
|
||||
}
|
||||
|
||||
@ -104,8 +103,7 @@ func (db *DB) ChangePassword(id int64, oldPass, newPass string) error {
|
||||
SET password = ?
|
||||
WHERE id = ?
|
||||
`
|
||||
_, err = db.Exec(query, string(newHashedPass), id)
|
||||
if err != nil {
|
||||
if _, err = db.Exec(query, string(newHashedPass), id); err != nil {
|
||||
return fmt.Errorf("error updating password in DB: %v", err)
|
||||
}
|
||||
|
||||
@ -134,8 +132,7 @@ func (db *DB) GetUser(id int64) (*User, error) {
|
||||
`
|
||||
|
||||
row := db.QueryRow(query, id)
|
||||
if err := row.Scan(&user.ID, &user.UserName, &user.FirstName,
|
||||
&user.LastName, &user.Role); err != nil {
|
||||
if err := row.Scan(&user.ID, &user.UserName, &user.FirstName, &user.LastName, &user.Role); err != nil {
|
||||
return nil, fmt.Errorf("error reading user information: %v", err)
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,7 @@ type Feed struct {
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
func minFeed() *Feed {
|
||||
func initFeed() *Feed {
|
||||
return &Feed{
|
||||
addCh: make(chan *feeds.Item),
|
||||
setCh: make(chan feeds.Feed),
|
||||
@ -38,7 +38,7 @@ func (f *Feed) start() {
|
||||
}
|
||||
|
||||
func NewFeed(title, link, desc string) *Feed {
|
||||
feed := minFeed()
|
||||
feed := initFeed()
|
||||
feed.feed = feeds.Feed{
|
||||
Title: title,
|
||||
Link: &feeds.Link{Href: link},
|
||||
@ -67,20 +67,17 @@ func OpenFeed(filename string) (*Feed, error) {
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
feed := minFeed()
|
||||
feed := initFeed()
|
||||
feed.wg.Add(1)
|
||||
go feed.start()
|
||||
feed.wg.Wait()
|
||||
|
||||
decoder := gob.NewDecoder(file)
|
||||
tmpFeed := new(feeds.Feed)
|
||||
err = decoder.Decode(tmpFeed)
|
||||
if err != nil {
|
||||
if err = gob.NewDecoder(file).Decode(tmpFeed); err != nil {
|
||||
return nil, fmt.Errorf("error decoding file %v: %v", filename, err)
|
||||
}
|
||||
|
||||
feed.Set(*tmpFeed)
|
||||
|
||||
return feed, nil
|
||||
}
|
||||
|
||||
@ -91,10 +88,8 @@ func (f *Feed) Save(filename string) error {
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
encoder := gob.NewEncoder(file)
|
||||
feed := f.Get()
|
||||
err = encoder.Encode(feed)
|
||||
if err != nil {
|
||||
if err = gob.NewEncoder(file).Encode(feed); err != nil {
|
||||
return fmt.Errorf("error encoding file %v: %v", filename, err)
|
||||
}
|
||||
|
||||
|
@ -17,8 +17,7 @@ type CookieStore struct {
|
||||
func NewKey() ([]byte, error) {
|
||||
key := make([]byte, 32)
|
||||
|
||||
_, err := io.ReadFull(rand.Reader, key)
|
||||
if err != nil {
|
||||
if _, err := io.ReadFull(rand.Reader, key); err != nil {
|
||||
return nil, fmt.Errorf("error generating key: %v", err)
|
||||
}
|
||||
|
||||
@ -33,9 +32,7 @@ func SaveKey(key []byte, filename string) error {
|
||||
defer file.Close()
|
||||
file.Chmod(0600)
|
||||
|
||||
encoder := gob.NewEncoder(file)
|
||||
err = encoder.Encode(key)
|
||||
if err != nil {
|
||||
if err = gob.NewEncoder(file).Encode(key); err != nil {
|
||||
return fmt.Errorf("error ecoding key: %v", err)
|
||||
}
|
||||
|
||||
@ -49,9 +46,7 @@ func LoadKey(filename string) ([]byte, error) {
|
||||
}
|
||||
|
||||
key := make([]byte, 32)
|
||||
decoder := gob.NewDecoder(file)
|
||||
err = decoder.Decode(&key)
|
||||
if err != nil {
|
||||
if err = gob.NewDecoder(file).Decode(&key); err != nil {
|
||||
return nil, fmt.Errorf("error decoding key: %v", err)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user