package main import ( "fmt" "log" "net/http" "os" "os/exec" "text/template" "time" "github.com/mmcdole/gofeed" "gogs.carduccidante.edu.it/karmen/core/util/pandoc" tplutil "gogs.carduccidante.edu.it/karmen/core/util/template" ) var funcmap = template.FuncMap{ "day": func() int { _, _, day := time.Now().Date() return day }, "month": func() int { _, month, _ := time.Now().Date() return int(month) }, "year": func() int { year, _, _ := time.Now().Date() return year }, "news": func() []*gofeed.Item { result := make([]*gofeed.Item, 0) fp := gofeed.NewParser() feed, _ := fp.ParseURL("https://www.agi.it/innovazione/rss") for _, it := range feed.Items { result = append(result, it) } return result[:2] }, "weather": func() []*gofeed.Item { result := make([]*gofeed.Item, 0) fp := gofeed.NewParser() feed, _ := fp.ParseURL("https://www.meteowebcam.eu/rss.xml") for _, it := range feed.Items { result = append(result, it) } return result[:1] }, } func convertPDFToPNG(filename string) error { out, err := exec.Command("pdftoppm", filename, "./data/screen", "-png").CombinedOutput() if string(out) != "" { return fmt.Errorf("pdftoppm: %s", string(out)) } if err != nil { return err } return nil } func serveScreen(res http.ResponseWriter, req *http.Request) { tpl, err := tplutil.LoadTextTemplate("./data/screen.tpl.md", funcmap) if err != nil { panic(err) } f, err := os.Create("./data/screen.md") if err != nil { panic(err) } defer f.Close() var data struct { Title string Item *gofeed.Item } log.Println("Generating markdown file from template.") if err := tpl.Execute(f, data); err != nil { panic(err) } log.Println("Converting markdown to pdf.") err = pandoc.Convert("./data/screen.md", "./data/screen.pdf", "-V", "geometry:papersize={3.8in,6in}", "-V", "pagestyle:empty") if err != nil { panic(err) } log.Println("Converting pdf to png.") err = convertPDFToPNG("./data/screen.pdf") if err != nil { panic(err) } log.Println("Serving screen.png...") http.ServeFile(res, req, "./data/screen-1.png") } func main() { http.HandleFunc("/", serveScreen) log.Println("Start alba backend...") http.ListenAndServe(":3000", nil) }