@@ -1,37 +1,24 @@
11 package scrap_paper
22
3-// We are building a service that uses htmx to return html from a url
43 import (
5- "bytes"
64 _ "embed"
75 "fmt"
86 "net/http"
97
10- "html/template"
11-
8+ "encore.app/lib"
129 "encore.dev"
1310 )
1411
1512 //go:embed index.html
16-var indexRawTemplate string
13+var IndexRawTemplate string
1714
1815 type IndexTemplateData struct {
1916 Title string
2017 Env string
2118 }
2219
23-func IndexHtml(data IndexTemplateData) (string, error) {
24- tmpl := template.Must(template.New("index").Parse(indexRawTemplate))
25-
26- var buf bytes.Buffer
27- if err := tmpl.Execute(&buf, data); err != nil {
28- return "", err
29- }
30- return buf.String(), nil
31-}
32-
3320 //encore:api public raw path=/!fallback
34-func Index(w http.ResponseWriter, r *http.Request) {
21+func (svc *ScrapPaperService) Index(w http.ResponseWriter, r *http.Request) {
3522 req := encore.CurrentRequest()
3623 env := encore.Meta().Environment.Name
3724 title := "Scrap Paper - Home"
@@ -41,7 +28,7 @@ func Index(w http.ResponseWriter, r *http.Request) {
4128 }
4229
4330 if req.Path == "/" {
44- html, err := IndexHtml(IndexTemplateData{
31+ html, err := lib.RenderTemplate(IndexRawTemplate, IndexTemplateData{
4532 Title: title,
4633 Env: env,
4734 })
@@ -55,17 +42,5 @@ func Index(w http.ResponseWriter, r *http.Request) {
5542
5643 // Since we are abusing fallback handler to mount a page on "/"
5744 // we need to return a 404 error if the path is not "/"
58- // TODO: Return an error page
5945 http.NotFound(w, r)
6046 }
61-
62-//
63-// Handle static assets
64-//
65-
66-//encore:api public raw path=/assets/:filename
67-func Assets(w http.ResponseWriter, r *http.Request) {
68- // This path is relative to the root of the project
69- path := "." + encore.CurrentRequest().Path
70- http.ServeFile(w, r, path)
71-}