@@ -0,0 +1,50 @@
1+package scrap_paper
2+
3+// We are building a service that uses htmx to return html from a url
4+import (
5+ "bytes"
6+ _ "embed"
7+ "fmt"
8+ "net/http"
9+
10+ "html/template"
11+
12+ "encore.dev"
13+)
14+
15+//go:embed index.html
16+var indexRawTemplate string
17+
18+type HelloWorldTemplateData struct {
19+ Name string
20+}
21+
22+func HelloWorldHtml(data HelloWorldTemplateData) (string, error) {
23+ tmpl := template.Must(template.New("hello_world").Parse(indexRawTemplate))
24+
25+ var buf bytes.Buffer
26+ if err := tmpl.Execute(&buf, data); err != nil {
27+ return "", err
28+ }
29+ return buf.String(), nil
30+}
31+
32+//
33+// Warning: This is a fallback handler, being missused as a catch-all handler.
34+// It is used to handle requests that don't match any other route.
35+// In our case it means it renders the index page.
36+//
37+
38+//encore:api public raw path=/!fallback
39+func Index(w http.ResponseWriter, r *http.Request) {
40+ req := encore.CurrentRequest()
41+
42+ fmt.Println(req)
43+
44+ html, err := HelloWorldHtml(HelloWorldTemplateData{Name: "World"})
45+ if err != nil {
46+ http.Error(w, err.Error(), http.StatusInternalServerError)
47+ return
48+ }
49+ w.Write([]byte(html))
50+}