2022-04-20 11:16:55 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gogs.carduccidante.edu.it/karmen/core/util/pandoc"
|
|
|
|
tplutil "gogs.carduccidante.edu.it/karmen/core/util/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
var funcmap = template.FuncMap{
|
|
|
|
"date": func() string {
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
return now.Format("2006-02-01")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertPDFToPNG(filename string) error {
|
2022-04-20 11:30:18 +02:00
|
|
|
out, err := exec.Command("pdftoppm", filename, "./data/screen", "-png").CombinedOutput()
|
2022-04-20 11:16:55 +02:00
|
|
|
if string(out) != "" {
|
2022-04-20 11:30:18 +02:00
|
|
|
return fmt.Errorf("pdftoppm: %s", string(out))
|
2022-04-20 11:16:55 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveScreen(res http.ResponseWriter, req *http.Request) {
|
2022-04-20 11:30:18 +02:00
|
|
|
tpl, err := tplutil.LoadTextTemplate("./data/screen.tpl.md", funcmap)
|
2022-04-20 11:16:55 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create("./data/screen.md")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
log.Println("Generating markdown file from template.")
|
|
|
|
if err := tpl.Execute(f, nil); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Converting markdown to pdf.")
|
|
|
|
err = pandoc.Convert("./data/screen.md", "./data/screen.pdf", "-V", "geometry:papersize={3.8in,6.4in}", "-V", "pagestyle:empty")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-04-20 11:30:18 +02:00
|
|
|
log.Println("Converting pdf to png.")
|
|
|
|
err = convertPDFToPNG("./data/screen.pdf")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-04-20 11:16:55 +02:00
|
|
|
log.Println("Serving screen.png...")
|
2022-04-20 11:30:18 +02:00
|
|
|
http.ServeFile(res, req, "./data/screen-1.png")
|
2022-04-20 11:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/", serveScreen)
|
|
|
|
|
|
|
|
log.Println("Start alba backend...")
|
|
|
|
http.ListenAndServe(":8080", nil)
|
|
|
|
}
|