|
@@ -74,19 +74,19 @@ func generateHandler(r *mux.Router, model string) {
|
|
PathPattern{"/%s/{id}/delete", "/%s?format=html&tpl_layout=base&tpl_content=%s", []string{"DELETE"}},
|
|
PathPattern{"/%s/{id}/delete", "/%s?format=html&tpl_layout=base&tpl_content=%s", []string{"DELETE"}},
|
|
}
|
|
}
|
|
|
|
|
|
- jsonPatterns []PathPattern = []PathPattern{
|
|
|
|
|
|
+ apiPatterns []PathPattern = []PathPattern{
|
|
PathPattern{"/api/%s", "", []string{"GET"}},
|
|
PathPattern{"/api/%s", "", []string{"GET"}},
|
|
PathPattern{"/api/%s/{id}", "", []string{"GET"}},
|
|
PathPattern{"/api/%s/{id}", "", []string{"GET"}},
|
|
- PathPattern{"/api/%s/add/", "/%s/%d?format=json&tpl_layout=base&tpl_content=%s_show", []string{"GET", "POST"}},
|
|
|
|
- PathPattern{"/api/%s/{id}/update", "/%s/%d?format=json&tpl_layout=base&tpl_content=%s_show", []string{"GET", "POST"}},
|
|
|
|
- PathPattern{"/api/%s/{id}/delete", "/%s?format=json&tpl_layout=base&tpl_content=%s", []string{"DELETE"}},
|
|
|
|
|
|
+ PathPattern{"/api/%s/add/", "", []string{"GET", "POST"}},
|
|
|
|
+ PathPattern{"/api/%s/{id}/update", "", []string{"GET", "POST"}},
|
|
|
|
+ PathPattern{"/api/%s/{id}/delete", "", []string{"DELETE"}},
|
|
}
|
|
}
|
|
)
|
|
)
|
|
for _, pattern := range patterns {
|
|
for _, pattern := range patterns {
|
|
r.Handle(pattern.Path(model), jwtCookie.Handler(recoverHandler(modelHandler(model, pattern)))).Methods(pattern.Methods...)
|
|
r.Handle(pattern.Path(model), jwtCookie.Handler(recoverHandler(modelHandler(model, pattern)))).Methods(pattern.Methods...)
|
|
}
|
|
}
|
|
|
|
|
|
- for _, pattern := range jsonPatterns {
|
|
|
|
|
|
+ for _, pattern := range apiPatterns {
|
|
r.Handle(pattern.Path(model), jwtHeader.Handler(recoverHandler(modelHandler(model, pattern)))).Methods(pattern.Methods...)
|
|
r.Handle(pattern.Path(model), jwtHeader.Handler(recoverHandler(modelHandler(model, pattern)))).Methods(pattern.Methods...)
|
|
}
|
|
}
|
|
|
|
|
|
@@ -189,7 +189,7 @@ func post(w http.ResponseWriter, r *http.Request, model string, pattern PathPatt
|
|
err error
|
|
err error
|
|
)
|
|
)
|
|
|
|
|
|
- format := r.URL.Query().Get("format")
|
|
|
|
|
|
+ respFormat := renderer.GetContentFormat(r)
|
|
postFn, err := orm.PostFunc(pattern.Path(model))
|
|
postFn, err := orm.PostFunc(pattern.Path(model))
|
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -198,18 +198,19 @@ func post(w http.ResponseWriter, r *http.Request, model string, pattern PathPatt
|
|
data, err = postFn(mux.Vars(r), r)
|
|
data, err = postFn(mux.Vars(r), r)
|
|
if err != nil {
|
|
if err != nil {
|
|
respondWithError(w, r, err)
|
|
respondWithError(w, r, err)
|
|
- } else if format == "html" {
|
|
|
|
|
|
+ } else if pattern.RedirectPattern != "" {
|
|
if id := mux.Vars(r)["id"]; id != "" {
|
|
if id := mux.Vars(r)["id"]; id != "" {
|
|
modelId, _ := strconv.Atoi(id)
|
|
modelId, _ := strconv.Atoi(id)
|
|
http.Redirect(w, r, pattern.RedirectPath(model, uint(modelId)), http.StatusSeeOther)
|
|
http.Redirect(w, r, pattern.RedirectPath(model, uint(modelId)), http.StatusSeeOther)
|
|
} else {
|
|
} else {
|
|
http.Redirect(w, r, pattern.RedirectPath(model, data.GetID()), http.StatusSeeOther)
|
|
http.Redirect(w, r, pattern.RedirectPath(model, data.GetID()), http.StatusSeeOther)
|
|
}
|
|
}
|
|
|
|
+ } else {
|
|
|
|
+ renderer.Render[respFormat](w, r, data.GetID())
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- renderer.Render[format](w, r, data.GetID())
|
|
|
|
}
|
|
}
|
|
|
|
|
|
func modelHandler(model string, pattern PathPattern) http.Handler {
|
|
func modelHandler(model string, pattern PathPattern) http.Handler {
|