2024-06-12 14:26:50 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2024-08-08 15:05:05 +02:00
|
|
|
"strings"
|
2024-06-12 14:26:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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"`
|
|
|
|
URL URL `xml:"Url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type URL struct {
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
Template string `xml:"template,attr"`
|
|
|
|
}
|
|
|
|
|
2024-08-08 16:23:36 +02:00
|
|
|
// Checks if the URL already includes a protocol
|
|
|
|
func hasProtocol(url string) bool {
|
|
|
|
return strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if the domain is a local address
|
2024-08-08 15:05:05 +02:00
|
|
|
func isLocalAddress(domain string) bool {
|
|
|
|
return domain == "localhost" || strings.HasPrefix(domain, "127.") || strings.HasPrefix(domain, "192.168.") || strings.HasPrefix(domain, "10.")
|
|
|
|
}
|
|
|
|
|
2024-08-08 16:23:36 +02:00
|
|
|
// Ensures that HTTP or HTTPS is befor the adress if needed
|
|
|
|
func addProtocol(domain string) string {
|
|
|
|
if hasProtocol(domain) {
|
|
|
|
return domain
|
|
|
|
}
|
|
|
|
if isLocalAddress(domain) {
|
|
|
|
return "http://" + domain
|
2024-08-08 15:05:05 +02:00
|
|
|
}
|
2024-08-08 16:23:36 +02:00
|
|
|
return "https://" + domain
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-08 16:23:36 +02:00
|
|
|
ShortName: "Search Engine",
|
2024-06-12 14:26:50 +02:00
|
|
|
Description: "Search engine",
|
|
|
|
Tags: "search, engine",
|
|
|
|
URL: URL{
|
|
|
|
Type: "text/html",
|
2024-08-08 16:23:36 +02:00
|
|
|
Template: fmt.Sprintf("%s/search?q={searchTerms}", baseURL),
|
2024-06-12 14:26:50 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Create("static/opensearch.xml")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error creating OpenSearch file:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
enc := xml.NewEncoder(file)
|
|
|
|
enc.Indent(" ", " ")
|
|
|
|
if err := enc.Encode(opensearch); err != nil {
|
|
|
|
fmt.Println("Error encoding OpenSearch XML:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("OpenSearch description file generated successfully.")
|
|
|
|
}
|