~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.
improve http
- _example/makefile +0 -4
- http.go +21 -22
_example/makefile
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
setup:
|
|
2
2
|
go install github.com/mitranim/gow@latest
|
|
3
|
-
go install github.com/pyros2097/gromer/cmd/gromer@latest
|
|
4
3
|
go install github.com/playwright-community/playwright-go/cmd/playwright@latest
|
|
5
4
|
playwright install --with-deps
|
|
6
5
|
|
|
7
|
-
update:
|
|
8
|
-
gromer -pkg github.com/pyros2097/gromer/_example
|
|
9
|
-
|
|
10
6
|
dev:
|
|
11
7
|
gow run main.go
|
|
12
8
|
|
http.go
CHANGED
|
@@ -31,31 +31,15 @@ import (
|
|
|
31
31
|
"xojoc.pw/useragent"
|
|
32
32
|
)
|
|
33
33
|
|
|
34
|
-
const (
|
|
35
|
-
gzipEncoding = "gzip"
|
|
36
|
-
flateEncoding = "deflate"
|
|
37
|
-
acceptEncoding = "Accept-Encoding"
|
|
38
|
-
)
|
|
39
|
-
|
|
40
34
|
var (
|
|
41
35
|
info *debug.BuildInfo
|
|
42
36
|
IsCloundRun bool
|
|
43
|
-
routeDefs []RouteDefinition
|
|
44
37
|
pathParamsRegex = regexp.MustCompile(`{(.*?)}`)
|
|
45
38
|
globalStatusComponent StatusComponent = nil
|
|
46
39
|
)
|
|
47
40
|
|
|
48
41
|
type StatusComponent func(c *gsx.Context, status int, err error) []*gsx.Tag
|
|
49
42
|
|
|
50
|
-
type RouteDefinition struct {
|
|
51
|
-
Pkg string `json:"pkg"`
|
|
52
|
-
PkgPath string `json:"pkgPath"`
|
|
53
|
-
Method string `json:"method"`
|
|
54
|
-
Path string `json:"path"`
|
|
55
|
-
PathParams []string `json:"pathParams"`
|
|
56
|
-
Params interface{} `json:"params"`
|
|
57
|
-
}
|
|
58
|
-
|
|
59
43
|
func init() {
|
|
60
44
|
IsCloundRun = os.Getenv("K_REVISION") != ""
|
|
61
45
|
info, _ = debug.ReadBuildInfo()
|
|
@@ -124,7 +108,7 @@ func GetRouteParams(route string) []string {
|
|
|
124
108
|
return params
|
|
125
109
|
}
|
|
126
110
|
|
|
127
|
-
func PerformRequest(route string, h interface{}, c
|
|
111
|
+
func PerformRequest(route string, h interface{}, c interface{}, w http.ResponseWriter, r *http.Request, isJson bool) {
|
|
128
112
|
params := GetRouteParams(route)
|
|
129
113
|
args := []reflect.Value{reflect.ValueOf(c)}
|
|
130
114
|
funcType := reflect.TypeOf(h)
|
|
@@ -200,7 +184,9 @@ func PerformRequest(route string, h interface{}, c *gsx.Context, w http.Response
|
|
|
200
184
|
RespondError(w, r, 400, eris.Errorf("Illegal Content-Type tag found %s", contentType))
|
|
201
185
|
return
|
|
202
186
|
}
|
|
187
|
+
if !isJson {
|
|
203
|
-
|
|
188
|
+
c.(*gsx.Context).Set("params", instance.Elem().Interface())
|
|
189
|
+
}
|
|
204
190
|
args = append(args, instance.Elem())
|
|
205
191
|
}
|
|
206
192
|
values := reflect.ValueOf(h).Call(args)
|
|
@@ -216,7 +202,7 @@ func PerformRequest(route string, h interface{}, c *gsx.Context, w http.Response
|
|
|
216
202
|
w.WriteHeader(responseStatus)
|
|
217
203
|
data, err := json.Marshal(response)
|
|
218
204
|
if err != nil {
|
|
219
|
-
RespondError(w, r, responseStatus, eris.Wrap(responseError.(error), "
|
|
205
|
+
RespondError(w, r, responseStatus, eris.Wrap(responseError.(error), "Json Marshal failed"))
|
|
220
206
|
return
|
|
221
207
|
}
|
|
222
208
|
w.Write(data)
|
|
@@ -226,7 +212,7 @@ func PerformRequest(route string, h interface{}, c *gsx.Context, w http.Response
|
|
|
226
212
|
// This has to be at end always
|
|
227
213
|
w.WriteHeader(responseStatus)
|
|
228
214
|
if responseStatus != 204 {
|
|
229
|
-
gsx.Write(c, w, response.([]*gsx.Tag))
|
|
215
|
+
gsx.Write(c.(*gsx.Context), w, response.([]*gsx.Tag))
|
|
230
216
|
}
|
|
231
217
|
}
|
|
232
218
|
|
|
@@ -255,6 +241,19 @@ func LogMiddleware(next http.Handler) http.Handler {
|
|
|
255
241
|
})
|
|
256
242
|
}
|
|
257
243
|
|
|
244
|
+
func CorsMiddleware(next http.Handler) http.Handler {
|
|
245
|
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
246
|
+
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
247
|
+
w.Header().Set("Access-Control-Allow-Methods", "*")
|
|
248
|
+
w.Header().Set("Access-Control-Allow-Headers", "*")
|
|
249
|
+
if r.Method == "OPTIONS" {
|
|
250
|
+
w.WriteHeader(200)
|
|
251
|
+
return
|
|
252
|
+
}
|
|
253
|
+
next.ServeHTTP(w, r)
|
|
254
|
+
})
|
|
255
|
+
}
|
|
256
|
+
|
|
258
257
|
func CompressMiddleware(next http.Handler) http.Handler {
|
|
259
258
|
return handlers.CompressHandler(next)
|
|
260
259
|
}
|
|
@@ -348,8 +347,8 @@ func PageRoute(router *mux.Router, route string, page, action interface{}) {
|
|
|
348
347
|
|
|
349
348
|
func ApiRoute(router *mux.Router, method, route string, h interface{}) {
|
|
350
349
|
router.HandleFunc(route, func(w http.ResponseWriter, r *http.Request) {
|
|
351
|
-
|
|
350
|
+
ctx := context.WithValue(context.WithValue(r.Context(), "url", r.URL), "header", r.Header)
|
|
352
|
-
PerformRequest(route, h,
|
|
351
|
+
PerformRequest(route, h, ctx, w, r, true)
|
|
353
352
|
}).Methods(method)
|
|
354
353
|
}
|
|
355
354
|
|