Implemented hub

This commit is contained in:
Jason Streifling 2024-02-24 15:31:33 +01:00
parent 49988edd82
commit 8f5739fb68
4 changed files with 38 additions and 13 deletions

View File

@ -29,7 +29,7 @@ func Login(db *data.DB) http.HandlerFunc {
return return
} }
template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "page-content", nil) template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil)
} }
} }
@ -54,13 +54,7 @@ func FinishEdit(feed *data.Feed) http.HandlerFunc {
}) })
feed.Save("tmp/rss.gob") feed.Save("tmp/rss.gob")
rss, err := feed.ToRss() template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil)
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)
} }
} }
@ -124,6 +118,30 @@ func AddUser(db *data.DB) http.HandlerFunc {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
template.Must(template.ParseFiles("web/templates/editor.html")).Execute(w, nil) 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)
} }
} }

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
@ -40,7 +39,6 @@ func main() {
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
fmt.Println(numRows)
if numRows == 0 { if numRows == 0 {
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 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) template.Must(template.ParseFiles("web/templates/index.html", "web/templates/add-user.html")).Execute(w, nil)
@ -52,8 +50,11 @@ func main() {
} }
mux.HandleFunc("POST /add-user/", ui.AddUser(db)) mux.HandleFunc("POST /add-user/", ui.AddUser(db))
mux.HandleFunc("POST /finished-edit/", ui.FinishEdit(feed)) mux.HandleFunc("POST /create-user/", ui.CreateUser())
mux.HandleFunc("POST /write-article/", ui.WriteArticle())
mux.HandleFunc("POST /finish-edit/", ui.FinishEdit(feed))
mux.HandleFunc("POST /login/", ui.Login(db)) mux.HandleFunc("POST /login/", ui.Login(db))
mux.HandleFunc("/rss/", ui.ShowRSS(feed))
log.Fatalln(http.ListenAndServe(":8080", mux)) log.Fatalln(http.ListenAndServe(":8080", mux))
} }

View File

@ -4,7 +4,7 @@
<input name="editor-title" placeholder="Titel" type="text" /> <input name="editor-title" placeholder="Titel" type="text" />
<textarea name="editor-desc" placeholder="Beschreibung"></textarea> <textarea name="editor-desc" placeholder="Beschreibung"></textarea>
<textarea name="editor-text" placeholder="Artikel"></textarea> <textarea name="editor-text" placeholder="Artikel"></textarea>
<input type="submit" value="Senden" hx-post="/finished-edit/" hx-target="#page-content" /> <input type="submit" value="Senden" hx-post="/finish-edit/" hx-target="#page-content" />
</form> </form>
{{end}} {{end}}

6
web/templates/hub.html Normal file
View File

@ -0,0 +1,6 @@
{{define "page-content"}}
<h2>Hub</h2>
<button hx-post="/write-article/" hx-target="#page-content">Artikel schreiben</button>
<button hx-post="/rss/" hx-target="#page-content">RSS Feed</button>
<button hx-post="/create-user/" hx-target="#page-content">Benutzer hinzufügen</button>
{{end}}