Fix echo's URI routing race condition that caused random 404s.

This commit is contained in:
Kailash Nadh 2020-11-08 18:36:25 +05:30
parent 684c64ced1
commit 1e8b533d45
4 changed files with 29 additions and 9 deletions

View File

@ -267,9 +267,9 @@ func handleCreateCampaign(c echo.Context) error {
}
// Hand over to the GET handler to return the last insertion.
c.SetParamNames("id")
c.SetParamValues(fmt.Sprintf("%d", newID))
return handleGetCampaigns(c)
return handleGetCampaigns(copyEchoCtx(c, map[string]string{
"id": fmt.Sprintf("%d", newID),
}))
}
// handleUpdateCampaign handles campaign modification.

View File

@ -238,3 +238,23 @@ func getPagination(q url.Values, perPage, maxPerPage int) pagination {
Limit: perPage,
}
}
// copyEchoCtx returns a copy of the the current echo.Context in a request
// with the given params set for the active handler to proxy the request
// to another handler without mutating its context.
func copyEchoCtx(c echo.Context, params map[string]string) echo.Context {
var (
keys = make([]string, 0, len(params))
vals = make([]string, 0, len(params))
)
for k, v := range params {
keys = append(keys, k)
vals = append(vals, v)
}
b := c.Echo().NewContext(c.Request(), c.Response())
b.Set("app", c.Get("app").(*App))
b.SetParamNames(keys...)
b.SetParamValues(vals...)
return b
}

View File

@ -118,9 +118,9 @@ func handleCreateList(c echo.Context) error {
}
// Hand over to the GET handler to return the last insertion.
c.SetParamNames("id")
c.SetParamValues(fmt.Sprintf("%d", newID))
return handleGetLists(c)
return handleGetLists(copyEchoCtx(c, map[string]string{
"id": fmt.Sprintf("%d", newID),
}))
}
// handleUpdateList handles list modification.

View File

@ -147,9 +147,9 @@ func handleCreateTemplate(c echo.Context) error {
}
// Hand over to the GET handler to return the last insertion.
c.SetParamNames("id")
c.SetParamValues(fmt.Sprintf("%d", newID))
return handleGetTemplates(c)
return handleGetTemplates(copyEchoCtx(c, map[string]string{
"id": fmt.Sprintf("%d", newID),
}))
}
// handleUpdateTemplate handles template modification.