webweav.ing/cmd/web/handlers.go

51 lines
1.3 KiB
Go
Raw Normal View History

2024-01-19 03:33:37 +00:00
package main
2024-01-22 20:43:46 +00:00
import (
2024-03-03 17:53:09 +00:00
// "html"
2024-01-22 20:43:46 +00:00
"net/http"
"strings"
2024-03-03 21:10:34 +00:00
"git.32bit.cafe/yequari/webweav.ing/feed"
2024-01-22 20:43:46 +00:00
)
2024-01-19 03:33:37 +00:00
func (app *application) home(w http.ResponseWriter, r *http.Request) {
2024-01-22 20:43:46 +00:00
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
2024-03-03 21:05:19 +00:00
data := newTemplateData(r)
data.Title = "Home"
app.renderPage(w, http.StatusOK, "home.tmpl.html", data)
}
2024-01-22 20:43:46 +00:00
2024-03-03 21:05:19 +00:00
func (app *application) feedgenView(w http.ResponseWriter, r *http.Request) {
data := newTemplateData(r)
data.Title = "Feed Generator"
app.renderPage(w, http.StatusOK, "feedgenview.tmpl.html", data)
2024-01-19 03:33:37 +00:00
}
func (app *application) generateRss(w http.ResponseWriter, r *http.Request) {
2024-01-22 20:43:46 +00:00
q := r.URL.Query()
siteName := q.Get("site-name")
siteUrl := q.Get("site-url")
siteDesc := q.Get("site-description")
pageUrls := q.Get("page-urls")
pages := strings.Split(pageUrls, "\n")
for i := range pages {
pages[i] = strings.TrimSpace(pages[i])
}
2024-03-03 17:53:09 +00:00
feedInfo, err := feed.NewFeedInfo(siteName, siteUrl, siteDesc, "", pages...)
2024-01-22 20:43:46 +00:00
if err != nil {
2024-03-03 17:53:09 +00:00
app.errorLog.Printf("Error generating feed: %s\n", err.Error())
return
2024-01-22 20:43:46 +00:00
}
2024-03-03 17:53:09 +00:00
feed := feedInfo.GenerateRSS()
data := newTemplateData(r)
2024-03-03 21:51:19 +00:00
data.Errors = feedInfo.Errors
2024-03-03 17:53:09 +00:00
data.Feeds = append(data.Feeds, feed)
app.renderElem(w, http.StatusOK, "feed-output.tmpl.html", data)
2024-01-19 03:33:37 +00:00
}