2019-04-19 21:56:39 +00:00
|
|
|
// Copyright © 2019 Peter Kurfer peter.kurfer@googlemail.com
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-04-20 00:28:27 +00:00
|
|
|
package rendering
|
2019-04-19 21:56:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/baez90/go-reveal-slides/internal/app/config"
|
2020-08-13 07:01:26 +00:00
|
|
|
"github.com/markbates/pkger"
|
2019-04-20 00:28:27 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-04-19 21:56:39 +00:00
|
|
|
"html/template"
|
2020-08-13 07:01:26 +00:00
|
|
|
"io/ioutil"
|
2019-04-19 21:56:39 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RevealRenderer interface {
|
|
|
|
http.Handler
|
|
|
|
init() error
|
|
|
|
}
|
|
|
|
|
2019-04-20 00:28:27 +00:00
|
|
|
func NewRevealRenderer(params *config.RevealParams) (renderer RevealRenderer, err error) {
|
2019-04-19 21:56:39 +00:00
|
|
|
renderer = &revealRenderer{
|
2019-04-20 00:28:27 +00:00
|
|
|
params: params,
|
2019-04-19 21:56:39 +00:00
|
|
|
}
|
|
|
|
err = renderer.init()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type revealRenderer struct {
|
|
|
|
template *template.Template
|
|
|
|
renderedTemplate string
|
|
|
|
params *config.RevealParams
|
|
|
|
}
|
|
|
|
|
|
|
|
func (renderer *revealRenderer) init() (err error) {
|
2020-08-13 07:01:26 +00:00
|
|
|
templateFile, err :=pkger.Open("/assets/template/reveal-markdown.tmpl")
|
2019-04-19 21:56:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-13 07:01:26 +00:00
|
|
|
templateString, err := ioutil.ReadAll(templateFile)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
renderer.template, err = template.New("index").Parse(string(templateString))
|
2019-04-19 21:56:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (renderer *revealRenderer) ServeHTTP(response http.ResponseWriter, request *http.Request) {
|
|
|
|
|
|
|
|
if renderer.template == nil {
|
2019-04-20 00:28:27 +00:00
|
|
|
writeErrorResponse(500, "rendering is not set - probably error during startup", response)
|
2019-04-19 21:56:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-20 00:28:27 +00:00
|
|
|
err := renderer.template.Execute(response, struct {
|
|
|
|
Reveal config.RevealParams
|
|
|
|
}{Reveal: *renderer.params})
|
2019-04-19 21:56:39 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2019-04-20 00:28:27 +00:00
|
|
|
writeErrorResponse(500, "Failed to render Markdown to rendering", response)
|
|
|
|
log.Errorf("Failed to render Markdown rendering: %v", err)
|
2019-04-19 21:56:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeErrorResponse(code int, msg string, response http.ResponseWriter) {
|
|
|
|
response.WriteHeader(code)
|
|
|
|
_, err := response.Write([]byte(msg))
|
|
|
|
log.Errorf("Failed to write error reponse: %v", err)
|
|
|
|
}
|