From db96868edd2d4b5526927b45ba4aa3fa3052a06d Mon Sep 17 00:00:00 2001 From: partisan Date: Thu, 8 Aug 2024 15:05:05 +0200 Subject: [PATCH] update open-search --- open-search.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/open-search.go b/open-search.go index 2d5d0b4..138020f 100644 --- a/open-search.go +++ b/open-search.go @@ -4,6 +4,7 @@ import ( "encoding/xml" "fmt" "os" + "strings" ) type OpenSearchDescription struct { @@ -20,7 +21,17 @@ type URL struct { Template string `xml:"template,attr"` } +// isLocalAddress checks if the domain is a local address +func isLocalAddress(domain string) bool { + return domain == "localhost" || strings.HasPrefix(domain, "127.") || strings.HasPrefix(domain, "192.168.") || strings.HasPrefix(domain, "10.") +} + func generateOpenSearchXML(config Config) { + protocol := "https://" + if isLocalAddress(config.Domain) { + protocol = "http://" + } + opensearch := OpenSearchDescription{ Xmlns: "http://a9.com/-/spec/opensearch/1.1/", ShortName: "Ocásek", @@ -28,7 +39,7 @@ func generateOpenSearchXML(config Config) { Tags: "search, engine", URL: URL{ Type: "text/html", - Template: fmt.Sprintf("https://%s/search?q={searchTerms}", config.Domain), + Template: fmt.Sprintf("%s%s/search?q={searchTerms}", protocol, config.Domain), }, }