diff --git a/cmd/data/markdown.go b/cmd/data/markdown.go index d083f24..40d432b 100644 --- a/cmd/data/markdown.go +++ b/cmd/data/markdown.go @@ -20,3 +20,16 @@ func ConvertToHTML(md string) (string, error) { return html, nil } + +func ConvertToPlain(md string) (string, error) { + var buf bytes.Buffer + + if err := goldmark.Convert([]byte(md), &buf); err != nil { + return "", fmt.Errorf("error converting markdown to html: %v", err) + } + + p := bluemonday.StrictPolicy() + plain := p.Sanitize(buf.String()) + + return plain, nil +} diff --git a/cmd/ui/handlers.go b/cmd/ui/admin.go similarity index 58% rename from cmd/ui/handlers.go rename to cmd/ui/admin.go index 2504c08..bb5bbb8 100644 --- a/cmd/ui/handlers.go +++ b/cmd/ui/admin.go @@ -5,56 +5,45 @@ import ( "html/template" "log" "net/http" - "time" - "github.com/gorilla/feeds" "streifling.com/jason/cpolis/cmd/data" ) -func Login(db *data.DB) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - user := r.PostFormValue("username") - pass := r.PostFormValue("password") +type AddUserData struct { + User string + First string + Last string + Role string + Msg string +} - id, err := db.GetID(user) - if err != nil { - log.Println(err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } +func inputsEmpty(user, pass, pass2, first, last, role string) bool { + return len(user) == 0 || + len(pass) == 0 || + len(pass2) == 0 || + len(first) == 0 || + len(last) == 0 || + len(role) == 0 +} - if err := db.CheckPassword(id, pass); err != nil { - log.Println(err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } +func checkUserStrings(user, first, last string) (string, int, bool) { + userLen := 15 + nameLen := 50 - template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil) + if len(user) > userLen { + return "Benutzername", userLen, false + } else if len(first) > nameLen { + return "Vorname", nameLen, false + } else if len(last) > nameLen { + return "Nachname", nameLen, false + } else { + return "", 0, true } } -func FinishEdit(feed *data.Feed) http.HandlerFunc { +func CreateUser() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - title := r.PostFormValue("editor-title") - desc := r.PostFormValue("editor-desc") - mdContent := r.PostFormValue("editor-text") - - content, err := data.ConvertToHTML(mdContent) - if err != nil { - log.Println(err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - feed.Add(&feeds.Item{ - Title: title, - Created: time.Now(), - Description: desc, - Content: content, - }) - feed.Save("tmp/rss.gob") - - template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil) + template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", nil) } } @@ -121,27 +110,3 @@ func AddUser(db *data.DB) http.HandlerFunc { template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil) } } - -func WriteArticle() http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "page-content", nil) - } -} - -func ShowRSS(feed *data.Feed) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - rss, err := feed.ToRss() - if err != nil { - log.Println(err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - template.Must(template.ParseFiles("web/templates/index.html", "web/templates/feed.rss")).Execute(w, rss) - } -} - -func CreateUser() http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", nil) - } -} diff --git a/cmd/ui/editor.go b/cmd/ui/editor.go new file mode 100644 index 0000000..d96c41e --- /dev/null +++ b/cmd/ui/editor.go @@ -0,0 +1,52 @@ +package ui + +import ( + "html/template" + "log" + "net/http" + "time" + + "github.com/gorilla/feeds" + "streifling.com/jason/cpolis/cmd/data" +) + +func WriteArticle() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "page-content", nil) + } +} + +func FinishEdit(feed *data.Feed) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + title, err := data.ConvertToPlain(r.PostFormValue("editor-title")) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + desc, err := data.ConvertToPlain(r.PostFormValue("editor-desc")) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + content, err := data.ConvertToHTML(r.PostFormValue("editor-text")) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + feed.Add(&feeds.Item{ + Title: title, + Created: time.Now(), + Description: desc, + Content: content, + }) + feed.Save("tmp/rss.gob") + + template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil) + } +} diff --git a/cmd/ui/helpers.go b/cmd/ui/helpers.go deleted file mode 100644 index 8d9401c..0000000 --- a/cmd/ui/helpers.go +++ /dev/null @@ -1,25 +0,0 @@ -package ui - -func inputsEmpty(user, pass, pass2, first, last, role string) bool { - return len(user) == 0 || - len(pass) == 0 || - len(pass2) == 0 || - len(first) == 0 || - len(last) == 0 || - len(role) == 0 -} - -func checkUserStrings(user, first, last string) (string, int, bool) { - userLen := 15 - nameLen := 50 - - if len(user) > userLen { - return "Benutzername", userLen, false - } else if len(first) > nameLen { - return "Vorname", nameLen, false - } else if len(last) > nameLen { - return "Nachname", nameLen, false - } else { - return "", 0, true - } -} diff --git a/cmd/ui/htmlStructs.go b/cmd/ui/htmlStructs.go deleted file mode 100644 index 2c36c45..0000000 --- a/cmd/ui/htmlStructs.go +++ /dev/null @@ -1,9 +0,0 @@ -package ui - -type AddUserData struct { - User string - First string - Last string - Role string - Msg string -} diff --git a/cmd/ui/rss.go b/cmd/ui/rss.go new file mode 100644 index 0000000..75c6479 --- /dev/null +++ b/cmd/ui/rss.go @@ -0,0 +1,21 @@ +package ui + +import ( + "html/template" + "log" + "net/http" + + "streifling.com/jason/cpolis/cmd/data" +) + +func ShowRSS(feed *data.Feed) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + rss, err := feed.ToRss() + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + template.Must(template.ParseFiles("web/templates/index.html", "web/templates/feed.rss")).Execute(w, rss) + } +} diff --git a/cmd/ui/session.go b/cmd/ui/session.go new file mode 100644 index 0000000..fd801fe --- /dev/null +++ b/cmd/ui/session.go @@ -0,0 +1,31 @@ +package ui + +import ( + "html/template" + "log" + "net/http" + + "streifling.com/jason/cpolis/cmd/data" +) + +func Login(db *data.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + user := r.PostFormValue("username") + pass := r.PostFormValue("password") + + id, err := db.GetID(user) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + if err := db.CheckPassword(id, pass); err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil) + } +}