From 49988edd82f634a6e5ce7a4f0269a723e63208d1 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sat, 24 Feb 2024 14:49:29 +0100 Subject: [PATCH] Add ability to display feed --- cmd/data/db.go | 16 +++++++++++++-- cmd/{feed => data}/rss.go | 41 +++++++++++---------------------------- cmd/ui/handlers.go | 27 +++++++++++++++++++------- main.go | 30 +++++++++++++++++++--------- web/templates/feed.rss | 3 +++ 5 files changed, 69 insertions(+), 48 deletions(-) rename cmd/{feed => data}/rss.go (70%) create mode 100644 web/templates/feed.rss diff --git a/cmd/data/db.go b/cmd/data/db.go index fca84c4..9c0fd54 100644 --- a/cmd/data/db.go +++ b/cmd/data/db.go @@ -102,16 +102,28 @@ func (db *DB) ChangePassword(id int64, oldPass, newPass string) error { return fmt.Errorf("error creating password hash: %v", err) } - updateQuery := ` + query := ` UPDATE users SET password = ? WHERE id = ? ` - _, err = db.Exec(updateQuery, string(newHashedPass), id) + _, err = db.Exec(query, string(newHashedPass), id) if err != nil { return fmt.Errorf("error updating password in DB: %v", err) } return nil } + +func (db *DB) CountEntries() (int64, error) { + var count int64 + + query := `SELECT COUNT(*) FROM users` + row := db.QueryRow(query) + if err := row.Scan(&count); err != nil { + return 0, fmt.Errorf("error counting rows in user DB: %v", err) + } + + return count, nil +} diff --git a/cmd/feed/rss.go b/cmd/data/rss.go similarity index 70% rename from cmd/feed/rss.go rename to cmd/data/rss.go index 3414472..52f58a7 100644 --- a/cmd/feed/rss.go +++ b/cmd/data/rss.go @@ -1,10 +1,9 @@ -package feed +package data import ( "encoding/gob" "fmt" "os" - "time" "github.com/gorilla/feeds" ) @@ -23,22 +22,6 @@ func NewFeed(title, link, desc string) *Feed { } } -func SaveFeed(feed *Feed, filename string) error { - file, err := os.Create(filename) - if err != nil { - return fmt.Errorf("error creating file %v: %v", filename, err) - } - defer file.Close() - - encoder := gob.NewEncoder(file) - err = encoder.Encode(feed) - if err != nil { - return fmt.Errorf("error encoding file %v: %v", filename, err) - } - - return nil -} - func OpenFeed(filename string) (*Feed, error) { file, err := os.Open(filename) if err != nil { @@ -56,20 +39,18 @@ func OpenFeed(filename string) (*Feed, error) { return feed, nil } -func AddToFeed(feed *Feed, title, desc, content string) error { - item := feeds.Item{ - Title: title, - Created: time.Now(), - Description: desc, - Content: content, - } - feed.Add(&item) - - rss, err := feed.ToRss() +func (feed *Feed) Save(filename string) error { + file, err := os.Create(filename) if err != nil { - return fmt.Errorf("error converting feed to RSS: %v", err) + return fmt.Errorf("error creating file %v: %v", filename, err) + } + defer file.Close() + + encoder := gob.NewEncoder(file) + err = encoder.Encode(feed) + if err != nil { + return fmt.Errorf("error encoding file %v: %v", filename, err) } - fmt.Println(rss) return nil } diff --git a/cmd/ui/handlers.go b/cmd/ui/handlers.go index db262e9..781f764 100644 --- a/cmd/ui/handlers.go +++ b/cmd/ui/handlers.go @@ -5,12 +5,13 @@ import ( "html/template" "log" "net/http" + "time" + "github.com/gorilla/feeds" "streifling.com/jason/cpolis/cmd/data" - "streifling.com/jason/cpolis/cmd/feed" ) -func HandleLogin(db *data.DB) http.HandlerFunc { +func Login(db *data.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { user := r.PostFormValue("username") pass := r.PostFormValue("password") @@ -32,7 +33,7 @@ func HandleLogin(db *data.DB) http.HandlerFunc { } } -func HandleFinishedEdit(f *feed.Feed) http.HandlerFunc { +func FinishEdit(feed *data.Feed) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { title := r.PostFormValue("editor-title") desc := r.PostFormValue("editor-desc") @@ -45,13 +46,25 @@ func HandleFinishedEdit(f *feed.Feed) http.HandlerFunc { return } - feed.AddToFeed(f, title, desc, content) - feed.SaveFeed(f, "tmp/rss.gob") - // template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "html-result", rssItem) + feed.Add(&feeds.Item{ + Title: title, + Created: time.Now(), + Description: desc, + Content: content, + }) + feed.Save("tmp/rss.gob") + + rss, err := feed.ToRss() + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + template.Must(template.ParseFiles("web/templates/feed.rss")).ExecuteTemplate(w, "page-content", rss) } } -func HandleAddUser(db *data.DB) http.HandlerFunc { +func AddUser(db *data.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var writer, editor, admin bool diff --git a/main.go b/main.go index b0bcfab..05b8ee3 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,13 @@ package main import ( + "fmt" "html/template" "log" "net/http" "os" "streifling.com/jason/cpolis/cmd/data" - "streifling.com/jason/cpolis/cmd/feed" "streifling.com/jason/cpolis/cmd/ui" ) @@ -25,23 +25,35 @@ func main() { } defer db.Close() - rss, err := feed.OpenFeed("tmp/rss.gob") + feed, err := data.OpenFeed("tmp/rss.gob") if err != nil { log.Println(err) - rss = feed.NewFeed("Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt", + feed = data.NewFeed("Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt", "https://distrikt-ni-st.de", "Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität") } mux := http.NewServeMux() mux.Handle("/web/static/", http.StripPrefix("/web/static/", http.FileServer(http.Dir("web/static/")))) - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - template.Must(template.ParseFiles("web/templates/index.html", "web/templates/login.html")).Execute(w, nil) - }) - mux.HandleFunc("POST /add-user/", ui.HandleAddUser(db)) - mux.HandleFunc("POST /finished-edit/", ui.HandleFinishedEdit(rss)) - mux.HandleFunc("POST /login/", ui.HandleLogin(db)) + numRows, err := db.CountEntries() + if err != nil { + log.Fatalln(err) + } + fmt.Println(numRows) + if numRows == 0 { + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + template.Must(template.ParseFiles("web/templates/index.html", "web/templates/add-user.html")).Execute(w, nil) + }) + } else { + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + template.Must(template.ParseFiles("web/templates/index.html", "web/templates/login.html")).Execute(w, nil) + }) + } + + mux.HandleFunc("POST /add-user/", ui.AddUser(db)) + mux.HandleFunc("POST /finished-edit/", ui.FinishEdit(feed)) + mux.HandleFunc("POST /login/", ui.Login(db)) log.Fatalln(http.ListenAndServe(":8080", mux)) } diff --git a/web/templates/feed.rss b/web/templates/feed.rss new file mode 100644 index 0000000..303b14c --- /dev/null +++ b/web/templates/feed.rss @@ -0,0 +1,3 @@ +{{define "page-content"}} +{{.}} +{{end}}