mirror of https://git.sr.ht/~statianzo/todogo
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.1 KiB
58 lines
1.1 KiB
2 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"embed"
|
||
|
"html/template"
|
||
|
"io"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
//go:embed views/*.html
|
||
|
var tmplFS embed.FS
|
||
|
|
||
|
//go:embed public/*
|
||
|
var publicFS embed.FS
|
||
|
|
||
|
type Template struct {
|
||
|
templates *template.Template
|
||
|
}
|
||
|
|
||
|
func New() *Template {
|
||
|
templates := template.Must(template.New("").ParseFS(tmplFS, "views/*.html"))
|
||
|
|
||
|
return &Template{
|
||
|
templates: templates,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (t *Template) Render(w io.Writer, name string, data any) error {
|
||
|
tmpl := template.Must(t.templates.Clone())
|
||
|
tmpl = template.Must(tmpl.ParseFS(tmplFS, "views/"+name))
|
||
|
return tmpl.ExecuteTemplate(w, "layout.html", data)
|
||
|
}
|
||
|
|
||
|
type Todo struct {
|
||
|
Id string
|
||
|
Title string
|
||
|
Completed bool
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
t := New()
|
||
|
http.Handle("/public/", http.FileServer(http.FS(publicFS)))
|
||
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||
|
err := t.Render(w, "list.html", nil)
|
||
|
if err != nil {
|
||
|
log.Println(err)
|
||
|
}
|
||
|
})
|
||
|
http.HandleFunc("/show", func(w http.ResponseWriter, r *http.Request) {
|
||
|
err := t.Render(w, "show.html", nil)
|
||
|
if err != nil {
|
||
|
log.Println(err)
|
||
|
}
|
||
|
})
|
||
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||
|
}
|