preparations for styling: refactor template rendering #1
Loading…
Reference in a new issue
No description provided.
Delete branch "styling"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
comments and review here, will resolve merge conflicts tomorrow
https://www.alexedwards.net/blog/serving-static-sites-with-go
This link probably helps understanding which design I oriented at (one base template with specific blocks replaced per page)
Nice restructuring.
@ -22,0 +23,4 @@
// - name is the name of the template file inside templates/, without .html suffix.
// - data is passed through to the template
func (b *BaseHandler) serveTemplate(w http.ResponseWriter, name string, data any) {
full_name := "templates/" + name + ".html"
Here we should consider checking whether the name is restricted to something like
[a-zA-Z0-9]
to prevent directory traversal. Possibly it is sufficient to forbid/
and/or.
This is not longer relevant, since all available templates are listed in one place inside our code and we do not parse filenames from anywhere else.
@ -22,0 +27,4 @@
// check that template exists
info, err := os.Stat(full_name)
if (err != nil && os.IsNotExist(err)) || info.IsDir() {
errMsg := fmt.Sprintf("Template %s nicht gefunden", full_name)
This could be an actual error using
fmt.Errorf
.Furthermore I began logging in English. We should make that consistent.
@ -22,0 +29,4 @@
if (err != nil && os.IsNotExist(err)) || info.IsDir() {
errMsg := fmt.Sprintf("Template %s nicht gefunden", full_name)
log.Println(errMsg)
w.WriteHeader(http.StatusNotFound)
I think, that StatusNotFound is for user supplied references (e.g. a course or a room id), but this should be an internal server error.
@ -22,0 +34,4 @@
return
}
tmpl, err := baseTemplate.ParseFiles(full_name)
Using this construction each template is parsed again and again, while it would be sufficient to parse them all on startup and later just execute them.
Fixed (and spent too much time implementing this ^^)
@ -22,0 +36,4 @@
tmpl, err := baseTemplate.ParseFiles(full_name)
if err != nil {
errMsg := fmt.Sprintf("Template %s konnte nicht geparst werden : %s", full_name, err.Error())
This could be an actual error using
fmt.Errorf
.Furthermore I began logging in English. We should make that consistent.
Furthermore error wrapping is something cool I just learnt.
Error wrapping looks nice. I started doing that.
In general it seems best to wrap errors which occur during runtime from the beginning of a http request handler, which then logs the error and returns an error page to the user. (I added an inline TODO about that)
Errors on startup however can just abort the whole program, like I implemented in
main.go
@ -22,0 +45,4 @@
// TODO: cache templates in parsed state, but not executed
err = template.Must(tmpl.Clone()).Execute(w, data)
if err != nil {
errMsg := fmt.Sprintf("Template %s konnte nicht ausgeführt werden : %s", full_name, err.Error())
This could be an actual error using
fmt.Errorf
.Furthermore I began logging in English. We should make that consistent.
Furthermore error wrapping is something cool I just learnt.
If you want to work on in go code, feel free to just merge this. I'll work on the actual template styling now.
0a4245b094
to9357ab1520