route directly to partial
This commit is contained in:
parent
13b3e9e42b
commit
6c77a814ce
1 changed files with 21 additions and 42 deletions
|
|
@ -233,59 +233,38 @@ func findPublicFuncsReturningHPage(dir string) ([]Page, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildGetPartialFromContext(builder *CodeBuilder, partials []Partial) {
|
func buildGetPartialFromContext(builder *CodeBuilder, partials []Partial) {
|
||||||
fName := "GetPartialFromContext"
|
|
||||||
|
|
||||||
body := `
|
|
||||||
path := r.URL.Path
|
|
||||||
`
|
|
||||||
|
|
||||||
if len(partials) == 0 {
|
if len(partials) == 0 {
|
||||||
body = ""
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
moduleName := GetModuleName()
|
moduleName := GetModuleName()
|
||||||
for _, f := range partials {
|
|
||||||
if f.FuncName == fName {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
caller := fmt.Sprintf("%s.%s", f.Package, f.FuncName)
|
|
||||||
path := fmt.Sprintf("/%s/%s.%s", moduleName, f.Import, f.FuncName)
|
|
||||||
|
|
||||||
body += fmt.Sprintf(`
|
var routerHandlerMethod = func(path string, caller string) string {
|
||||||
if path == "%s" || path == "%s" {
|
return fmt.Sprintf(`
|
||||||
cc := r.Context().Value(h.RequestContextKey).(*h.RequestContext)
|
router.Handle("%s", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
return %s(cc)
|
cc := r.Context().Value(h.RequestContextKey).(*h.RequestContext)
|
||||||
}
|
partial := %s(cc)
|
||||||
`, f.FuncName, path, caller)
|
|
||||||
}
|
|
||||||
|
|
||||||
body += "return nil"
|
|
||||||
|
|
||||||
f := Function{
|
|
||||||
Name: fName,
|
|
||||||
Parameters: []NameType{
|
|
||||||
{Name: "r", Type: "*http.Request"},
|
|
||||||
},
|
|
||||||
Return: []ReturnType{
|
|
||||||
{Type: "*h.Partial"},
|
|
||||||
},
|
|
||||||
Body: body,
|
|
||||||
}
|
|
||||||
|
|
||||||
builder.Append(builder.BuildFunction(f))
|
|
||||||
|
|
||||||
registerFunction := fmt.Sprintf(`
|
|
||||||
func RegisterPartials(router *chi.Mux) {
|
|
||||||
router.Handle("/%s/partials*", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
partial := GetPartialFromContext(r)
|
|
||||||
if partial == nil {
|
if partial == nil {
|
||||||
w.WriteHeader(404)
|
w.WriteHeader(404)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
h.PartialView(w, partial)
|
h.PartialView(w, partial)
|
||||||
}))
|
}))`, path, caller)
|
||||||
|
}
|
||||||
|
|
||||||
|
handlerMethods := make([]string, 0)
|
||||||
|
|
||||||
|
for _, f := range partials {
|
||||||
|
caller := fmt.Sprintf("%s.%s", f.Package, f.FuncName)
|
||||||
|
path := fmt.Sprintf("/%s/%s.%s", moduleName, f.Import, f.FuncName)
|
||||||
|
handlerMethods = append(handlerMethods, routerHandlerMethod(path, caller))
|
||||||
|
}
|
||||||
|
|
||||||
|
registerFunction := fmt.Sprintf(`
|
||||||
|
func RegisterPartials(router *chi.Mux) {
|
||||||
|
%s
|
||||||
}
|
}
|
||||||
`, moduleName)
|
`, strings.Join(handlerMethods, "\n"))
|
||||||
|
|
||||||
builder.AppendLine(registerFunction)
|
builder.AppendLine(registerFunction)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue