about summary refs log tree commit diff stats
path: root/internal/server
diff options
context:
space:
mode:
authorAlan Pearce2024-05-07 18:54:45 +0200
committerAlan Pearce2024-05-07 18:54:45 +0200
commit771ef706d7c70f583dad956077eaf79fc4fdc093 (patch)
treee5940f27bcec96607dfd024b00dc2d34ee9df19e /internal/server
parentc15b142b18dcdc7f5ab6d5f1afca8ae1696692cc (diff)
downloadsearchix-771ef706d7c70f583dad956077eaf79fc4fdc093.tar.lz
searchix-771ef706d7c70f583dad956077eaf79fc4fdc093.tar.zst
searchix-771ef706d7c70f583dad956077eaf79fc4fdc093.zip
style: split homepage and search page
Diffstat (limited to 'internal/server')
-rw-r--r--internal/server/server.go11
-rw-r--r--internal/server/templates.go24
2 files changed, 26 insertions, 9 deletions
diff --git a/internal/server/server.go b/internal/server/server.go
index 3f87b9d..c0fbfd0 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -57,6 +57,7 @@ const jsSnippet = template.HTML(livereload.JsSnippet) // #nosec G203
 type TemplateData struct {
 	LiveReload template.HTML
 	Query      string
+	Results    bool
 }
 
 type ResultData[T options.NixOption] struct {
@@ -120,6 +121,14 @@ func New(runtimeConfig *Config) (*Server, error) {
 		}
 	})
 
+	mux.HandleFunc("/search/{source}", func(w http.ResponseWriter, r *http.Request) {
+		log.Println(r.PathValue("source"))
+		err := templates["search"].Execute(w, indexData)
+		if err != nil {
+			http.Error(w, err.Error(), http.StatusInternalServerError)
+		}
+	})
+
 	timeout := 1 * time.Second
 	mux.HandleFunc("/options/results", func(w http.ResponseWriter, r *http.Request) {
 		ctx, cancel := context.WithTimeoutCause(r.Context(), timeout, errors.New("timeout"))
@@ -141,7 +150,7 @@ func New(runtimeConfig *Config) (*Server, error) {
 		}
 		if r.Header.Get("Fetch") == "true" {
 			w.Header().Add("Content-Type", "text/html; charset=utf-8")
-			err = templates["options"].Execute(w, tdata)
+			err = templates["options"].ExecuteTemplate(w, "options.gotmpl", tdata)
 		} else {
 			err = templates["options"].ExecuteTemplate(w, "index.gotmpl", tdata)
 		}
diff --git a/internal/server/templates.go b/internal/server/templates.go
index 71fc5c7..8755e36 100644
--- a/internal/server/templates.go
+++ b/internal/server/templates.go
@@ -34,14 +34,20 @@ var templateFuncs = template.FuncMap{
 	},
 }
 
-func loadTemplate(filenames ...string) (*template.Template, error) {
-	tpl := template.New(path.Base(filenames[0]))
+func loadTemplate(layoutFile string, filename string) (*template.Template, error) {
+	tpl := template.New("index.gotmpl")
 
 	tpl.Funcs(templateFuncs)
-
-	_, err := tpl.ParseFiles(filenames...)
+	_, err := tpl.ParseFiles(layoutFile)
 	if err != nil {
-		return nil, errors.WithMessage(err, "could not parse template")
+		return nil, errors.WithMessage(err, "could not parse layout template")
+	}
+
+	if filename != "" {
+		_, err = tpl.ParseGlob(filename)
+		if err != nil {
+			return nil, errors.WithMessage(err, "could not parse template")
+		}
 	}
 
 	return tpl, nil
@@ -53,21 +59,23 @@ func loadTemplates() (TemplateCollection, error) {
 
 	layoutFile := path.Join(templateDir, "index.gotmpl")
 
-	index, err := loadTemplate(layoutFile)
+	index, err := loadTemplate(layoutFile, "")
 	if err != nil {
 		return nil, err
 	}
 	templates["index"] = index
 
-	templatePaths, err := filepath.Glob(path.Join(templateDir, "blocks", "*.gotmpl"))
+	glob := path.Join(templateDir, "blocks", "*.gotmpl")
+	templatePaths, err := filepath.Glob(glob)
 	if err != nil {
 		return nil, errors.WithMessage(err, "could not glob block templates")
 	}
 	for _, fullname := range templatePaths {
-		tpl, err := loadTemplate(fullname, layoutFile)
+		tpl, err := loadTemplate(layoutFile, glob)
 		if err != nil {
 			return nil, err
 		}
+
 		name, _ := strings.CutSuffix(path.Base(fullname), ".gotmpl")
 		templates[name] = tpl
 	}