detached 65ebb0e

static embed, html rendering and htmx

commit
65ebb0ebe304377e4572ec015da84931892ee1db
author
bnema · · 2023-09-01 18:34 UTC
parent
b1d2b4c
signature
unsigned
10 files · +180 -41
M cmd/main.go +68 -6
@@ -1,21 +1,83 @@
11 package main
22
33 import (
4+ "fmt"
5+ "html/template"
46 "net/http"
7+ "strings"
58
69 "github.com/labstack/echo/v4"
710 "gogs.bnema.dev/gordon-echo/internal/ui"
11+ "gogs.bnema.dev/gordon-echo/pkg/utils"
812 )
913
10-func main() {
11- e := echo.New()
14+const trailedAdminPath = "/admin/"
15+
16+// bindStaticAdminUI serves the static admin UI
17+func bindStaticAdminUI(e *echo.Echo) error {
18+ tmpl, err := template.New("index").ParseFS(ui.PublicFS, "index.html")
19+ if err != nil {
20+ return fmt.Errorf("failed to parse index.html: %w", err)
21+ }
22+
23+ renderer := &utils.Renderer{
24+ Template: tmpl,
25+ ParseError: err,
26+ }
27+
28+ // redirect to trailing slash to ensure that relative urls will still work properly
29+ e.GET(
30+ strings.TrimRight(trailedAdminPath, "/"),
31+ func(c echo.Context) error {
32+ return c.Redirect(http.StatusTemporaryRedirect, strings.TrimLeft(trailedAdminPath, "/"))
33+ },
34+ )
35+
36+ // Main handler for /admin/
37+ e.GET(trailedAdminPath, func(c echo.Context) error {
38+ data := map[string]interface{}{
39+ "website": map[string]interface{}{
40+ "title": "Hello, World!",
41+ },
42+ }
43+
44+ html, err := renderer.Render(data)
45+ if err != nil {
46+ return c.String(http.StatusInternalServerError, fmt.Sprintf("Failed to render template: %v", err))
47+ }
1248
13- e.GET("/", func(c echo.Context) error {
14- // render the internal/ui/templates/index.html template
15- return ui.Render(c, "index.html", nil)
49+ return c.HTML(http.StatusOK, html)
1650 })
51+ // serves static files from the /ui/dist directory
52+ // (similar to echo.StaticFS but with gzip middleware enabled)
53+ e.GET(
54+ trailedAdminPath+"*",
55+ echo.StaticDirectoryHandler(ui.PublicFS, false),
56+ )
57+ e.GET("/htmx", func(c echo.Context) error {
58+ fragment := c.Request().Header.Get("X-Fragment")
59+
60+ if fragment == "hello" {
61+ // Fetch and return the "hello" fragment
62+ content, err := utils.GetHTMLFragmentByID("hello")
63+ fmt.Println("X-Fragment:", c.Request().Header.Get("X-Fragment"))
64+ if err != nil {
65+ return c.String(http.StatusInternalServerError, err.Error())
66+ }
67+ return c.HTML(http.StatusOK, content)
68+ }
69+
70+ // Handle other fragments or default behavior
71+ return c.String(http.StatusBadRequest, "Invalid fragment")
72+ })
73+ return nil
74+}
75+
76+func main() {
77+ e := echo.New()
1778
18- e.GET("/public/*", echo.WrapHandler(http.FileServer(http.FS(ui.PublicFS))))
79+ // use func bindStaticAdminUI to serve the static admin UI
80+ bindStaticAdminUI(e)
1981
2082 e.Logger.Fatal(e.Start(":1323"))
2183
@@ -1,21 +1,83 @@
1 package main1 package main
2 2
3 import (3 import (
4+ "fmt"
5+ "html/template"
4 "net/http"6 "net/http"
7+ "strings"
5 8
6 "github.com/labstack/echo/v4"9 "github.com/labstack/echo/v4"
7 "gogs.bnema.dev/gordon-echo/internal/ui"10 "gogs.bnema.dev/gordon-echo/internal/ui"
11+ "gogs.bnema.dev/gordon-echo/pkg/utils"
8 )12 )
9 13
10-func main() {14+const trailedAdminPath = "/admin/"
11- e := echo.New()15+
16+// bindStaticAdminUI serves the static admin UI
17+func bindStaticAdminUI(e *echo.Echo) error {
18+ tmpl, err := template.New("index").ParseFS(ui.PublicFS, "index.html")
19+ if err != nil {
20+ return fmt.Errorf("failed to parse index.html: %w", err)
21+ }
22+
23+ renderer := &utils.Renderer{
24+ Template: tmpl,
25+ ParseError: err,
26+ }
27+
28+ // redirect to trailing slash to ensure that relative urls will still work properly
29+ e.GET(
30+ strings.TrimRight(trailedAdminPath, "/"),
31+ func(c echo.Context) error {
32+ return c.Redirect(http.StatusTemporaryRedirect, strings.TrimLeft(trailedAdminPath, "/"))
33+ },
34+ )
35+
36+ // Main handler for /admin/
37+ e.GET(trailedAdminPath, func(c echo.Context) error {
38+ data := map[string]interface{}{
39+ "website": map[string]interface{}{
40+ "title": "Hello, World!",
41+ },
42+ }
43+
44+ html, err := renderer.Render(data)
45+ if err != nil {
46+ return c.String(http.StatusInternalServerError, fmt.Sprintf("Failed to render template: %v", err))
47+ }
12 48
13- e.GET("/", func(c echo.Context) error {49+ return c.HTML(http.StatusOK, html)
14- // render the internal/ui/templates/index.html template
15- return ui.Render(c, "index.html", nil)
16 })50 })
51+ // serves static files from the /ui/dist directory
52+ // (similar to echo.StaticFS but with gzip middleware enabled)
53+ e.GET(
54+ trailedAdminPath+"*",
55+ echo.StaticDirectoryHandler(ui.PublicFS, false),
56+ )
57+ e.GET("/htmx", func(c echo.Context) error {
58+ fragment := c.Request().Header.Get("X-Fragment")
59+
60+ if fragment == "hello" {
61+ // Fetch and return the "hello" fragment
62+ content, err := utils.GetHTMLFragmentByID("hello")
63+ fmt.Println("X-Fragment:", c.Request().Header.Get("X-Fragment"))
64+ if err != nil {
65+ return c.String(http.StatusInternalServerError, err.Error())
66+ }
67+ return c.HTML(http.StatusOK, content)
68+ }
69+
70+ // Handle other fragments or default behavior
71+ return c.String(http.StatusBadRequest, "Invalid fragment")
72+ })
73+ return nil
74+}
75+
76+func main() {
77+ e := echo.New()
17 78
18- e.GET("/public/*", echo.WrapHandler(http.FileServer(http.FS(ui.PublicFS))))79+ // use func bindStaticAdminUI to serve the static admin UI
80+ bindStaticAdminUI(e)
19 81
20 e.Logger.Fatal(e.Start(":1323"))82 e.Logger.Fatal(e.Start(":1323"))
21 83
M go.mod +7 -0
@@ -5,6 +5,13 @@ go 1.21.0
55 require github.com/labstack/echo/v4 v4.11.1
66
77 require (
8+ github.com/PuerkitoBio/goquery v1.8.1 // indirect
9+ github.com/andybalholm/cascadia v1.3.1 // indirect
10+ github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
11+)
12+
13+require (
14+ github.com/labstack/echo v3.3.10+incompatible
815 github.com/labstack/gommon v0.4.0 // indirect
916 github.com/mattn/go-colorable v0.1.13 // indirect
1017 github.com/mattn/go-isatty v0.0.19 // indirect
@@ -5,6 +5,13 @@ go 1.21.0
5 require github.com/labstack/echo/v4 v4.11.15 require github.com/labstack/echo/v4 v4.11.1
6 6
7 require (7 require (
8+ github.com/PuerkitoBio/goquery v1.8.1 // indirect
9+ github.com/andybalholm/cascadia v1.3.1 // indirect
10+ github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
11+)
12+
13+require (
14+ github.com/labstack/echo v3.3.10+incompatible
8 github.com/labstack/gommon v0.4.0 // indirect15 github.com/labstack/gommon v0.4.0 // indirect
9 github.com/mattn/go-colorable v0.1.13 // indirect16 github.com/mattn/go-colorable v0.1.13 // indirect
10 github.com/mattn/go-isatty v0.0.19 // indirect17 github.com/mattn/go-isatty v0.0.19 // indirect
M go.sum +38 -0
@@ -1,5 +1,13 @@
1+github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
2+github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
3+github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
4+github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
15 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
26 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
8+github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
9+github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
10+github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
311 github.com/labstack/echo/v4 v4.11.1 h1:dEpLU2FLg4UVmvCGPuk/APjlH6GDpbEPti61srUUUs4=
412 github.com/labstack/echo/v4 v4.11.1/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ=
513 github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
@@ -19,19 +27,49 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
1927 github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
2028 github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
2129 github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
30+github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
31+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
32+golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
2233 golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
2334 golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
35+golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
36+golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
37+golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
38+golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
39+golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
40+golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
2441 golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
2542 golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
43+golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
44+golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
45+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
46+golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
47+golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
48+golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2649 golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2750 golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2851 golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
52+golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
53+golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2954 golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
55+golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3056 golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3157 golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
3258 golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
59+golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
60+golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
61+golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
62+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
63+golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
64+golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
65+golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
66+golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
3367 golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
3468 golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
69+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
70+golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
71+golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
72+golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
3573 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3674 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3775 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
@@ -1,5 +1,13 @@
1+github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
2+github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
3+github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
4+github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
1 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=5 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=6 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
8+github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
9+github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
10+github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
3 github.com/labstack/echo/v4 v4.11.1 h1:dEpLU2FLg4UVmvCGPuk/APjlH6GDpbEPti61srUUUs4=11 github.com/labstack/echo/v4 v4.11.1 h1:dEpLU2FLg4UVmvCGPuk/APjlH6GDpbEPti61srUUUs4=
4 github.com/labstack/echo/v4 v4.11.1/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ=12 github.com/labstack/echo/v4 v4.11.1/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ=
5 github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=13 github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
@@ -19,19 +27,49 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
19 github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=27 github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
20 github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=28 github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
21 github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=29 github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
30+github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
31+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
32+golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
22 golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=33 golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
23 golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=34 golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
35+golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
36+golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
37+golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
38+golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
39+golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
40+golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
24 golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=41 golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
25 golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=42 golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
43+golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
44+golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
45+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
46+golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
47+golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
48+golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
26 golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=49 golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
27 golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=50 golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
28 golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=51 golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
52+golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
53+golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
29 golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=54 golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
55+golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
30 golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=56 golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
31 golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=57 golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
32 golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=58 golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
59+golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
60+golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
61+golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
62+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
63+golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
64+golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
65+golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
66+golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
33 golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=67 golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
34 golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=68 golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
69+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
70+golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
71+golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
72+golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
35 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=73 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
36 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=74 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
37 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=75 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
M internal/ui/embed.go +3 -9
@@ -4,20 +4,14 @@ import (
44 "embed"
55
66 "github.com/labstack/echo/v4"
7- "gogs.bnema.dev/gordon-echo/pkg/utils"
87 )
98
10-// go embed all:public
9+//go:embed public/*
1110 var public embed.FS
1211
13-// go embed all:templates
12+//go:embed templates/*
1413 var templates embed.FS
1514
1615 var PublicFS = echo.MustSubFS(public, "public")
17-var TemplateFS = echo.MustSubFS(templates, "templates")
1816
19-// Render renders a template with the specified data and writes to the response.
20-func Render(c echo.Context, name string, data any) {
21- r := &utils.Renderer{}
22- return r.Render(c, name, data)
23-}
17+var TemplateFS = echo.MustSubFS(templates, "templates")
@@ -4,20 +4,14 @@ import (
4 "embed"4 "embed"
5 5
6 "github.com/labstack/echo/v4"6 "github.com/labstack/echo/v4"
7- "gogs.bnema.dev/gordon-echo/pkg/utils"
8 )7 )
9 8
10-// go embed all:public9+//go:embed public/*
11 var public embed.FS10 var public embed.FS
12 11
13-// go embed all:templates12+//go:embed templates/*
14 var templates embed.FS13 var templates embed.FS
15 14
16 var PublicFS = echo.MustSubFS(public, "public")15 var PublicFS = echo.MustSubFS(public, "public")
17-var TemplateFS = echo.MustSubFS(templates, "templates")
18 16
19-// Render renders a template with the specified data and writes to the response.17+var TemplateFS = echo.MustSubFS(templates, "templates")
20-func Render(c echo.Context, name string, data any) {
21- r := &utils.Renderer{}
22- return r.Render(c, name, data)
23-}
A internal/ui/public/index.html +18 -0
@@ -0,0 +1,18 @@
1+<!DOCTYPE html>
2+<html lang="en">
3+
4+<head>
5+ <meta charset="UTF-8">
6+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7+ <title>{{.website.title}}</title>
8+ <!-- HTMX min js -->
9+ <script src="libs/htmx.min.js"></script>
10+ <!-- UnoCSS -->
11+ <script src="https://cdn.jsdelivr.net/npm/@unocss/runtime"></script>
12+
13+</head>
14+
15+<body id="app" hx-get="/htmx" hx-headers='{"X-Fragment": "hello"}' hx-trigger="load"></body>
16+
17+
18+</html>⏎̸
@@ -0,0 +1,18 @@
1+<!DOCTYPE html>
2+<html lang="en">
3+
4+<head>
5+ <meta charset="UTF-8">
6+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7+ <title>{{.website.title}}</title>
8+ <!-- HTMX min js -->
9+ <script src="libs/htmx.min.js"></script>
10+ <!-- UnoCSS -->
11+ <script src="https://cdn.jsdelivr.net/npm/@unocss/runtime"></script>
12+
13+</head>
14+
15+<body id="app" hx-get="/htmx" hx-headers='{"X-Fragment": "hello"}' hx-trigger="load"></body>
16+
17+
18+</html>⏎̸
R internal/ui/public/libs/htmx.min.js.js → internal/ui/public/libs/htmx.min.js +0 -0 renamed, 100% similar

renamed, 100% similar

A internal/ui/templates/components.html +6 -0
@@ -0,0 +1,6 @@
1+
2+
3+
4+<div id ="hello">
5+ <h1 class="text-5xl fw100 animate-bounce-alt animate-count-infinite animate-duration-1s">Hello World from components.html</h1>
6+</div>⏎̸
@@ -0,0 +1,6 @@
1+
2+
3+
4+<div id ="hello">
5+ <h1 class="text-5xl fw100 animate-bounce-alt animate-count-infinite animate-duration-1s">Hello World from components.html</h1>
6+</div>⏎̸
D internal/ui/templates/index.html +0 -20
@@ -1,20 +0,0 @@
1-<!DOCTYPE html>
2-<html lang="en">
3-
4-<head>
5- <meta charset="UTF-8">
6- <meta name="viewport" content="width=device-width, initial-scale=1.0">
7- <title>{{website.title}}</title>
8- <!-- HTMX min js -->
9- <script src="./libs/htmx.min.js"></script>
10- <!-- UnoCSS -->
11- <link rel="stylesheet" href="./libs/uno.css">
12-
13-</head>
14-
15-<body id="app">
16- <!-- a quick hello world customized by uno css -->
17- <h1 class="text-center text-4xl text-blue-500">Hello World</h1>
18-</body>
19-
20-</html>⏎̸
@@ -1,20 +0,0 @@
1-<!DOCTYPE html>
2-<html lang="en">
3-
4-<head>
5- <meta charset="UTF-8">
6- <meta name="viewport" content="width=device-width, initial-scale=1.0">
7- <title>{{website.title}}</title>
8- <!-- HTMX min js -->
9- <script src="./libs/htmx.min.js"></script>
10- <!-- UnoCSS -->
11- <link rel="stylesheet" href="./libs/uno.css">
12-
13-</head>
14-
15-<body id="app">
16- <!-- a quick hello world customized by uno css -->
17- <h1 class="text-center text-4xl text-blue-500">Hello World</h1>
18-</body>
19-
20-</html>⏎̸
A pkg/utils/fragments.go +34 -0
@@ -0,0 +1,34 @@
1+package utils
2+
3+import (
4+ "fmt"
5+
6+ "github.com/PuerkitoBio/goquery"
7+ "gogs.bnema.dev/gordon-echo/internal/ui"
8+)
9+
10+// GetHTMLFragmentByID returns the HTML fragment with the specified id
11+func GetHTMLFragmentByID(id string) (string, error) {
12+ data, err := ui.TemplateFS.Open("components.html")
13+ if err != nil {
14+ return "", fmt.Errorf("failed to open components.html: %w", err)
15+ }
16+ defer data.Close()
17+
18+ doc, err := goquery.NewDocumentFromReader(data)
19+ if err != nil {
20+ return "", fmt.Errorf("failed to parse HTML document: %w", err)
21+ }
22+
23+ // Find the div with the specified id
24+ fragmentContent, err := doc.Find("#" + id).Html()
25+ if err != nil {
26+ return "", fmt.Errorf("failed to extract HTML for id %s: %w", id, err)
27+ }
28+
29+ if fragmentContent == "" {
30+ return "", fmt.Errorf("element with id %s not found", id)
31+ }
32+
33+ return fragmentContent, nil
34+}
@@ -0,0 +1,34 @@
1+package utils
2+
3+import (
4+ "fmt"
5+
6+ "github.com/PuerkitoBio/goquery"
7+ "gogs.bnema.dev/gordon-echo/internal/ui"
8+)
9+
10+// GetHTMLFragmentByID returns the HTML fragment with the specified id
11+func GetHTMLFragmentByID(id string) (string, error) {
12+ data, err := ui.TemplateFS.Open("components.html")
13+ if err != nil {
14+ return "", fmt.Errorf("failed to open components.html: %w", err)
15+ }
16+ defer data.Close()
17+
18+ doc, err := goquery.NewDocumentFromReader(data)
19+ if err != nil {
20+ return "", fmt.Errorf("failed to parse HTML document: %w", err)
21+ }
22+
23+ // Find the div with the specified id
24+ fragmentContent, err := doc.Find("#" + id).Html()
25+ if err != nil {
26+ return "", fmt.Errorf("failed to extract HTML for id %s: %w", id, err)
27+ }
28+
29+ if fragmentContent == "" {
30+ return "", fmt.Errorf("element with id %s not found", id)
31+ }
32+
33+ return fragmentContent, nil
34+}
M pkg/utils/render.go +6 -6
@@ -7,24 +7,24 @@ import (
77 )
88
99 type Renderer struct {
10- template *template.Template
11- parseError error
10+ Template *template.Template
11+ ParseError error
1212 }
1313
1414 // Render executes the template with the specified data as the dot object
1515 // and returns the result as plain string.
1616 func (r *Renderer) Render(data any) (string, error) {
17- if r.parseError != nil {
18- return "", r.parseError
17+ if r.ParseError != nil {
18+ return "", r.ParseError
1919 }
2020
21- if r.template == nil {
21+ if r.Template == nil {
2222 return "", errors.New("invalid or nil template")
2323 }
2424
2525 buf := new(bytes.Buffer)
2626
27- if err := r.template.Execute(buf, data); err != nil {
27+ if err := r.Template.ExecuteTemplate(buf, "index", data); err {
2828 return "", err
2929 }
3030
@@ -7,24 +7,24 @@ import (
7 )7 )
8 8
9 type Renderer struct {9 type Renderer struct {
10- template *template.Template10+ Template *template.Template
11- parseError error11+ ParseError error
12 }12 }
13 13
14 // Render executes the template with the specified data as the dot object14 // Render executes the template with the specified data as the dot object
15 // and returns the result as plain string.15 // and returns the result as plain string.
16 func (r *Renderer) Render(data any) (string, error) {16 func (r *Renderer) Render(data any) (string, error) {
17- if r.parseError != nil {17+ if r.ParseError != nil {
18- return "", r.parseError18+ return "", r.ParseError
19 }19 }
20 20
21- if r.template == nil {21+ if r.Template == nil {
22 return "", errors.New("invalid or nil template")22 return "", errors.New("invalid or nil template")
23 }23 }
24 24
25 buf := new(bytes.Buffer)25 buf := new(bytes.Buffer)
26 26
27- if err := r.template.Execute(buf, data); err != nil {27+ if err := r.Template.ExecuteTemplate(buf, "index", data); err {
28 return "", err28 return "", err
29 }29 }
30 30

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close