Files
crowsnest/cmd/frontend/main.go

101 lines
2.9 KiB
Go
Raw Normal View History

2024-12-20 01:15:56 +01:00
package main
import (
"crowsnest/internal/data"
"crowsnest/internal/model"
2025-01-02 01:20:26 +01:00
"crowsnest/internal/viewmodel"
"html/template"
"net/http"
2025-01-02 01:20:26 +01:00
"regexp"
2025-01-02 00:35:41 +01:00
"strings"
2024-12-20 01:15:56 +01:00
)
type articleDateOrder struct {}
func (ord articleDateOrder) Weight(a *model.Article) int {
2025-01-02 00:35:41 +01:00
return int(a.PublishDate.Unix())
}
2025-01-02 00:35:41 +01:00
type articleTermFrequency struct {
terms []string
}
func (ord articleTermFrequency) Weight(a *model.Article) int {
score := 0
for _, term := range ord.terms {
score += strings.Count(a.Content, term)
}
return score
}
func index(w http.ResponseWriter, req *http.Request) {
2025-01-02 01:20:26 +01:00
// retrieve from repo
fds, err := data.NewFileDatastore("./persistence/spiegel100.json")
if err != nil { panic(err) }
repo, err := data.NewDefaultRepository[*model.Article](fds, "article")
if err != nil { panic(err) }
articles, err := repo.GetByCriteria(articleDateOrder{})
if err != nil { panic(err) }
2024-12-20 01:15:56 +01:00
2025-01-02 01:20:26 +01:00
// convert to viewmodel
articleVMs := make([]*viewmodel.ArticleViewModel, 0, len(articles))
for _, a := range articles {
articleVMs = append(articleVMs, viewmodel.NewArticleViewModel(a))
}
// render template
t := template.Must(template.ParseFiles("templates/article.html", "templates/layout.html"))
2025-01-02 01:20:26 +01:00
err = t.ExecuteTemplate(w, "base", articleVMs[:10])
if err != nil { panic(err) }
2024-12-27 22:34:43 +01:00
}
2025-01-02 00:35:41 +01:00
func search(w http.ResponseWriter, req *http.Request) {
2025-01-02 01:20:26 +01:00
// parse the form data
2025-01-02 00:35:41 +01:00
err := req.ParseForm()
if err != nil {
http.Error(w, "Unable to parse form", http.StatusBadRequest)
return
}
2025-01-02 01:20:26 +01:00
// collect search terms
p := regexp.MustCompile("\\s+")
searchTerms := make([]string, 0)
for _, elem := range p.Split(req.FormValue("search"), -1) {
elem = strings.TrimSpace(elem)
if elem == "" { continue }
searchTerms = append(searchTerms, elem)
}
2025-01-02 00:35:41 +01:00
2025-01-02 01:20:26 +01:00
// retrieve from repo
2025-01-02 00:35:41 +01:00
fds, _ := data.NewFileDatastore("./persistence/spiegel100.json")
repo, _ := data.NewDefaultRepository[*model.Article](fds, "article")
2025-01-02 01:20:26 +01:00
var articles []*model.Article
if len(searchTerms) != 0 {
articles, _ = repo.GetByCriteria(articleTermFrequency{ terms: searchTerms })
} else {
articles, _ = repo.GetAll()
}
// convert to viewmodel
articleVMs := make([]*viewmodel.ArticleViewModel, 0, len(articles))
for _, a := range articles {
articleVMs = append(articleVMs, viewmodel.NewArticleViewModel(a))
}
2025-01-02 00:35:41 +01:00
t := template.Must(template.ParseFiles("templates/article.html", "templates/layout.html"))
2025-01-02 01:20:26 +01:00
err = t.ExecuteTemplate(w, "base", articleVMs)
if err != nil { panic(err) }
2025-01-02 00:35:41 +01:00
}
2024-12-20 01:15:56 +01:00
func main() {
// routes
http.HandleFunc("/", index)
2025-01-02 00:35:41 +01:00
http.HandleFunc("/search", search)
2024-12-20 01:15:56 +01:00
// serve files from the "static" directory
2024-12-20 01:15:56 +01:00
fs := http.FileServer(http.Dir("./static"))
http.Handle("/static", http.StripPrefix("/", fs))
2024-12-20 01:15:56 +01:00
// start server
http.ListenAndServe(":8080", nil)
2024-12-20 01:15:56 +01:00
}