~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.
add more stuff to context
- cmd/main.go +26 -5
- context.go +5 -4
cmd/main.go
CHANGED
|
@@ -72,7 +72,7 @@ func rewritePkg(pkg string) string {
|
|
|
72
72
|
arr := strings.Split(pkg, "/")
|
|
73
73
|
lastItem := arr[len(arr)-1]
|
|
74
74
|
if strings.Contains(lastItem, "_") {
|
|
75
|
-
return arr[len(arr)-2] +
|
|
75
|
+
return arr[len(arr)-2] + lastItem
|
|
76
76
|
}
|
|
77
77
|
return lastItem
|
|
78
78
|
}
|
|
@@ -122,7 +122,7 @@ func main() {
|
|
|
122
122
|
ctx.Set("moduleName", moduleName)
|
|
123
123
|
ctx.Set("allPkgs", allPkgs)
|
|
124
124
|
ctx.Set("routes", routes)
|
|
125
|
-
s, err := velvet.Render(`//
|
|
125
|
+
s, err := velvet.Render(`// Code generated by Wapp Cmd Go. DO NOT EDIT.
|
|
126
126
|
package main
|
|
127
127
|
|
|
128
128
|
import (
|
|
@@ -134,6 +134,7 @@ import (
|
|
|
134
134
|
|
|
135
135
|
"github.com/apex/gateway/v2"
|
|
136
136
|
"github.com/gorilla/mux"
|
|
137
|
+
"github.com/pyros2097/wapp"
|
|
137
138
|
|
|
138
139
|
"{{ moduleName }}/context"
|
|
139
140
|
{{#each allPkgs }}"{{ moduleName }}/pages{{ @key }}"
|
|
@@ -147,7 +148,7 @@ func main() {
|
|
|
147
148
|
isLambda := os.Getenv("_LAMBDA_SERVER_PORT") != ""
|
|
148
149
|
r := mux.NewRouter()
|
|
149
150
|
r.PathPrefix("/assets/").Handler(http.FileServer(http.FS(assetsFS)))
|
|
150
|
-
{{#each routes as |route| }}r.HandleFunc("{{ route.Path }}",
|
|
151
|
+
{{#each routes as |route| }}r.HandleFunc("{{ route.Path }}", wrap({{ route.Pkg }}.{{ route.Method }})).Methods("{{ route.Method }}")
|
|
151
152
|
{{/each}}
|
|
152
153
|
if !isLambda {
|
|
153
154
|
println("http server listening on http://localhost:3000")
|
|
@@ -162,9 +163,29 @@ func main() {
|
|
|
162
163
|
log.Print("running in lambda mode")
|
|
163
164
|
log.Fatal(gateway.ListenAndServe(":3000", r))
|
|
164
165
|
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
func wrap(h interface{}) func(http.ResponseWriter, *http.Request) {
|
|
169
|
+
return func(w http.ResponseWriter, r *http.Request) {
|
|
170
|
+
ctx, err := context.NewReqContext(r)
|
|
171
|
+
if err != nil {
|
|
172
|
+
wapp.RespondError(w, 500, err)
|
|
173
|
+
return
|
|
174
|
+
}
|
|
175
|
+
err = wapp.PerformRequest(h, ctx, w, r)
|
|
176
|
+
if err != nil {
|
|
177
|
+
ctx.Rollback()
|
|
178
|
+
} else {
|
|
179
|
+
ctx.Commit()
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
165
|
-
|
|
183
|
+
`, ctx)
|
|
184
|
+
if err != nil {
|
|
185
|
+
panic(err)
|
|
186
|
+
}
|
|
187
|
+
err = ioutil.WriteFile("main.go", []byte(s), 0644)
|
|
166
188
|
if err != nil {
|
|
167
189
|
panic(err)
|
|
168
190
|
}
|
|
169
|
-
println(s)
|
|
170
191
|
}
|
context.go
CHANGED
|
@@ -65,7 +65,7 @@ func RespondError(w http.ResponseWriter, status int, err error) {
|
|
|
65
65
|
w.Write(data)
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
func PerformRequest(h interface{}, ctx interface{}, w http.ResponseWriter, r *http.Request) {
|
|
68
|
+
func PerformRequest(h interface{}, ctx interface{}, w http.ResponseWriter, r *http.Request) error {
|
|
69
69
|
args := []reflect.Value{reflect.ValueOf(ctx)}
|
|
70
70
|
funcType := reflect.TypeOf(h)
|
|
71
71
|
icount := funcType.NumIn()
|
|
@@ -76,7 +76,7 @@ func PerformRequest(h interface{}, ctx interface{}, w http.ResponseWriter, r *ht
|
|
|
76
76
|
err := json.NewDecoder(r.Body).Decode(instance.Interface())
|
|
77
77
|
if err != nil {
|
|
78
78
|
RespondError(w, 500, err)
|
|
79
|
-
return
|
|
79
|
+
return err
|
|
80
80
|
}
|
|
81
81
|
} else if r.Method == "GET" {
|
|
82
82
|
rv := instance.Elem()
|
|
@@ -96,16 +96,17 @@ func PerformRequest(h interface{}, ctx interface{}, w http.ResponseWriter, r *ht
|
|
|
96
96
|
responseError := values[2].Interface()
|
|
97
97
|
if responseError != nil {
|
|
98
98
|
RespondError(w, responseStatus, responseError.(error))
|
|
99
|
-
return
|
|
99
|
+
return responseError.(error)
|
|
100
100
|
}
|
|
101
101
|
if v, ok := response.(*Element); ok {
|
|
102
102
|
w.WriteHeader(responseStatus)
|
|
103
103
|
w.Header().Set("Content-Type", "text/html")
|
|
104
104
|
v.WriteHtml(w)
|
|
105
|
-
return
|
|
105
|
+
return nil
|
|
106
106
|
}
|
|
107
107
|
w.WriteHeader(responseStatus)
|
|
108
108
|
w.Header().Set("Content-Type", "application/json")
|
|
109
109
|
data, _ := json.Marshal(response)
|
|
110
110
|
w.Write(data)
|
|
111
|
+
return nil
|
|
111
112
|
}
|