Search/open-search.go

59 lines
1.3 KiB
Go
Raw Normal View History

2024-06-12 14:26:50 +02:00
package main
import (
"encoding/xml"
"fmt"
"os"
)
type OpenSearchDescription struct {
XMLName xml.Name `xml:"OpenSearchDescription"`
Xmlns string `xml:"xmlns,attr"`
ShortName string `xml:"ShortName"`
Description string `xml:"Description"`
Tags string `xml:"Tags"`
2024-08-21 22:56:21 +02:00
URLs []URL `xml:"Url"`
2024-06-12 14:26:50 +02:00
}
type URL struct {
Type string `xml:"type,attr"`
Template string `xml:"template,attr"`
}
func generateOpenSearchXML(config Config) {
baseURL := addProtocol(config.Domain)
2024-08-08 15:05:05 +02:00
2024-06-12 14:26:50 +02:00
opensearch := OpenSearchDescription{
Xmlns: "http://a9.com/-/spec/opensearch/1.1/",
2024-10-21 07:30:48 +02:00
ShortName: "QGato",
Description: "QGato search engine",
Tags: "search, spitfire, qgato",
2024-08-21 22:56:21 +02:00
URLs: []URL{
{
Type: "text/html",
Template: fmt.Sprintf("%s/search?q={searchTerms}", baseURL),
},
{
Type: "application/x-suggestions+json",
Template: fmt.Sprintf("%s/suggestions?q={searchTerms}", baseURL),
},
2024-06-12 14:26:50 +02:00
},
}
file, err := os.Create("static/opensearch.xml")
if err != nil {
2024-08-10 13:27:23 +02:00
printErr("Error creating OpenSearch file: %v", err)
2024-06-12 14:26:50 +02:00
return
}
defer file.Close()
enc := xml.NewEncoder(file)
enc.Indent(" ", " ")
if err := enc.Encode(opensearch); err != nil {
2024-08-10 13:27:23 +02:00
printErr("Error encoding OpenSearch XML: %v", err)
2024-06-12 14:26:50 +02:00
return
}
2024-08-10 13:27:23 +02:00
printInfo("OpenSearch description file generated successfully.")
2024-06-12 14:26:50 +02:00
}