| 58 | var _ http.Handler = (*Handler)(nil) |
| 59 | |
| 60 | func (h *Handler) render(w http.ResponseWriter, r *http.Request) { |
| 61 | // Read the body |
| 62 | body, err := io.ReadAll(r.Body) |
| 63 | if err != nil { |
| 64 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 65 | return |
| 66 | } |
| 67 | // Load the props |
| 68 | var props map[string]interface{} |
| 69 | if err := json.Unmarshal(body, &props); err != nil { |
| 70 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 71 | return |
| 72 | } |
| 73 | script, err := fs.ReadFile(h.fsys, "bud/view/_ssr.js") |
| 74 | if err != nil { |
| 75 | if errors.Is(err, fs.ErrNotExist) { |
| 76 | http.Error(w, err.Error(), http.StatusNotFound) |
| 77 | return |
| 78 | } |
| 79 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 80 | return |
| 81 | } |
| 82 | route := "/" + r.URL.Query().Get("route") |
| 83 | expr := fmt.Sprintf(`%s; bud.render(%q, %s)`, script, route, body) |
| 84 | result, err := h.vm.Eval("_ssr.js", expr) |
| 85 | if err != nil { |
| 86 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 87 | return |
| 88 | } |
| 89 | w.Write([]byte(result)) |
| 90 | } |
| 91 | |
| 92 | func (h *Handler) open(w http.ResponseWriter, r *http.Request) { |
| 93 | path := r.URL.Query().Get("path") |