Add ability to display feed
This commit is contained in:
parent
36f7a92a06
commit
49988edd82
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
@ -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
|
||||
|
||||
|
24
main.go
24
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/"))))
|
||||
|
||||
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.HandleAddUser(db))
|
||||
mux.HandleFunc("POST /finished-edit/", ui.HandleFinishedEdit(rss))
|
||||
mux.HandleFunc("POST /login/", ui.HandleLogin(db))
|
||||
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))
|
||||
}
|
||||
|
3
web/templates/feed.rss
Normal file
3
web/templates/feed.rss
Normal file
@ -0,0 +1,3 @@
|
||||
{{define "page-content"}}
|
||||
{{.}}
|
||||
{{end}}
|
Loading…
x
Reference in New Issue
Block a user