Add opensearch endpoint and handler
This commit is contained in:
35
internal/opensearch_handler.go
Normal file
35
internal/opensearch_handler.go
Normal 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,
|
||||
)),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -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())
|
||||
|
||||
|
||||
@@ -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" />
|
||||
|
||||
|
||||
Reference in New Issue
Block a user