~repos /gromer
git clone https://pyrossh.dev/repos/gromer.git
gromer is a framework and cli to build multipage web apps in golang using htmx and alpinejs.
c1237fcb
—
Peter John 3 years ago
fix http
- gsx/gsx.go +1 -1
- gsx/gsx_test.go +3 -3
- gsx/parser.go +5 -5
- gsx/parser_test.go +3 -3
- http.go +14 -3
gsx/gsx.go
CHANGED
|
@@ -133,7 +133,7 @@ func Write(c *Context, w io.Writer, tags []*Tag) {
|
|
|
133
133
|
}
|
|
134
134
|
w.Write([]byte(`</head><body>`))
|
|
135
135
|
}
|
|
136
|
-
out :=
|
|
136
|
+
out := RenderString(tags)
|
|
137
137
|
w.Write([]byte(out))
|
|
138
138
|
if c.hx == nil {
|
|
139
139
|
w.Write([]byte(`</body></html>`))
|
gsx/gsx_test.go
CHANGED
|
@@ -69,7 +69,7 @@ func TestComponent(t *testing.T) {
|
|
|
69
69
|
</Todo>
|
|
70
70
|
<Todo />
|
|
71
71
|
`)
|
|
72
|
-
actual :=
|
|
72
|
+
actual := RenderString(nodes)
|
|
73
73
|
expected := trimLeft(`
|
|
74
74
|
<Todo>
|
|
75
75
|
<li id="todo-4" class="completed">
|
|
@@ -137,7 +137,7 @@ func TestMultipleComponent(t *testing.T) {
|
|
|
137
137
|
<Todo />
|
|
138
138
|
<TodoCount />
|
|
139
139
|
`)
|
|
140
|
-
actual :=
|
|
140
|
+
actual := RenderString(nodes)
|
|
141
141
|
expected := trimLeft(`
|
|
142
142
|
<Todo>
|
|
143
143
|
<li id="todo-4" class="completed">
|
|
@@ -210,7 +210,7 @@ func TestFor(t *testing.T) {
|
|
|
210
210
|
}
|
|
211
211
|
</ol>
|
|
212
212
|
`)
|
|
213
|
-
actual :=
|
|
213
|
+
actual := RenderString(nodes)
|
|
214
214
|
expected := trimLeft(`
|
|
215
215
|
<ul class="relative">
|
|
216
216
|
<li>
|
gsx/parser.go
CHANGED
|
@@ -134,15 +134,15 @@ func cloneTags(tags []*Tag) []*Tag {
|
|
|
134
134
|
return newTags
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
-
func
|
|
137
|
+
func RenderString(tags []*Tag) string {
|
|
138
138
|
s := ""
|
|
139
139
|
for _, t := range tags {
|
|
140
|
-
s +=
|
|
140
|
+
s += RenderTagString(t, "") + "\n"
|
|
141
141
|
}
|
|
142
142
|
return s
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
-
func
|
|
145
|
+
func RenderTagString(x *Tag, space string) string {
|
|
146
146
|
if x.Name == "" {
|
|
147
147
|
if x.Text != nil && x.Text.Str != nil {
|
|
148
148
|
return space + strings.ReplaceAll(*x.Text.Str, `"`, "")
|
|
@@ -154,7 +154,7 @@ func renderTagString(x *Tag, space string) string {
|
|
|
154
154
|
if x.Name == "fragment" {
|
|
155
155
|
s := ""
|
|
156
156
|
for _, c := range x.Children {
|
|
157
|
-
s +=
|
|
157
|
+
s += RenderTagString(c, space) + "\n"
|
|
158
158
|
}
|
|
159
159
|
return s
|
|
160
160
|
}
|
|
@@ -171,7 +171,7 @@ func renderTagString(x *Tag, space string) string {
|
|
|
171
171
|
}
|
|
172
172
|
if !x.SelfClosing {
|
|
173
173
|
for _, c := range x.Children {
|
|
174
|
-
s +=
|
|
174
|
+
s += RenderTagString(c, space+" ") + "\n"
|
|
175
175
|
}
|
|
176
176
|
s += space + "</" + x.Name + ">"
|
|
177
177
|
}
|
gsx/parser_test.go
CHANGED
|
@@ -9,7 +9,7 @@ import (
|
|
|
9
9
|
|
|
10
10
|
func TestParse(t *testing.T) {
|
|
11
11
|
r := require.New(t)
|
|
12
|
-
actual :=
|
|
12
|
+
actual := RenderString(parse("test", `
|
|
13
13
|
<ul id="todo-list" class="relative">
|
|
14
14
|
<Todo todo={v}>
|
|
15
15
|
<div>"Todo123"</div>
|
|
@@ -46,7 +46,7 @@ func TestParse(t *testing.T) {
|
|
|
46
46
|
|
|
47
47
|
func TestSelfClose(t *testing.T) {
|
|
48
48
|
r := require.New(t)
|
|
49
|
-
actual :=
|
|
49
|
+
actual := RenderString(parse("test", `
|
|
50
50
|
<Todo />
|
|
51
51
|
<TodoCount />
|
|
52
52
|
`))
|
|
@@ -59,7 +59,7 @@ func TestSelfClose(t *testing.T) {
|
|
|
59
59
|
|
|
60
60
|
func TestForLoop(t *testing.T) {
|
|
61
61
|
r := require.New(t)
|
|
62
|
-
actual :=
|
|
62
|
+
actual := RenderString(parse("test", `
|
|
63
63
|
<ul>
|
|
64
64
|
for k, v := range todos {
|
|
65
65
|
return (
|
http.go
CHANGED
|
@@ -196,7 +196,7 @@ func PerformRequest(route string, h interface{}, c *gsx.Context, w http.Response
|
|
|
196
196
|
w.Header().Set("Content-Type", "text/html")
|
|
197
197
|
// This has to be at end always
|
|
198
198
|
w.WriteHeader(responseStatus)
|
|
199
|
-
|
|
199
|
+
gsx.Write(c, w, response.([]*gsx.Tag))
|
|
200
200
|
}
|
|
201
201
|
|
|
202
202
|
func LogMiddleware(next http.Handler) http.Handler {
|
|
@@ -238,7 +238,18 @@ func CacheMiddleware(next http.Handler) http.Handler {
|
|
|
238
238
|
func StatusHandler(h interface{}) http.Handler {
|
|
239
239
|
return LogMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
240
240
|
ctx := context.WithValue(context.WithValue(r.Context(), "url", r.URL), "header", r.Header)
|
|
241
|
+
var hx *gsx.HX
|
|
242
|
+
if r.Header.Get("HX-Request") == "true" {
|
|
243
|
+
hx = &gsx.HX{
|
|
241
|
-
|
|
244
|
+
Boosted: r.Header.Get("HX-Boosted") == "true",
|
|
245
|
+
CurrentUrl: r.Header.Get("HX-Current-URL"),
|
|
246
|
+
Prompt: r.Header.Get("HX-Prompt"),
|
|
247
|
+
Target: r.Header.Get("HX-Target"),
|
|
248
|
+
TriggerName: r.Header.Get("HX-Trigger-Name"),
|
|
249
|
+
TriggerID: r.Header.Get("HX-Trigger"),
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
renderContext := gsx.NewContext(ctx, hx)
|
|
242
253
|
values := reflect.ValueOf(h).Call([]reflect.Value{reflect.ValueOf(renderContext)})
|
|
243
254
|
response := values[0].Interface()
|
|
244
255
|
responseStatus := values[1].Interface().(int)
|
|
@@ -251,7 +262,7 @@ func StatusHandler(h interface{}) http.Handler {
|
|
|
251
262
|
|
|
252
263
|
// This has to be at end always after headers are set
|
|
253
264
|
w.WriteHeader(responseStatus)
|
|
254
|
-
|
|
265
|
+
gsx.Write(renderContext, w, response.([]*gsx.Tag))
|
|
255
266
|
})).(http.Handler)
|
|
256
267
|
}
|
|
257
268
|
|