41 lines
1.0 KiB
Go
41 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
//"fmt"
|
|
"crowsnest/internal/data"
|
|
"crowsnest/internal/model"
|
|
"fmt"
|
|
"html/template"
|
|
"net/http"
|
|
)
|
|
|
|
type articleDateOrder struct {}
|
|
func (ord articleDateOrder) Weight(a *model.Article) int {
|
|
return int(a.PublishDate.Unix()) * -1
|
|
}
|
|
|
|
func index(w http.ResponseWriter, req *http.Request) {
|
|
fds, _ := data.NewFileDatastore("./persistence/spiegel100.json")
|
|
|
|
repo, _ := data.NewDefaultRepository[*model.Article](fds, "article")
|
|
articles, _ := repo.GetByCriteria(articleDateOrder{})
|
|
|
|
t := template.Must(template.ParseFiles("templates/article.html", "templates/layout.html"))
|
|
_ = t.ExecuteTemplate(w, "base", articles[:10])
|
|
}
|
|
|
|
func main() {
|
|
// routes
|
|
http.HandleFunc("/", index)
|
|
|
|
// serve files from the "static" directory
|
|
fs := http.FileServer(http.Dir("./static"))
|
|
http.Handle("/static", http.StripPrefix("/", fs))
|
|
|
|
t := template.Must(template.ParseFiles("templates/article.html"))
|
|
fmt.Println(t)
|
|
|
|
// start server
|
|
http.ListenAndServe(":8080", nil)
|
|
}
|