diff options
-rw-r--r-- | frontend/templates/blocks/search.gotmpl | 2 | ||||
-rw-r--r-- | frontend/templates/index.gotmpl | 10 | ||||
-rw-r--r-- | frontend/templates/opensearch.xml.gotmpl | 16 | ||||
-rw-r--r-- | internal/server/mux.go | 36 | ||||
-rw-r--r-- | internal/server/templates.go | 16 |
5 files changed, 77 insertions, 3 deletions
diff --git a/frontend/templates/blocks/search.gotmpl b/frontend/templates/blocks/search.gotmpl index 3ad05bf..6f47df3 100644 --- a/frontend/templates/blocks/search.gotmpl +++ b/frontend/templates/blocks/search.gotmpl @@ -11,6 +11,6 @@ {{- end }} {{- end }} -{{- define "js" }} +{{- define "head" }} <script src="/static/search.js" defer></script> {{- end }} diff --git a/frontend/templates/index.gotmpl b/frontend/templates/index.gotmpl index 6db4cc2..90f875b 100644 --- a/frontend/templates/index.gotmpl +++ b/frontend/templates/index.gotmpl @@ -6,9 +6,17 @@ <title>Searchix</title> <link href="/static/base.css" rel="stylesheet" /> <link href="/static/style.css" rel="stylesheet" /> - {{ block "js" . }} + {{ block "head" . }} {{ end }} {{ .ExtraHeadHTML }} + {{- range $key, $value := .Sources }} + <link + rel="search" + type="application/opensearchdescription+xml" + title="Searchix {{ $value.Name }}" + href="/options/{{ $key }}/opensearch.xml" + /> + {{- end }} </head> <body> <header> diff --git a/frontend/templates/opensearch.xml.gotmpl b/frontend/templates/opensearch.xml.gotmpl new file mode 100644 index 0000000..8d978ea --- /dev/null +++ b/frontend/templates/opensearch.xml.gotmpl @@ -0,0 +1,16 @@ +<OpenSearchDescription + xmlns="http://a9.com/-/spec/opensearch/1.1/" + xmlns:moz="http://www.mozilla.org/2006/browser/search/" +> + <ShortName>Searchix {{ .Source.Name }}</ShortName> + <LongName>Search {{ .Source.Name }} options with Searchix</LongName> + <Description>Search options {{ .Source.Name }}</Description> + <Url + type="text/html" + method="get" + template="{{ .BaseURL }}/options/{{ .Source.Key }}/search?query={searchTerms}&from=opensearch" + /> + <moz:SearchForm + >{{ .BaseURL }}/options/{{ .Source.Key }}/search</moz:SearchForm + > +</OpenSearchDescription> 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 { |