2021-12-22 10:52:02 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-03-15 13:53:24 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"hash/fnv"
|
|
|
|
"html/template"
|
2021-12-22 10:52:02 +00:00
|
|
|
"io"
|
2022-03-15 13:53:24 +00:00
|
|
|
"net/http"
|
|
|
|
"path"
|
2021-12-22 10:52:02 +00:00
|
|
|
|
2022-03-15 13:53:24 +00:00
|
|
|
"github.com/Masterminds/sprig/v3"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-12-22 10:52:02 +00:00
|
|
|
"go.uber.org/multierr"
|
|
|
|
|
2022-09-25 12:23:22 +00:00
|
|
|
"code.icb4dc0.de/prskr/goveal/config"
|
|
|
|
"code.icb4dc0.de/prskr/goveal/fs"
|
|
|
|
"code.icb4dc0.de/prskr/goveal/rendering"
|
|
|
|
"code.icb4dc0.de/prskr/goveal/web"
|
2021-12-22 10:52:02 +00:00
|
|
|
)
|
|
|
|
|
2022-03-15 13:53:24 +00:00
|
|
|
var indexTmpl *template.Template
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if t, err := template.
|
|
|
|
New("index").
|
|
|
|
Funcs(sprig.FuncMap()).
|
2022-03-15 19:22:04 +00:00
|
|
|
Funcs(map[string]any{
|
2022-03-15 13:53:24 +00:00
|
|
|
"fileId": func(fileName string) string {
|
|
|
|
h := fnv.New32a()
|
|
|
|
return hex.EncodeToString(h.Sum([]byte(path.Base(fileName))))
|
|
|
|
},
|
|
|
|
}).
|
|
|
|
ParseFS(web.WebFS, "*.gohtml"); err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else {
|
|
|
|
indexTmpl = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-22 10:52:02 +00:00
|
|
|
type Views struct {
|
2022-03-15 13:53:24 +00:00
|
|
|
logger *log.Logger
|
2021-12-22 10:52:02 +00:00
|
|
|
cfg *config.Components
|
|
|
|
wdfs fs.FS
|
|
|
|
mdFilepath string
|
|
|
|
}
|
|
|
|
|
2022-03-15 13:53:24 +00:00
|
|
|
func RegisterViews(router *httprouter.Router, logger *log.Logger, wdfs fs.FS, mdFilepath string, cfg *config.Components) {
|
|
|
|
p := &Views{
|
|
|
|
logger: logger,
|
|
|
|
cfg: cfg,
|
|
|
|
wdfs: wdfs,
|
|
|
|
mdFilepath: mdFilepath,
|
|
|
|
}
|
|
|
|
router.GET("/", p.IndexPage)
|
|
|
|
router.GET("/index.html", p.IndexPage)
|
|
|
|
router.GET("/index.htm", p.IndexPage)
|
|
|
|
router.GET("/slides", p.RenderedMarkdown)
|
2021-12-22 10:52:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 13:53:24 +00:00
|
|
|
func (p *Views) IndexPage(writer http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
|
|
|
|
writer.Header().Set("Content-Type", "text/html")
|
2022-03-15 19:22:04 +00:00
|
|
|
if err := indexTmpl.ExecuteTemplate(writer, "index.gohtml", map[string]any{
|
2021-12-22 10:52:02 +00:00
|
|
|
"Reveal": p.cfg.Reveal,
|
|
|
|
"Rendering": p.cfg.Rendering,
|
2022-03-15 13:53:24 +00:00
|
|
|
}); err != nil {
|
|
|
|
p.logger.Errorf("Failed to render template: %v", err)
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
_, _ = writer.Write([]byte(err.Error()))
|
|
|
|
}
|
2021-12-22 10:52:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 13:53:24 +00:00
|
|
|
func (p *Views) RenderedMarkdown(writer http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
|
2021-12-22 10:52:02 +00:00
|
|
|
f, err := p.wdfs.Open(p.mdFilepath)
|
|
|
|
if err != nil {
|
2022-03-15 13:53:24 +00:00
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
_, _ = writer.Write([]byte(err.Error()))
|
|
|
|
return
|
2021-12-22 10:52:02 +00:00
|
|
|
}
|
|
|
|
defer multierr.AppendInvoke(&err, multierr.Close(f))
|
|
|
|
data, err := io.ReadAll(f)
|
|
|
|
if err != nil {
|
2022-03-15 13:53:24 +00:00
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
_, _ = writer.Write([]byte(err.Error()))
|
|
|
|
return
|
2021-12-22 10:52:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 13:53:24 +00:00
|
|
|
writer.Header().Set("Content-Type", "text/html")
|
2021-12-22 21:31:45 +00:00
|
|
|
var rendered []byte
|
|
|
|
if rendered, err = rendering.ToHTML(string(data), p.cfg.Rendering); err != nil {
|
2022-03-15 13:53:24 +00:00
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
_, _ = writer.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
} else if _, err = writer.Write(rendered); err != nil {
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
_, _ = writer.Write([]byte(err.Error()))
|
|
|
|
return
|
2021-12-22 10:52:02 +00:00
|
|
|
}
|
|
|
|
}
|