GoBlog/opensearch.go

29 lines
1010 B
Go
Raw Normal View History

2021-06-09 20:44:22 +00:00
package main
import (
"fmt"
"net/http"
)
func (a *goBlog) serveOpenSearch(w http.ResponseWriter, r *http.Request) {
blog := r.Context().Value(blogContextKey).(string)
b := a.cfg.Blogs[blog]
title := b.Title
sURL := a.getFullAddress(b.getRelativePath(b.Search.Path))
2021-06-09 20:44:22 +00:00
xml := fmt.Sprintf("<?xml version=\"1.0\"?><OpenSearchDescription xmlns=\"http://a9.com/-/spec/opensearch/1.1/\" xmlns:moz=\"http://www.mozilla.org/2006/browser/search/\">"+
"<ShortName>%s</ShortName><Description>%s</Description>"+
"<Url type=\"text/html\" method=\"post\" template=\"%s\"><Param name=\"q\" value=\"{searchTerms}\" /></Url>"+
"<moz:SearchForm>%s</moz:SearchForm>"+
"</OpenSearchDescription>",
title, title, sURL, sURL)
w.Header().Set(contentType, "application/opensearchdescription+xml")
_, _ = writeMinified(w, contentTypeXML, []byte(xml))
2021-06-09 20:44:22 +00:00
}
func openSearchUrl(b *configBlog) string {
if b.Search != nil && b.Search.Enabled {
return b.getRelativePath(b.Search.Path + "/opensearch.xml")
}
return ""
}