Add ability to display feed
This commit is contained in:
		@@ -102,16 +102,28 @@ func (db *DB) ChangePassword(id int64, oldPass, newPass string) error {
 | 
				
			|||||||
		return fmt.Errorf("error creating password hash: %v", err)
 | 
							return fmt.Errorf("error creating password hash: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	updateQuery := `
 | 
						query := `
 | 
				
			||||||
    UPDATE users SET
 | 
					    UPDATE users SET
 | 
				
			||||||
        password = ?
 | 
					        password = ?
 | 
				
			||||||
    WHERE
 | 
					    WHERE
 | 
				
			||||||
        id = ?
 | 
					        id = ?
 | 
				
			||||||
    `
 | 
					    `
 | 
				
			||||||
	_, err = db.Exec(updateQuery, string(newHashedPass), id)
 | 
						_, err = db.Exec(query, string(newHashedPass), id)
 | 
				
			||||||
	if 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)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						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 (
 | 
					import (
 | 
				
			||||||
	"encoding/gob"
 | 
						"encoding/gob"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"time"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/gorilla/feeds"
 | 
						"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) {
 | 
					func OpenFeed(filename string) (*Feed, error) {
 | 
				
			||||||
	file, err := os.Open(filename)
 | 
						file, err := os.Open(filename)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
@@ -56,20 +39,18 @@ func OpenFeed(filename string) (*Feed, error) {
 | 
				
			|||||||
	return feed, nil
 | 
						return feed, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func AddToFeed(feed *Feed, title, desc, content string) error {
 | 
					func (feed *Feed) Save(filename string) error {
 | 
				
			||||||
	item := feeds.Item{
 | 
						file, err := os.Create(filename)
 | 
				
			||||||
		Title:       title,
 | 
					 | 
				
			||||||
		Created:     time.Now(),
 | 
					 | 
				
			||||||
		Description: desc,
 | 
					 | 
				
			||||||
		Content:     content,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	feed.Add(&item)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	rss, err := feed.ToRss()
 | 
					 | 
				
			||||||
	if err != nil {
 | 
						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
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -5,12 +5,13 @@ import (
 | 
				
			|||||||
	"html/template"
 | 
						"html/template"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/gorilla/feeds"
 | 
				
			||||||
	"streifling.com/jason/cpolis/cmd/data"
 | 
						"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) {
 | 
						return func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
		user := r.PostFormValue("username")
 | 
							user := r.PostFormValue("username")
 | 
				
			||||||
		pass := r.PostFormValue("password")
 | 
							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) {
 | 
						return func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
		title := r.PostFormValue("editor-title")
 | 
							title := r.PostFormValue("editor-title")
 | 
				
			||||||
		desc := r.PostFormValue("editor-desc")
 | 
							desc := r.PostFormValue("editor-desc")
 | 
				
			||||||
@@ -45,13 +46,25 @@ func HandleFinishedEdit(f *feed.Feed) http.HandlerFunc {
 | 
				
			|||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		feed.AddToFeed(f, title, desc, content)
 | 
							feed.Add(&feeds.Item{
 | 
				
			||||||
		feed.SaveFeed(f, "tmp/rss.gob")
 | 
								Title:       title,
 | 
				
			||||||
		// template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "html-result", rssItem)
 | 
								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) {
 | 
						return func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
		var writer, editor, admin bool
 | 
							var writer, editor, admin bool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										30
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								main.go
									
									
									
									
									
								
							@@ -1,13 +1,13 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"html/template"
 | 
						"html/template"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"streifling.com/jason/cpolis/cmd/data"
 | 
						"streifling.com/jason/cpolis/cmd/data"
 | 
				
			||||||
	"streifling.com/jason/cpolis/cmd/feed"
 | 
					 | 
				
			||||||
	"streifling.com/jason/cpolis/cmd/ui"
 | 
						"streifling.com/jason/cpolis/cmd/ui"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -25,23 +25,35 @@ func main() {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	defer db.Close()
 | 
						defer db.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	rss, err := feed.OpenFeed("tmp/rss.gob")
 | 
						feed, err := data.OpenFeed("tmp/rss.gob")
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Println(err)
 | 
							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",
 | 
								"https://distrikt-ni-st.de",
 | 
				
			||||||
			"Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität")
 | 
								"Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	mux := http.NewServeMux()
 | 
						mux := http.NewServeMux()
 | 
				
			||||||
	mux.Handle("/web/static/", http.StripPrefix("/web/static/", http.FileServer(http.Dir("web/static/"))))
 | 
						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))
 | 
						numRows, err := db.CountEntries()
 | 
				
			||||||
	mux.HandleFunc("POST /finished-edit/", ui.HandleFinishedEdit(rss))
 | 
						if err != nil {
 | 
				
			||||||
	mux.HandleFunc("POST /login/", ui.HandleLogin(db))
 | 
							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))
 | 
						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}}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user