Add opensearch endpoint and handler

This commit is contained in:
James Mills
2021-02-02 15:12:08 +10:00
parent 3923ff91ba
commit 93cab4c1bc
3 changed files with 39 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
package internal
import (
"fmt"
"net/http"
"github.com/julienschmidt/httprouter"
)
const OpenSearchTemplate string = `<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>%s</ShortName>
<Description>%s</Description>
<Tags>search</Tags>
<Contact>%s</Contact>
<Url type="text/html" method="get" template="%s?q={searchTerms}"/>
</OpenSearchDescription>
`
// OpenSearchHandler ...
func (s *Server) OpenSearchHandler() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Set("Content-Type", "text/xml")
w.Write(
[]byte(fmt.Sprintf(
OpenSearchTemplate,
s.config.Name,
s.config.Description,
s.config.AdminEmail,
s.config.BaseURL,
)),
)
}
}

View File

@@ -285,6 +285,8 @@ func (s *Server) initRoutes() {
s.router.GET("/", s.IndexHandler())
s.router.HEAD("/", s.IndexHandler())
s.router.GET("/opensearch.xml", s.OpenSearchHandler())
s.router.GET("/robots.txt", s.RobotsHandler())
s.router.HEAD("/robots.txt", s.RobotsHandler())

View File

@@ -13,6 +13,8 @@
<link rel="icon" type="image/png" href="/img/{{ .Commit}}/favicon.png" />
{{ end }}
<link rel="search" type="application/opensearchdescription+xml" href="/opensearch.xml" title="Spyda Search">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />