diff options
author | Alan Pearce | 2024-05-15 20:17:06 +0200 |
---|---|---|
committer | Alan Pearce | 2024-05-15 20:19:39 +0200 |
commit | 5521173ea0e05bda93096b51f67c5e813cb1d87e (patch) | |
tree | 79f066b3396b19f1b76a1e544a359b9b4088df82 /internal | |
parent | 9437f522c2ed21950acde884cafc369ca45f4b7b (diff) | |
download | searchix-5521173ea0e05bda93096b51f67c5e813cb1d87e.tar.lz searchix-5521173ea0e05bda93096b51f67c5e813cb1d87e.tar.zst searchix-5521173ea0e05bda93096b51f67c5e813cb1d87e.zip |
feat: add browser search engines via opensearch description
https://developer.mozilla.org/en-US/docs/Web/OpenSearch
Diffstat (limited to 'internal')
-rw-r--r-- | internal/server/mux.go | 36 | ||||
-rw-r--r-- | internal/server/templates.go | 16 |
2 files changed, 51 insertions, 1 deletions
diff --git a/internal/server/mux.go b/internal/server/mux.go index f9eaf03..e15535f 100644 --- a/internal/server/mux.go +++ b/internal/server/mux.go @@ -229,6 +229,42 @@ func NewMux( } }) + mux.HandleFunc( + "/options/{source}/opensearch.xml", + func(w http.ResponseWriter, r *http.Request) { + type openSearchData struct { + BaseURL string + Source *config.Source + } + + sourceKey := r.PathValue("source") + source := cfg.Importer.Sources[sourceKey] + if source == nil { + errorHandler(w, r, "Source not found", http.StatusNotFound) + + return + } + + w.Header().Set("Content-Type", "application/opensearchdescription+xml") + err := templates["opensearch.xml"].ExecuteTemplate( + w, + "opensearch.xml.gotmpl", + openSearchData{ + BaseURL: cfg.Web.BaseURL.String(), + Source: source, + }, + ) + if err != nil { + // no errorHandler; HTML does not make sense here + http.Error( + w, + fmt.Sprintf("Template render error: %v", err), + http.StatusInternalServerError, + ) + } + }, + ) + mux.Handle("/static/", http.FileServer(http.FS(frontend.Files))) if liveReload { diff --git a/internal/server/templates.go b/internal/server/templates.go index d8ffcf5..1837665 100644 --- a/internal/server/templates.go +++ b/internal/server/templates.go @@ -81,9 +81,23 @@ func loadTemplates() (TemplateCollection, error) { } templates["index"] = index - glob := path.Join(templateDir, "blocks", "*.gotmpl") + glob := path.Join(templateDir, "*.gotmpl") templatePaths, err := fs.Glob(frontend.Files, glob) if err != nil { + return nil, errors.WithMessage(err, "could not glob main templates") + } + for _, fullname := range templatePaths { + tpl, err := loadTemplate(layoutFile, fullname) + if err != nil { + return nil, err + } + name, _ := strings.CutSuffix(path.Base(fullname), ".gotmpl") + templates[name] = tpl + } + + glob = path.Join(templateDir, "blocks", "*.gotmpl") + templatePaths, err = fs.Glob(frontend.Files, glob) + if err != nil { return nil, errors.WithMessage(err, "could not glob block templates") } for _, fullname := range templatePaths { |