~repos /gromer

#golang#htmx#ssr

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.


c277a7ec Peter John

tag: v0.6.2

v0.6.2

4 years ago
fix path params
Files changed (3) hide show
  1. cmd/wapp/main.go +6 -7
  2. html.go +10 -1
  3. html_test.go +10 -0
cmd/wapp/main.go CHANGED
@@ -7,20 +7,17 @@ import (
7
7
  "log"
8
8
  "os"
9
9
  "path/filepath"
10
- "regexp"
11
10
  "strings"
12
11
 
13
12
  "github.com/gobuffalo/velvet"
13
+ "github.com/pyros2097/wapp"
14
14
  "golang.org/x/mod/modfile"
15
15
  )
16
16
 
17
- var pathParamsRegex = regexp.MustCompile(`{(.*?)}`)
18
-
19
17
  type Route struct {
20
18
  Method string
21
19
  Path string
22
20
  Pkg string
23
- Params []string
24
21
  }
25
22
 
26
23
  type ApiCall struct {
@@ -204,12 +201,11 @@ func main() {
204
201
  pkg = "pages"
205
202
  }
206
203
  routePath := rewritePath(path)
207
- params := pathParamsRegex.FindAllString(routePath, -1)
204
+ params := wapp.GetRouteParams(routePath)
208
205
  routes = append(routes, &Route{
209
206
  Method: method,
210
207
  Path: routePath,
211
208
  Pkg: rewritePkg(pkg),
212
- Params: params,
213
209
  })
214
210
  if strings.Contains(path, "/api/") {
215
211
  apiCalls = append(apiCalls, getApiFunc(method, path, params))
@@ -233,6 +229,7 @@ func main() {
233
229
  package main
234
230
 
235
231
  import (
232
+ c "context"
236
233
  "embed"
237
234
  "net/http"
238
235
  "os"
@@ -283,7 +280,9 @@ func handle(router *mux.Router, method, route string, h interface{}) {
283
280
  defer func() {
284
281
  wapp.LogReq(status, r)
285
282
  }()
286
- ctx, err := context.WithContext(r.Context())
283
+ ctx, err := context.WithContext(c.WithValue(
284
+ c.WithValue(r.Context(), "url", r.URL),
285
+ "header", r.Header))
287
286
  if err != nil {
288
287
  wapp.RespondError(w, 500, err)
289
288
  return
html.go CHANGED
@@ -425,8 +425,17 @@ func RespondError(w http.ResponseWriter, status int, err error) {
425
425
 
426
426
  var pathParamsRegex = regexp.MustCompile(`{(.*?)}`)
427
427
 
428
+ func GetRouteParams(route string) []string {
429
+ params := []string{}
430
+ found := pathParamsRegex.FindAllString(route, -1)
431
+ for _, v := range found {
432
+ params = append(params, strings.Replace(strings.Replace(v, "}", "", 1), "{", "", 1))
433
+ }
434
+ return params
435
+ }
436
+
428
437
  func PerformRequest(route string, h interface{}, ctx interface{}, w http.ResponseWriter, r *http.Request) (int, error) {
429
- params := pathParamsRegex.FindAllString(route, -1)
438
+ params := GetRouteParams(route)
430
439
  args := []reflect.Value{reflect.ValueOf(ctx)}
431
440
  funcType := reflect.TypeOf(h)
432
441
  icount := funcType.NumIn()
html_test.go CHANGED
@@ -9,6 +9,16 @@ import (
9
9
  . "github.com/franela/goblin"
10
10
  )
11
11
 
12
+ func TestGetRouteParams(t *testing.T) {
13
+ g := Goblin(t)
14
+ g.Describe("GetRouteParams", func() {
15
+ g.It("should return all the right params", func() {
16
+ params := GetRouteParams("/api/todos/{id}/update/{action}")
17
+ g.Assert(params).Equal([]string{"id", "action"})
18
+ })
19
+ })
20
+ }
21
+
12
22
  func Row(uis ...interface{}) *Element {
13
23
  return NewElement("div", false, append([]interface{}{Css("flex flex-row justify-center items-center")}, uis...)...)
14
24
  }