cmd/stepd: Buffer templates
This commit is contained in:
parent
c85bd09ac5
commit
60f1b62965
@ -5,6 +5,7 @@ import "fmt"
|
||||
import "path"
|
||||
import "io/fs"
|
||||
import "strings"
|
||||
import "strconv"
|
||||
import "net/http"
|
||||
import "path/filepath"
|
||||
import "git.tebibyte.media/sashakoshka/step"
|
||||
@ -75,22 +76,43 @@ func (this *handler) serveFile (res http.ResponseWriter, req *http.Request, file
|
||||
return
|
||||
}
|
||||
|
||||
// parse and execute
|
||||
// parse
|
||||
document, err := this.environment.Parse(name)
|
||||
if err != nil {
|
||||
this.serveError(res, req, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
if contentType, ok := document.FrontMatter["content-type"]; ok {
|
||||
res.Header().Set("Content-Type", contentType)
|
||||
|
||||
// set up HTTP response recorder
|
||||
recorder := step.HTTPResponseRecorder {
|
||||
Status: http.StatusOK,
|
||||
Head: res.Header(),
|
||||
}
|
||||
if contentType, ok := document.FrontMatter["content-type"]; ok {
|
||||
recorder.Header().Set("Content-Type", contentType)
|
||||
}
|
||||
if status, ok := document.FrontMatter["status"]; ok {
|
||||
if status, err := strconv.Atoi(status); err == nil {
|
||||
recorder.Status = status
|
||||
}
|
||||
}
|
||||
|
||||
// execute document
|
||||
data := step.HTTPData { }
|
||||
data.Res.Header = make(http.Header)
|
||||
data.Res.WriteHeader = res.WriteHeader
|
||||
data.Res.WriteHeader = recorder.WriteHeader
|
||||
data.Res.Reset = recorder.Reset
|
||||
data.Req = req
|
||||
err = document.Execute(res, step.ExecutionData {
|
||||
err = document.Execute(&recorder, step.ExecutionData {
|
||||
Data: data,
|
||||
})
|
||||
if err != nil {
|
||||
this.serveError(res, req, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
// play back recorded response
|
||||
err = recorder.Play(res)
|
||||
if err != nil {
|
||||
this.logErr(name, err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user