xonecas.io
5011
Sp

scrap-paper / commit 85f1a8b

feature: adds htmx and tailwind

Sean2025-03-04 85f1a8b70881a9da7b9efa702e70b93dca82bd83 parent ea6ee55

8 files changed

README.md+26−0
@@ -0,0 +1,26 @@
1+# Scrap paper
2+
3+## A simple notetaker made with encore and htmx
4+
5+### Overview:
6+
7+Folder structure is pretty standard for an encore app:
8+
9+```
10+root (encore)
11+├── lib/ # A shared utility internal library
12+└── scrap-paper/ # The proof of concept and experimental area
13+```
14+
15+### Dependencies:
16+
17+For now it is a small list and we strive to keep it small.
18+
19+- Frameworks:
20+
21+ - encore.dev -> Backend framework
22+ - htmx.org -> Frontend framework(?)
23+
24+- Packages:
25+
26+ - https://pkg.go.dev/github.com/kr/pretty -> a handy pretty print for Go structs
assets/favicon.svg+1−0
@@ -0,0 +1 @@
1+<?xml version="1.0" ?><svg height="24" version="1.1" width="24" xmlns="http://www.w3.org/2000/svg" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><g transform="translate(0 -1028.4)"><path d="m5 1030.4c-1.1046 0-2 0.9-2 2v8 4 6c0 1.1 0.8954 2 2 2h14c1.105 0 2-0.9 2-2v-6-4-4-4c0-1.1-0.895-2-2-2h-4-10z" fill="#bdc3c7"/><g fill="#95a5a6"><path d="m6 1028.4c-0.5523 0-1 0.4-1 1v3c0 0.5 0.4477 1 1 1s1-0.5 1-1 …
go.mod+6−0
@@ -3,3 +3,9 @@ module encore.app
33 go 1.23.4
44
55 require encore.dev v1.46.1
6+
7+require (
8+ github.com/kr/pretty v0.3.1 // indirect
9+ github.com/kr/text v0.2.0 // indirect
10+ github.com/rogpeppe/go-internal v1.9.0 // indirect
11+)
go.sum+8−0
@@ -1,2 +1,10 @@
11 encore.dev v1.46.1 h1:IGUpqPm600xAiJqMVcnaNiWya14yAH5imFwzGnFReaA=
22 encore.dev v1.46.1/go.mod h1:XdWK6bKKAVzutmOKpC5qzalDQJLNfRCF/YCgA7OUZ3E=
3+github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
4+github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
5+github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
6+github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
7+github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
8+github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
9+github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
10+github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
lib/pretty_print.go+11−0
@@ -0,0 +1,11 @@
1+package lib
2+
3+import (
4+ "fmt"
5+
6+ "github.com/kr/pretty"
7+)
8+
9+func PrettyPrint(v interface{}) {
10+ fmt.Printf("%# v\n", pretty.Formatter(v))
11+}
scrap-paper/index.go+78−0
@@ -0,0 +1,78 @@
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 IndexTemplateData struct {
19+ Title string
20+ Env string
21+}
22+
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+
33+//
34+// Warning: This is a fallback handler, being missused as a catch-all handler.
35+// It is used to handle requests that don't match any other route.
36+// In our case it means it renders the index page.
37+//
38+
39+//encore:api public raw path=/!fallback
40+func Index(w http.ResponseWriter, r *http.Request) {
41+ req := encore.CurrentRequest()
42+ env := encore.Meta().Environment.Name
43+ title := "📝 Scrap Paper - Home"
44+
45+ if env != "prod" {
46+ title = fmt.Sprintf("%s [%s]", title, env)
47+ }
48+
49+ if req.Path == "/" {
50+ html, err := IndexHtml(IndexTemplateData{
51+ Title: title,
52+ Env: env,
53+ })
54+ if err != nil {
55+ http.Error(w, err.Error(), http.StatusInternalServerError)
56+ return // TODO: Return an error page
57+ }
58+ w.Write([]byte(html))
59+
60+ return // DONE
61+ }
62+
63+ // Since we are abusing fallback handler to mount a page on "/"
64+ // we need to return a 404 error if the path is not "/"
65+ // TODO: Return an error page
66+ http.NotFound(w, r)
67+}
68+
69+//
70+// Handle static assets
71+//
72+
73+//encore:api public raw path=/assets/:filename
74+func Assets(w http.ResponseWriter, r *http.Request) {
75+ // This path is relative to the root of the project
76+ path := "." + encore.CurrentRequest().Path
77+ http.ServeFile(w, r, path)
78+}
scrap-paper/index.html+17−2
@@ -1,5 +1,20 @@
1+<!DOCTYPE html>
12 <html>
2- <body>
3- <h1>Hello World</h1>
3+ <head>
4+ <meta charset="UTF-8">
5+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
7+ <link rel="icon" href="assets/favicon.svg" type="image/svg+xml">
8+ <script src="https://unpkg.com/@tailwindcss/browser@4"></script>
9+
10+ <title>{{.Title}}</title>
11+ </head>
12+ <body class="font-sans bg-slate-50 text-slate-900 dark:bg-slate-900 dark:text-slate-50">
13+
14+ {{ if (eq .Env "prod") }}
15+ <script src="https://unpkg.com/htmx.org@2.0.4" integrity="sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" crossorigin="anonymous"></script>
16+ {{ else }}
17+ <script src="https://unpkg.com/htmx.org@2.0.4/dist/htmx.js" integrity="sha384-oeUn82QNXPuVkGCkcrInrS1twIxKhkZiFfr2TdiuObZ3n3yIeMiqcRzkIcguaof1" crossorigin="anonymous"></script>
18+ {{ end }}
419 </body>
520 </html>
scrap-paper/service.go+0−50
@@ -1,50 +0,0 @@
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-}