restructuring; setting up basic webserver and templating

This commit is contained in:
2024-12-30 15:45:40 +01:00
parent e3fcd62159
commit db91ebeece
8 changed files with 66 additions and 37 deletions

View File

@@ -1,41 +1,35 @@
package main
import (
"fmt"
"net/http"
"crowsnest/internal/model"
"crowsnest/internal/data"
//"fmt"
"crowsnest/internal/data"
"crowsnest/internal/model"
"fmt"
"html/template"
"net/http"
)
func test(w http.ResponseWriter, req *http.Request) {
for name, headers := range req.Header {
for _, h := range headers {
fmt.Fprintf(w, "%v: %v\n", name, h)
}
}
}
func PrintAllKeys(ds data.IDatastore) {
m, err := ds.GetAllKeys()
fmt.Println(m, err)
}
func main() {
http.HandleFunc("/test", test)
// Serve files from the "static" directory
fs := http.FileServer(http.Dir("./static"))
http.Handle("/", http.StripPrefix("/", fs))
//a := &model.Article{}
fds, _ := data.NewFileDatastore("data.json")
fmt.Println(fds.GetAll())
func index(w http.ResponseWriter, req *http.Request) {
fds, _ := data.NewFileDatastore("./persistence/spiegel100.json")
repo, _ := data.NewDefaultRepository[*model.Article](fds, "article")
articles, _ := repo.GetAll()
fmt.Println(articles[0])
//http.ListenAndServe(":8090", nil)
t := template.Must(template.ParseFiles("templates/article.html", "templates/layout.html"))
_ = t.ExecuteTemplate(w, "base", articles)
}
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)
}