Things I did while unable to commit

- Log rotation
- Execution cancellation
- HTTP redirect, error functions
- Changed naming of document parsing/loading functions
This commit is contained in:
2024-12-10 20:37:40 -05:00
parent f112a2e564
commit bf668b0cf7
8 changed files with 101 additions and 26 deletions

View File

@@ -4,6 +4,7 @@ import "log"
import "fmt"
import "path"
import "io/fs"
import "errors"
import "strings"
import "strconv"
import "net/http"
@@ -11,11 +12,6 @@ import "path/filepath"
import "git.tebibyte.media/sashakoshka/step"
import "git.tebibyte.media/sashakoshka/goutil/container"
type ErrorData struct {
Status int
Message any
}
type DirectoryData struct {
Name string
Entries []fs.DirEntry
@@ -120,7 +116,7 @@ func (this *Handler) serveDirectory (
this.serveFile(res, req, pat)
return
}
document, err := this.Environment.Parse(this.DirectoryDocument)
document, err := this.Environment.Load(this.DirectoryDocument)
if err != nil {
this.serveError(res, req, http.StatusInternalServerError, err, false)
return
@@ -143,7 +139,7 @@ func (this *Handler) serveDocument (
name string,
) {
// parse
document, err := this.Environment.Parse(name)
document, err := this.Environment.Load(name)
if err != nil {
this.serveError(res, req, http.StatusInternalServerError, err, false)
return
@@ -177,8 +173,17 @@ func (this *Handler) serveDocument (
err = document.Execute(&recorder, step.ExecutionData {
Data: data,
})
if errors.Is(err, step.ErrExecutionCanceled) { err = nil }
var httpError Error
if errors.As(err, &httpError) {
this.serveError (
res, req,
httpError.Status, httpError.Message, false)
}
if err != nil {
this.serveError(res, req, http.StatusInternalServerError, err, false)
this.serveError (
res, req,
http.StatusInternalServerError, err, false)
return
}
@@ -209,13 +214,13 @@ func (this *Handler) serveError (
return
}
document, err := this.Environment.Parse(this.ErrorDocument)
document, err := this.Environment.Load(this.ErrorDocument)
if err != nil {
this.serveError(res, req, http.StatusInternalServerError, err, true)
return
}
err = document.Execute(res, step.ExecutionData {
Data: ErrorData {
Data: Error {
Status: status,
Message: message,
},

View File

@@ -1,9 +1,27 @@
package http
import "io"
import "fmt"
import "bytes"
import "net/http"
// Error represents an HTTP error. It is passed to error documents as data.
// If returned from a template, the server will show the appropriate error page.
type Error struct {
Status int
Message any
}
func (err Error) Error () string {
message := fmt.Sprint(err.Message)
text := http.StatusText(err.Status)
if message == "" {
return fmt.Sprintf("%d: %s", err.Status, text)
} else {
return fmt.Sprintf("%d: %s: %s", err.Status, text, message)
}
}
// HTTPData represents information about an ongoing HTTP request that is made
// available to templates as they are being executed.
type HTTPData struct {