Updated to be correct port.
This commit is contained in:
+13
-27
@@ -2,38 +2,24 @@ package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func userIDFromContext(ctx context.Context) (string, bool) {
|
||||
id, ok := ctx.Value(userIDKey).(string)
|
||||
return id, ok
|
||||
}
|
||||
|
||||
// TODO: Not sure this is needed right now
|
||||
// Renders a full page by combining the base template with a page template
|
||||
// Parsed together so the page can define blocks needed for base template
|
||||
func render(w http.ResponseWriter, _ *http.Request, page string, data any) {
|
||||
files := []string{
|
||||
filepath.Join("internal", "templates", "layouts", "base.html"),
|
||||
filepath.Join("internal", "templates", "pages", page+".html"),
|
||||
func userIDFromContext(ctx context.Context) (int64, bool) {
|
||||
val := ctx.Value(userIDKey)
|
||||
if val == nil {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(files...)
|
||||
strID, ok := val.(string)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
id, err := strconv.ParseInt(strID, 10, 64)
|
||||
if err != nil {
|
||||
http.Error(w, "template error: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return 0, false
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
|
||||
if err := tmpl.ExecuteTemplate(w, "base", data); err != nil {
|
||||
http.Error(w, "render error: "+err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func view(w http.ResponseWriter, r *http.Request, page string, data any) {
|
||||
render(w, r, page, data)
|
||||
return id, true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user