A bit of cleaning up

This commit is contained in:
Jason Streifling 2024-03-05 16:38:18 +01:00
parent b74036343f
commit dd50c4f385
4 changed files with 21 additions and 40 deletions

View File

@ -37,7 +37,7 @@ type TagList struct {
wg sync.WaitGroup wg sync.WaitGroup
} }
func minArticleList() *ArticleList { func initArticleList() *ArticleList {
return &ArticleList{ return &ArticleList{
addCh: make(chan *Article), addCh: make(chan *Article),
delCh: make(chan uuid.UUID), delCh: make(chan uuid.UUID),
@ -46,7 +46,7 @@ func minArticleList() *ArticleList {
} }
} }
func minTagList() *TagList { func initTagList() *TagList {
return &TagList{ return &TagList{
addCh: make(chan string), addCh: make(chan string),
getCh: make(chan []string), getCh: make(chan []string),
@ -89,7 +89,7 @@ func (tl *TagList) start() {
} }
func NewArticleList() *ArticleList { func NewArticleList() *ArticleList {
list := minArticleList() list := initArticleList()
list.articles = []*Article{} list.articles = []*Article{}
list.wg.Add(1) list.wg.Add(1)
@ -106,6 +106,7 @@ func (al *ArticleList) Add(a *Article) {
func (al *ArticleList) Release(uuid uuid.UUID) (*Article, bool) { func (al *ArticleList) Release(uuid uuid.UUID) (*Article, bool) {
al.delCh <- uuid al.delCh <- uuid
article := <-al.retCh article := <-al.retCh
if article == nil { if article == nil {
return nil, false return nil, false
} }
@ -123,10 +124,8 @@ func (al *ArticleList) Save(filename string) error {
} }
defer file.Close() defer file.Close()
encoder := gob.NewEncoder(file)
articles := al.Get() articles := al.Get()
err = encoder.Encode(articles) if err = gob.NewEncoder(file).Encode(articles); err != nil {
if err != nil {
return fmt.Errorf("error ecoding key: %v", err) 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) return nil, fmt.Errorf("error opening key file: %v", err)
} }
decoder := gob.NewDecoder(file)
articleList := NewArticleList() articleList := NewArticleList()
err = decoder.Decode(&articleList.articles) if err = gob.NewDecoder(file).Decode(&articleList.articles); err != nil {
if err != nil {
return nil, fmt.Errorf("error decoding key: %v", err) return nil, fmt.Errorf("error decoding key: %v", err)
} }
@ -150,7 +147,7 @@ func LoadArticleList(filename string) (*ArticleList, error) {
} }
func NewTagList() *TagList { func NewTagList() *TagList {
list := minTagList() list := initTagList()
list.tags = []string{} list.tags = []string{}
list.wg.Add(1) list.wg.Add(1)
@ -175,10 +172,8 @@ func (tl *TagList) Save(filename string) error {
} }
defer file.Close() defer file.Close()
encoder := gob.NewEncoder(file)
tags := tl.Get() tags := tl.Get()
err = encoder.Encode(tags) if err = gob.NewEncoder(file).Encode(tags); err != nil {
if err != nil {
return fmt.Errorf("error ecoding key: %v", err) return fmt.Errorf("error ecoding key: %v", err)
} }
@ -190,11 +185,10 @@ func LoadTagList(filename string) (*TagList, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("error opening key file: %v", err) return nil, fmt.Errorf("error opening key file: %v", err)
} }
defer file.Close()
decoder := gob.NewDecoder(file)
tagList := NewTagList() tagList := NewTagList()
err = decoder.Decode(&tagList.tags) if err = gob.NewDecoder(file).Decode(&tagList.tags); err != nil {
if err != nil {
return nil, fmt.Errorf("error decoding key: %v", err) return nil, fmt.Errorf("error decoding key: %v", err)
} }

View File

@ -45,8 +45,7 @@ func (db *DB) AddUser(user *User, pass string) error {
(username, password, first_name, last_name, role) (username, password, first_name, last_name, role)
VALUES (?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?)
` `
_, err = db.Exec(query, user.UserName, string(hashedPass), user.FirstName, user.LastName, user.Role) if _, err = db.Exec(query, user.UserName, string(hashedPass), user.FirstName, user.LastName, user.Role); err != nil {
if err != nil {
return fmt.Errorf("error inserting user into DB: %v", err) 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 = ? SET password = ?
WHERE id = ? WHERE id = ?
` `
_, err = db.Exec(query, string(newHashedPass), id) if _, err = db.Exec(query, string(newHashedPass), id); err != nil {
if err != nil {
return fmt.Errorf("error updating password in DB: %v", err) 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) row := db.QueryRow(query, id)
if err := row.Scan(&user.ID, &user.UserName, &user.FirstName, if err := row.Scan(&user.ID, &user.UserName, &user.FirstName, &user.LastName, &user.Role); err != nil {
&user.LastName, &user.Role); err != nil {
return nil, fmt.Errorf("error reading user information: %v", err) return nil, fmt.Errorf("error reading user information: %v", err)
} }

View File

@ -17,7 +17,7 @@ type Feed struct {
wg sync.WaitGroup wg sync.WaitGroup
} }
func minFeed() *Feed { func initFeed() *Feed {
return &Feed{ return &Feed{
addCh: make(chan *feeds.Item), addCh: make(chan *feeds.Item),
setCh: make(chan feeds.Feed), setCh: make(chan feeds.Feed),
@ -38,7 +38,7 @@ func (f *Feed) start() {
} }
func NewFeed(title, link, desc string) *Feed { func NewFeed(title, link, desc string) *Feed {
feed := minFeed() feed := initFeed()
feed.feed = feeds.Feed{ feed.feed = feeds.Feed{
Title: title, Title: title,
Link: &feeds.Link{Href: link}, Link: &feeds.Link{Href: link},
@ -67,20 +67,17 @@ func OpenFeed(filename string) (*Feed, error) {
} }
defer file.Close() defer file.Close()
feed := minFeed() feed := initFeed()
feed.wg.Add(1) feed.wg.Add(1)
go feed.start() go feed.start()
feed.wg.Wait() feed.wg.Wait()
decoder := gob.NewDecoder(file)
tmpFeed := new(feeds.Feed) tmpFeed := new(feeds.Feed)
err = decoder.Decode(tmpFeed) if err = gob.NewDecoder(file).Decode(tmpFeed); err != nil {
if err != nil {
return nil, fmt.Errorf("error decoding file %v: %v", filename, err) return nil, fmt.Errorf("error decoding file %v: %v", filename, err)
} }
feed.Set(*tmpFeed) feed.Set(*tmpFeed)
return feed, nil return feed, nil
} }
@ -91,10 +88,8 @@ func (f *Feed) Save(filename string) error {
} }
defer file.Close() defer file.Close()
encoder := gob.NewEncoder(file)
feed := f.Get() feed := f.Get()
err = encoder.Encode(feed) if err = gob.NewEncoder(file).Encode(feed); err != nil {
if err != nil {
return fmt.Errorf("error encoding file %v: %v", filename, err) return fmt.Errorf("error encoding file %v: %v", filename, err)
} }

View File

@ -17,8 +17,7 @@ type CookieStore struct {
func NewKey() ([]byte, error) { func NewKey() ([]byte, error) {
key := make([]byte, 32) key := make([]byte, 32)
_, err := io.ReadFull(rand.Reader, key) if _, err := io.ReadFull(rand.Reader, key); err != nil {
if err != nil {
return nil, fmt.Errorf("error generating key: %v", err) return nil, fmt.Errorf("error generating key: %v", err)
} }
@ -33,9 +32,7 @@ func SaveKey(key []byte, filename string) error {
defer file.Close() defer file.Close()
file.Chmod(0600) file.Chmod(0600)
encoder := gob.NewEncoder(file) if err = gob.NewEncoder(file).Encode(key); err != nil {
err = encoder.Encode(key)
if err != nil {
return fmt.Errorf("error ecoding key: %v", err) return fmt.Errorf("error ecoding key: %v", err)
} }
@ -49,9 +46,7 @@ func LoadKey(filename string) ([]byte, error) {
} }
key := make([]byte, 32) key := make([]byte, 32)
decoder := gob.NewDecoder(file) if err = gob.NewDecoder(file).Decode(&key); err != nil {
err = decoder.Decode(&key)
if err != nil {
return nil, fmt.Errorf("error decoding key: %v", err) return nil, fmt.Errorf("error decoding key: %v", err)
} }