// $Id: recipe-index.go,v 1.3 2025/04/22 13:53:49 oc45ujef Exp $
package main
import (
"encoding/xml"
"fmt"
"log"
"net/http"
"net/http/cgi"
"os"
"io/fs"
)
type Recipe struct {
Title string `xml:"title"`
Icon struct {
Href string `xml:"href,attr"`
} `xml:"icon"`
}
func site(w http.ResponseWriter, r *http.Request) {
files, err := fs.Glob(os.DirFS("."), "*.xml")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
fmt.Fprintln(w, `
Index of /~oc45ujef/recipes
Rezepte
`)
for _, file := range files {
f, err := os.Open(file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
continue
}
defer f.Close()
var parsed Recipe
if err = xml.NewDecoder(f).Decode(&parsed); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
continue
}
fmt.Fprintf(w, "
%s", file, parsed.Title, parsed.Icon.Href, parsed.Title)
}
fmt.Fprint(w, `
`)
}
func main() {
if err := cgi.Serve(http.HandlerFunc(site)); err != nil {
log.Println(err)
}
}