first drafts for inventory sync page
This commit is contained in:
+30
-16
@@ -15,11 +15,33 @@ import (
|
||||
)
|
||||
|
||||
// GET /
|
||||
// compiles the page template or component template matching the url
|
||||
func (s *Server) serveTemplates(r *http.Request) (response.Response, error) {
|
||||
ctx := r.Context()
|
||||
name, pathParams := getPageTemplateNameForURL(r.URL)
|
||||
name, args := s.getTemplateNameAndArgs(r, "internal/site/templates/component_bodies")
|
||||
|
||||
templateArgs := []any{
|
||||
b, err := s.templater.ExecuteComponentBody(name, args...)
|
||||
if err == nil {
|
||||
return response.HTML(b), nil
|
||||
}
|
||||
|
||||
if !isFileNotFoundError(err) {
|
||||
return s.handleTemplateError(err, args...)
|
||||
}
|
||||
|
||||
name, args = s.getTemplateNameAndArgs(r, "internal/site/templates/page_bodies")
|
||||
|
||||
if b, err = s.templater.ExecutePage(name, args...); err == nil {
|
||||
return response.HTML(b), nil
|
||||
}
|
||||
|
||||
return s.handleTemplateError(err, args...)
|
||||
}
|
||||
|
||||
func (s *Server) getTemplateNameAndArgs(r *http.Request, templateDir string) (name string, args []any) {
|
||||
ctx := r.Context()
|
||||
name, pathParams := getTemplateNameForURL(r.URL, templateDir)
|
||||
|
||||
return name, []any{
|
||||
"Request",
|
||||
r,
|
||||
// add services and data here
|
||||
@@ -47,22 +69,15 @@ func (s *Server) serveTemplates(r *http.Request) (response.Response, error) {
|
||||
"Auth",
|
||||
newTemplateAuthenticator(r),
|
||||
}
|
||||
|
||||
b, err := s.templater.ExecutePage(name, templateArgs...)
|
||||
if err != nil {
|
||||
return s.handleTemplateError(err, templateArgs...)
|
||||
}
|
||||
|
||||
return response.Body(io.NopCloser(bytes.NewBuffer(b))), nil
|
||||
}
|
||||
|
||||
// TODO: clean this up...
|
||||
// TODO: somehow tell what the path params are and pass them up.
|
||||
// - then consider pushing this functionality into the template library.
|
||||
//
|
||||
// getPageTemplateNameForURL eliminate any trailing .html or /, and checks for any
|
||||
// getComponentTemplateNameForURL eliminate any trailing .html or /, and checks for any
|
||||
// file with path parameters in the name, eg '{abc}.html.tmpl', prefering exact filename matches.
|
||||
func getPageTemplateNameForURL(u *url.URL) (name string, params map[string]string) {
|
||||
func getTemplateNameForURL(u *url.URL, templateDir string) (name string, params map[string]string) {
|
||||
fp := strings.TrimPrefix(strings.TrimSuffix(strings.TrimSuffix(u.Path, ".html"), "/"), "/")
|
||||
if fp == "" {
|
||||
// "/" maps to "/index"
|
||||
@@ -72,17 +87,16 @@ func getPageTemplateNameForURL(u *url.URL) (name string, params map[string]strin
|
||||
fpParts := strings.Split(fp, "/")
|
||||
res := getMatchingGlobPatternsCapturingFilepathIncludingParametrizedFilepaths(fpParts)
|
||||
for _, combs := range res {
|
||||
const pageBodiesPrefix = "internal/site/templates/page_bodies"
|
||||
pattern := path.Join(pageBodiesPrefix, path.Join(combs...)) + ".html.tmpl"
|
||||
pattern := path.Join(templateDir, path.Join(combs...)) + ".html.tmpl"
|
||||
|
||||
matches, _ := filepath.Glob(pattern)
|
||||
if len(matches) == 0 {
|
||||
pattern := path.Join(pageBodiesPrefix, path.Join(combs...), "index") + ".html.tmpl"
|
||||
pattern := path.Join(templateDir, path.Join(combs...), "index") + ".html.tmpl"
|
||||
matches, _ = filepath.Glob(pattern)
|
||||
}
|
||||
if len(matches) > 0 {
|
||||
match := matches[0]
|
||||
name = strings.TrimPrefix(strings.TrimSuffix(match, ".html.tmpl"), pageBodiesPrefix+"/")
|
||||
name = strings.TrimPrefix(strings.TrimSuffix(match, ".html.tmpl"), templateDir+"/")
|
||||
|
||||
patternParts := strings.Split(name, "/")
|
||||
params = make(map[string]string)
|
||||
|
||||
Reference in New Issue
Block a user