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

@@ -1,9 +1,10 @@
package http
import "net/http"
import "net/url"
import "net/http"
import "html/template"
import "git.tebibyte.media/sashakoshka/step"
import shttp "git.tebibyte.media/sashakoshka/step/http"
var _ step.FuncProvider = new(Provider)
@@ -23,6 +24,8 @@ func (this *Provider) FuncMap () template.FuncMap {
"statusText": http.StatusText,
"parseQuery": funcParseQuery,
"parseForm": funcParseForm,
"error": funcError,
"redirect": funcRedirect,
}
}
@@ -35,8 +38,22 @@ func funcParseQuery (query string) url.Values {
return values
}
func funcError (status int, message any) (string, error) {
return "", shttp.Error {
Status: status,
Message: message,
}
}
func funcRedirect (res shttp.WrappedResponseWriter, status int, pat string) (string, error) {
// TODO remove parameters
res.Header.Add("Location", pat)
res.WriteHeader(status)
return "", step.ErrExecutionCanceled
}
func funcParseForm (req *http.Request) url.Values {
// FIXME there is already a parse form method lol this can be removed
err := req.ParseForm()
if err != nil { return nil }
return req.Form

View File

@@ -25,6 +25,9 @@ func (this *Provider) FuncMapFor (document *step.Document) template.FuncMap {
}
return template.FuncMap {
"panic": stat.funcPanic,
"cancel": stat.funcCancel,
"create": stat.funcCreate,
"createHTML": stat.funcCreateHTML,
"execute": stat.funcExecute,
"include": stat.funcInclude,
"includeHTML": stat.funcIncludeHTML,
@@ -43,10 +46,23 @@ func (this *state) funcPanic (message any) (string, error) {
}
}
func (this *state) funcCancel () (string, error) {
return "", step.ErrExecutionCanceled
}
func (this *state) funcCreate (name string, arguments ...any) (string, error) {
return this.funcInclude(name, arguments)
}
func (this *state) funcCreateHTML (name string, arguments ...any) (template.HTML, error) {
return this.funcIncludeHTML(name, arguments)
}
func (this *state) funcExecute (name string, data any) (step.ExecutionResult, error) {
name, err := this.document.Rel(name)
if err != nil { return step.ExecutionResult { }, err }
document, err := this.document.Environment().Parse(name)
document, err := this.document.Environment().LoadRelative (
name, this.document)
if err != nil { return step.ExecutionResult { }, err }
builder := strings.Builder { }
err = document.Execute(&builder, step.ExecutionData { Data: data })