Added ability to upload media and parse cli arguments

This commit is contained in:
Jason Streifling 2024-03-29 09:07:17 +01:00
parent f49fc8ca0b
commit 1d91d0c216
9 changed files with 215 additions and 95 deletions

44
cmd/control/cli.go Normal file
View File

@ -0,0 +1,44 @@
package control
import (
"flag"
"fmt"
"path/filepath"
)
type CliArgs struct {
KeyFile string
LogFile string
PicsDir string
WebDir string
}
func HandleCliArgs() (*CliArgs, error) {
var err error
cliArgs := new(CliArgs)
keyFile := flag.String("key", "/var/www/cpolis.key", "key file")
logFile := flag.String("log", "/var/log/cpolis.log", "log file")
picsDir := flag.String("pics", "/var/www/cpolis/pics", "pictures directory")
webDir := flag.String("web", "/var/www/cpolis/web", "web directory")
flag.Parse()
cliArgs.KeyFile, err = filepath.Abs(*keyFile)
if err != nil {
return nil, fmt.Errorf("error finding KeyFile: %v", err)
}
cliArgs.LogFile, err = filepath.Abs(*logFile)
if err != nil {
return nil, fmt.Errorf("error finding LogFile: %v", err)
}
cliArgs.PicsDir, err = filepath.Abs(*picsDir)
if err != nil {
return nil, fmt.Errorf("error finding PicsDir: %v", err)
}
cliArgs.WebDir, err = filepath.Abs(*webDir)
if err != nil {
return nil, fmt.Errorf("error finding WebDir: %v", err)
}
return cliArgs, nil
}

View File

@ -16,13 +16,17 @@ func init() {
} }
func main() { func main() {
logFile, err := os.OpenFile("tmp/cpolis.log", cliArgs, err := control.HandleCliArgs()
os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) if err != nil {
log.Fatalln(err)
}
logFile, err := os.OpenFile(cliArgs.LogFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
defer logFile.Close() defer logFile.Close()
// log.SetOutput(logFile) log.SetOutput(logFile)
db, err := model.OpenDB("cpolis") db, err := model.OpenDB("cpolis")
if err != nil { if err != nil {
@ -30,48 +34,49 @@ func main() {
} }
defer db.Close() defer db.Close()
key, err := control.LoadKey("tmp/key.gob") key, err := control.LoadKey(cliArgs.KeyFile)
if err != nil { if err != nil {
key, err = control.NewKey() key, err = control.NewKey()
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
control.SaveKey(key, "tmp/key.gob") control.SaveKey(key, cliArgs.KeyFile)
} }
store := control.NewCookieStore(key) store := control.NewCookieStore(key)
mux := http.NewServeMux() mux := http.NewServeMux()
mux.Handle("/web/static/", http.StripPrefix("/web/static/", mux.Handle("/web/static/", http.StripPrefix("/web/static/",
http.FileServer(http.Dir("web/static/")))) http.FileServer(http.Dir(cliArgs.WebDir+"/static/"))))
mux.HandleFunc("/", view.HomePage(db, store)) mux.HandleFunc("/", view.HomePage(cliArgs, db, store))
mux.HandleFunc("GET /create-tag/", view.CreateTag) mux.HandleFunc("GET /create-tag/", view.CreateTag(cliArgs))
mux.HandleFunc("GET /create-user/", view.CreateUser) mux.HandleFunc("GET /create-user/", view.CreateUser(cliArgs))
mux.HandleFunc("GET /edit-user/", view.EditUser(db, store)) mux.HandleFunc("GET /edit-user/", view.EditUser(cliArgs, db, store))
mux.HandleFunc("GET /hub/", view.ShowHub(db, store)) mux.HandleFunc("GET /hub/", view.ShowHub(cliArgs, db, store))
mux.HandleFunc("GET /logout/", view.Logout(store)) mux.HandleFunc("GET /logout/", view.Logout(cliArgs, store))
mux.HandleFunc("GET /publish-issue/", view.PublishLatestIssue(db, store)) mux.HandleFunc("GET /publish-issue/", view.PublishLatestIssue(cliArgs, db, store))
mux.HandleFunc("GET /rejected-articles/", view.ShowRejectedArticles(db, store)) mux.HandleFunc("GET /rejected-articles/", view.ShowRejectedArticles(cliArgs, db, store))
mux.HandleFunc("GET /rss/", view.ShowRSS( mux.HandleFunc("GET /rss/", view.ShowRSS(cliArgs,
db, db,
"Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt", "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.HandleFunc("GET /this-issue/", view.ShowCurrentArticles(db)) mux.HandleFunc("GET /this-issue/", view.ShowCurrentArticles(cliArgs, db))
mux.HandleFunc("GET /unpublished-articles/", view.ShowUnpublishedArticles(db)) mux.HandleFunc("GET /unpublished-articles/", view.ShowUnpublishedArticles(cliArgs, db))
mux.HandleFunc("GET /write-article/", view.WriteArticle(db)) mux.HandleFunc("GET /write-article/", view.WriteArticle(cliArgs, db))
mux.HandleFunc("POST /add-tag/", view.AddTag(db, store)) mux.HandleFunc("POST /add-tag/", view.AddTag(cliArgs, db, store))
mux.HandleFunc("POST /add-user/", view.AddUser(db, store)) mux.HandleFunc("POST /add-user/", view.AddUser(cliArgs, db, store))
mux.HandleFunc("POST /login/", view.Login(db, store)) mux.HandleFunc("POST /login/", view.Login(cliArgs, db, store))
mux.HandleFunc("POST /publish-article/", view.PublishArticle(db, store)) mux.HandleFunc("POST /publish-article/", view.PublishArticle(cliArgs, db, store))
mux.HandleFunc("POST /reject-article/", view.RejectArticle(db, store)) mux.HandleFunc("POST /reject-article/", view.RejectArticle(cliArgs, db, store))
mux.HandleFunc("POST /resubmit-article/", view.ResubmitArticle(db, store)) mux.HandleFunc("POST /resubmit-article/", view.ResubmitArticle(cliArgs, db, store))
mux.HandleFunc("POST /review-rejected-article/", view.ReviewRejectedArticle(db, store)) mux.HandleFunc("POST /review-rejected-article/", view.ReviewRejectedArticle(cliArgs, db, store))
mux.HandleFunc("POST /review-unpublished-article/", view.ReviewUnpublishedArticle(db, store)) mux.HandleFunc("POST /review-unpublished-article/", view.ReviewUnpublishedArticle(cliArgs, db, store))
mux.HandleFunc("POST /submit-article/", view.SubmitArticle(db, store)) mux.HandleFunc("POST /submit-article/", view.SubmitArticle(cliArgs, db, store))
mux.HandleFunc("POST /update-user/", view.UpdateUser(db, store)) mux.HandleFunc("POST /update-user/", view.UpdateUser(cliArgs, db, store))
mux.HandleFunc("POST /upload-image/", view.UploadImage(cliArgs))
log.Fatalln(http.ListenAndServe(":8080", mux)) log.Fatalln(http.ListenAndServe(":8080", mux))
} }

View File

@ -1,9 +1,12 @@
package view package view
import ( import (
"fmt"
"html/template" "html/template"
"io"
"log" "log"
"net/http" "net/http"
"os"
"strconv" "strconv"
"time" "time"
@ -11,21 +14,21 @@ import (
"streifling.com/jason/cpolis/cmd/model" "streifling.com/jason/cpolis/cmd/model"
) )
func ShowHub(db *model.DB, s *control.CookieStore) http.HandlerFunc { func ShowHub(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
session, err := s.Get(r, "cookie") session, err := s.Get(r, "cookie")
if err != nil { if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden." msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
} }
tmpl, err := template.ParseFiles("web/templates/hub.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", session.Values["role"].(int)) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", session.Values["role"].(int))
} }
} }
func WriteArticle(db *model.DB) http.HandlerFunc { func WriteArticle(c *control.CliArgs, db *model.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
tags, err := db.GetTagList() tags, err := db.GetTagList()
if err != nil { if err != nil {
@ -34,16 +37,16 @@ func WriteArticle(db *model.DB) http.HandlerFunc {
return return
} }
tmpl, err := template.ParseFiles("web/templates/editor.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", tags) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", tags)
} }
} }
func SubmitArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc { func SubmitArticle(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
session, err := s.Get(r, "cookie") session, err := s.Get(r, "cookie")
if err != nil { if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden." msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
} }
@ -81,13 +84,13 @@ func SubmitArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc {
return return
} }
tmpl, err := template.ParseFiles("web/templates/hub.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
tmpl = template.Must(tmpl, err) tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"]) tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
} }
} }
func ResubmitArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc { func ResubmitArticle(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PostFormValue("article-id"), 10, 64) id, err := strconv.ParseInt(r.PostFormValue("article-id"), 10, 64)
if err != nil { if err != nil {
@ -130,18 +133,18 @@ func ResubmitArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc {
session, err := s.Get(r, "cookie") session, err := s.Get(r, "cookie")
if err != nil { if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden." msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
} }
tmpl, err := template.ParseFiles("web/templates/hub.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
tmpl = template.Must(tmpl, err) tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"]) tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
} }
} }
func ShowUnpublishedArticles(db *model.DB) http.HandlerFunc { func ShowUnpublishedArticles(c *control.CliArgs, db *model.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
unpublishedArticles, err := db.GetCertainArticles(false, false) unpublishedArticles, err := db.GetCertainArticles(false, false)
if err != nil { if err != nil {
@ -150,13 +153,13 @@ func ShowUnpublishedArticles(db *model.DB) http.HandlerFunc {
return return
} }
tmpl, err := template.ParseFiles("web/templates/unpublished-articles.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/unpublished-articles.html")
tmpl = template.Must(tmpl, err) tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", unpublishedArticles) tmpl.ExecuteTemplate(w, "page-content", unpublishedArticles)
} }
} }
func ShowRejectedArticles(db *model.DB, s *control.CookieStore) http.HandlerFunc { func ShowRejectedArticles(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
type htmlData struct { type htmlData struct {
MyIDs map[int64]bool MyIDs map[int64]bool
@ -185,13 +188,13 @@ func ShowRejectedArticles(db *model.DB, s *control.CookieStore) http.HandlerFunc
} }
} }
tmpl, err := template.ParseFiles("web/templates/rejected-articles.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/rejected-articles.html")
tmpl = template.Must(tmpl, err) tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", data) tmpl.ExecuteTemplate(w, "page-content", data)
} }
} }
func ReviewUnpublishedArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc { func ReviewUnpublishedArticle(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
type htmlData struct { type htmlData struct {
Article *model.Article Article *model.Article
@ -220,13 +223,13 @@ func ReviewUnpublishedArticle(db *model.DB, s *control.CookieStore) http.Handler
return return
} }
tmpl, err := template.ParseFiles("web/templates/to-be-published.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/to-be-published.html")
tmpl = template.Must(tmpl, err) tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", data) tmpl.ExecuteTemplate(w, "page-content", data)
} }
} }
func ReviewRejectedArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc { func ReviewRejectedArticle(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
type htmlData struct { type htmlData struct {
Selected map[int64]bool Selected map[int64]bool
@ -267,13 +270,13 @@ func ReviewRejectedArticle(db *model.DB, s *control.CookieStore) http.HandlerFun
data.Selected[tag.ID] = true data.Selected[tag.ID] = true
} }
tmpl, err := template.ParseFiles("web/templates/rework-article.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/rework-article.html")
tmpl = template.Must(tmpl, err) tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", data) tmpl.ExecuteTemplate(w, "page-content", data)
} }
} }
func PublishArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc { func PublishArticle(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PostFormValue("id"), 10, 64) id, err := strconv.ParseInt(r.PostFormValue("id"), 10, 64)
if err != nil { if err != nil {
@ -284,7 +287,7 @@ func PublishArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc {
session, err := s.Get(r, "cookie") session, err := s.Get(r, "cookie")
if err != nil { if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden." msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
} }
@ -322,13 +325,13 @@ func PublishArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc {
return return
} }
tmpl, err := template.ParseFiles("web/templates/hub.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
tmpl = template.Must(tmpl, err) tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"]) tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
} }
} }
func RejectArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc { func RejectArticle(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PostFormValue("id"), 10, 64) id, err := strconv.ParseInt(r.PostFormValue("id"), 10, 64)
if err != nil { if err != nil {
@ -339,7 +342,7 @@ func RejectArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc {
session, err := s.Get(r, "cookie") session, err := s.Get(r, "cookie")
if err != nil { if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden." msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
} }
@ -352,13 +355,13 @@ func RejectArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc {
return return
} }
tmpl, err := template.ParseFiles("web/templates/hub.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
tmpl = template.Must(tmpl, err) tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"]) tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
} }
} }
func ShowCurrentArticles(db *model.DB) http.HandlerFunc { func ShowCurrentArticles(c *control.CliArgs, db *model.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
articles, err := db.GetCurrentIssueArticles() articles, err := db.GetCurrentIssueArticles()
if err != nil { if err != nil {
@ -367,7 +370,37 @@ func ShowCurrentArticles(db *model.DB) http.HandlerFunc {
return return
} }
tmpl, err := template.ParseFiles("web/templates/current-articles.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/current-articles.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", articles) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", articles)
} }
} }
func UploadImage(c *control.CliArgs) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
file, header, err := r.FormFile("article-image")
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
filename := fmt.Sprint("tmp/pics/", time.Now().Format("2006-01-02_15:04:05"), "-", header.Filename)
img, err := os.Create(filename)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer img.Close()
if _, err = io.Copy(img, file); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html")
template.Must(tmpl, err).ExecuteTemplate(w, "editor-images", fmt.Sprint("![", header.Filename, "](", filename, ")"))
}
}

View File

@ -8,23 +8,25 @@ import (
"streifling.com/jason/cpolis/cmd/model" "streifling.com/jason/cpolis/cmd/model"
) )
func CreateTag(w http.ResponseWriter, r *http.Request) { func CreateTag(c *control.CliArgs) http.HandlerFunc {
tmpl, err := template.ParseFiles("web/templates/add-tag.html") return func(w http.ResponseWriter, r *http.Request) {
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-tag.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
}
} }
func AddTag(db *model.DB, s *control.CookieStore) http.HandlerFunc { func AddTag(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
db.AddTag(r.PostFormValue("tag")) db.AddTag(r.PostFormValue("tag"))
session, err := s.Get(r, "cookie") session, err := s.Get(r, "cookie")
if err != nil { if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden." msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
} }
tmpl, err := template.ParseFiles("web/templates/hub.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
tmpl = template.Must(tmpl, err) tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"]) tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
} }

View File

@ -9,7 +9,7 @@ import (
"streifling.com/jason/cpolis/cmd/model" "streifling.com/jason/cpolis/cmd/model"
) )
func PublishLatestIssue(db *model.DB, s *control.CookieStore) http.HandlerFunc { func PublishLatestIssue(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if err := db.PublishLatestIssue(); err != nil { if err := db.PublishLatestIssue(); err != nil {
log.Println(err) log.Println(err)
@ -19,12 +19,12 @@ func PublishLatestIssue(db *model.DB, s *control.CookieStore) http.HandlerFunc {
session, err := s.Get(r, "cookie") session, err := s.Get(r, "cookie")
if err != nil { if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden." msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
} }
tmpl, err := template.ParseFiles("web/templates/hub.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
tmpl = template.Must(tmpl, err) tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"]) tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
} }

View File

@ -12,7 +12,7 @@ import (
"streifling.com/jason/cpolis/cmd/model" "streifling.com/jason/cpolis/cmd/model"
) )
func ShowRSS(db *model.DB, title, link, desc string) http.HandlerFunc { func ShowRSS(c *control.CliArgs, db *model.DB, title, link, desc string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
channel := &rss.Channel{ channel := &rss.Channel{
Title: title, Title: title,
@ -88,7 +88,7 @@ func ShowRSS(db *model.DB, title, link, desc string) http.HandlerFunc {
return return
} }
files := []string{"web/templates/index.html", "web/templates/feed.rss"} files := []string{c.WebDir + "/templates/index.html", c.WebDir + "/templates/feed.rss"}
tmpl, err := template.ParseFiles(files...) tmpl, err := template.ParseFiles(files...)
template.Must(tmpl, err).Execute(w, rss) template.Must(tmpl, err).Execute(w, rss)
} }

View File

@ -27,26 +27,26 @@ func saveSession(w http.ResponseWriter, r *http.Request, s *control.CookieStore,
return nil return nil
} }
func HomePage(db *model.DB, s *control.CookieStore) http.HandlerFunc { func HomePage(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
numRows, err := db.CountEntries("users") numRows, err := db.CountEntries("users")
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
files := []string{"web/templates/index.html"} files := []string{c.WebDir + "/templates/index.html"}
if numRows == 0 { if numRows == 0 {
files = append(files, "web/templates/add-user.html") files = append(files, c.WebDir+"/templates/add-user.html")
tmpl, err := template.ParseFiles(files...) tmpl, err := template.ParseFiles(files...)
template.Must(tmpl, err).Execute(w, nil) template.Must(tmpl, err).Execute(w, nil)
} else { } else {
session, _ := s.Get(r, "cookie") session, _ := s.Get(r, "cookie")
if auth, ok := session.Values["authenticated"].(bool); auth && ok { if auth, ok := session.Values["authenticated"].(bool); auth && ok {
files = append(files, "web/templates/hub.html") files = append(files, c.WebDir+"/templates/hub.html")
tmpl, err := template.ParseFiles(files...) tmpl, err := template.ParseFiles(files...)
template.Must(tmpl, err).Execute(w, session.Values["role"]) template.Must(tmpl, err).Execute(w, session.Values["role"])
} else { } else {
files = append(files, "web/templates/login.html") files = append(files, c.WebDir+"/templates/login.html")
tmpl, err := template.ParseFiles(files...) tmpl, err := template.ParseFiles(files...)
template.Must(tmpl, err).Execute(w, nil) template.Must(tmpl, err).Execute(w, nil)
} }
@ -54,7 +54,7 @@ func HomePage(db *model.DB, s *control.CookieStore) http.HandlerFunc {
} }
} }
func Login(db *model.DB, s *control.CookieStore) http.HandlerFunc { func Login(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
userName := r.PostFormValue("username") userName := r.PostFormValue("username")
password := r.PostFormValue("password") password := r.PostFormValue("password")
@ -84,16 +84,16 @@ func Login(db *model.DB, s *control.CookieStore) http.HandlerFunc {
return return
} }
tmpl, err := template.ParseFiles("web/templates/hub.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", user.Role) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", user.Role)
} }
} }
func Logout(s *control.CookieStore) http.HandlerFunc { func Logout(c *control.CliArgs, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
session, err := s.Get(r, "cookie") session, err := s.Get(r, "cookie")
if err != nil { if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden." msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
} }
@ -106,7 +106,7 @@ func Logout(s *control.CookieStore) http.HandlerFunc {
return return
} }
tmpl, err := template.ParseFiles("web/templates/login.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
} }
} }

View File

@ -31,12 +31,14 @@ func checkUserStrings(user *model.User) (string, int, bool) {
} }
} }
func CreateUser(w http.ResponseWriter, r *http.Request) { func CreateUser(c *control.CliArgs) http.HandlerFunc {
tmpl, err := template.ParseFiles("web/templates/add-user.html") return func(w http.ResponseWriter, r *http.Request) {
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
}
} }
func AddUser(db *model.DB, s *control.CookieStore) http.HandlerFunc { func AddUser(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
role, err := strconv.Atoi(r.PostFormValue("role")) role, err := strconv.Atoi(r.PostFormValue("role"))
if err != nil { if err != nil {
@ -59,7 +61,7 @@ func AddUser(db *model.DB, s *control.CookieStore) http.HandlerFunc {
if len(htmlData.UserName) == 0 || len(htmlData.FirstName) == 0 || if len(htmlData.UserName) == 0 || len(htmlData.FirstName) == 0 ||
len(htmlData.LastName) == 0 || len(pass) == 0 || len(pass2) == 0 { len(htmlData.LastName) == 0 || len(pass) == 0 || len(pass2) == 0 {
htmlData.Msg = "Alle Felder müssen ausgefüllt werden." htmlData.Msg = "Alle Felder müssen ausgefüllt werden."
tmpl, err := template.ParseFiles("web/templates/add-user.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData)
return return
} }
@ -67,7 +69,7 @@ func AddUser(db *model.DB, s *control.CookieStore) http.HandlerFunc {
if !ok { if !ok {
htmlData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ", htmlData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ",
stringLen, " Zeichen erlaubt.") stringLen, " Zeichen erlaubt.")
tmpl, err := template.ParseFiles("web/templates/add-user.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData)
return return
} }
@ -75,13 +77,13 @@ func AddUser(db *model.DB, s *control.CookieStore) http.HandlerFunc {
if id != 0 { if id != 0 {
htmlData.Msg = fmt.Sprint(htmlData.UserName, htmlData.Msg = fmt.Sprint(htmlData.UserName,
" ist bereits vergeben. Bitte anderen Benutzernamen wählen.") " ist bereits vergeben. Bitte anderen Benutzernamen wählen.")
tmpl, err := template.ParseFiles("web/templates/add-user.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData)
return return
} }
if pass != pass2 { if pass != pass2 {
htmlData.Msg = "Die Passwörter stimmen nicht überein." htmlData.Msg = "Die Passwörter stimmen nicht überein."
tmpl, err := template.ParseFiles("web/templates/add-user.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData)
return return
} }
@ -117,16 +119,16 @@ func AddUser(db *model.DB, s *control.CookieStore) http.HandlerFunc {
} }
} }
tmpl, err := template.ParseFiles("web/templates/hub.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", 0) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", 0)
} }
} }
func EditUser(db *model.DB, s *control.CookieStore) http.HandlerFunc { func EditUser(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
session, err := s.Get(r, "cookie") session, err := s.Get(r, "cookie")
if err != nil { if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden." msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
} }
@ -138,16 +140,16 @@ func EditUser(db *model.DB, s *control.CookieStore) http.HandlerFunc {
return return
} }
tmpl, err := template.ParseFiles("web/templates/edit-user.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", user) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", user)
} }
} }
func UpdateUser(db *model.DB, s *control.CookieStore) http.HandlerFunc { func UpdateUser(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
session, err := s.Get(r, "cookie") session, err := s.Get(r, "cookie")
if err != nil { if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden." msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
} }
@ -167,7 +169,7 @@ func UpdateUser(db *model.DB, s *control.CookieStore) http.HandlerFunc {
if len(userData.UserName) == 0 || len(userData.FirstName) == 0 || if len(userData.UserName) == 0 || len(userData.FirstName) == 0 ||
len(userData.LastName) == 0 { len(userData.LastName) == 0 {
userData.Msg = "Alle Felder mit * müssen ausgefüllt sein." userData.Msg = "Alle Felder mit * müssen ausgefüllt sein."
tmpl, err := template.ParseFiles("web/templates/edit-user.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html")
tmpl = template.Must(tmpl, err) tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", userData.Msg) tmpl.ExecuteTemplate(w, "page-content", userData.Msg)
return return
@ -177,7 +179,7 @@ func UpdateUser(db *model.DB, s *control.CookieStore) http.HandlerFunc {
if !ok { if !ok {
userData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ", userData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ",
stringLen, " Zeichen erlaubt.") stringLen, " Zeichen erlaubt.")
tmpl, err := template.ParseFiles("web/templates/edit-user.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html")
tmpl = template.Must(tmpl, err) tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", userData) tmpl.ExecuteTemplate(w, "page-content", userData)
return return
@ -187,7 +189,7 @@ func UpdateUser(db *model.DB, s *control.CookieStore) http.HandlerFunc {
if id != userData.ID { if id != userData.ID {
userData.Msg = "Benutzername bereits vergeben." userData.Msg = "Benutzername bereits vergeben."
userData.UserName = "" userData.UserName = ""
tmpl, err := template.ParseFiles("web/templates/edit-user.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html")
tmpl = template.Must(tmpl, err) tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", userData) tmpl.ExecuteTemplate(w, "page-content", userData)
return return
@ -203,11 +205,11 @@ func UpdateUser(db *model.DB, s *control.CookieStore) http.HandlerFunc {
newPass, newPass,
newPass2); err != nil { newPass2); err != nil {
userData.Msg = "Aktualisierung der Benutzerdaten fehlgeschlagen." userData.Msg = "Aktualisierung der Benutzerdaten fehlgeschlagen."
tmpl, err := template.ParseFiles("web/templates/edit-user.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", userData) template.Must(tmpl, err).ExecuteTemplate(w, "page-content", userData)
} }
tmpl, err := template.ParseFiles("web/templates/hub.html") tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
tmpl = template.Must(tmpl, err) tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int)) tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int))
} }

View File

@ -16,8 +16,42 @@
{{end}} {{end}}
</div> </div>
<div id="editor-images">
<input name="article-image" type="file" hx-encoding="multipart/form-data" hx-post="/upload-image/"
hx-swap="beforeend" hx-target="#editor-images" />
</div>
<input type="submit" value="Senden" hx-post="/submit-article/" hx-target="#page-content" /> <input type="submit" value="Senden" hx-post="/submit-article/" hx-target="#page-content" />
</form> </form>
<button hx-get="/hub/" hx-target="#page-content">Abbrechen</button> <button hx-get="/hub/" hx-target="#page-content">Abbrechen</button>
<script>
function copyToClipboard(text) {
event.preventDefault(); // Get-Request verhindern
var textarea = document.createElement("textarea");
textarea.textContent = text;
document.body.appendChild(textarea);
textarea.select();
try {
document.execCommand('copy');
} catch (err) {
console.warn('Fehler beim Kopieren', err);
}
document.body.removeChild(textarea);
}
</script>
{{end}}
{{define "editor-images"}}
{{if gt (len .) 0}}
<div>
{{.}}
<button onclick="copyToClipboard('{{.}}')">Kopieren</button>
</div>
{{end}}
{{end}} {{end}}