stupid proofing missing "http" in config file

This commit is contained in:
partisan 2024-08-08 16:23:36 +02:00
parent 17589d51e5
commit 283096d299

View file

@ -21,25 +21,38 @@ type URL struct {
Template string `xml:"template,attr"` Template string `xml:"template,attr"`
} }
// isLocalAddress checks if the domain is a local address // 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
func isLocalAddress(domain string) bool { func isLocalAddress(domain string) bool {
return domain == "localhost" || strings.HasPrefix(domain, "127.") || strings.HasPrefix(domain, "192.168.") || strings.HasPrefix(domain, "10.") return domain == "localhost" || strings.HasPrefix(domain, "127.") || strings.HasPrefix(domain, "192.168.") || strings.HasPrefix(domain, "10.")
} }
func generateOpenSearchXML(config Config) { // Ensures that HTTP or HTTPS is befor the adress if needed
protocol := "https://" func addProtocol(domain string) string {
if isLocalAddress(config.Domain) { if hasProtocol(domain) {
protocol = "http://" return domain
} }
if isLocalAddress(domain) {
return "http://" + domain
}
return "https://" + domain
}
func generateOpenSearchXML(config Config) {
baseURL := addProtocol(config.Domain)
opensearch := OpenSearchDescription{ opensearch := OpenSearchDescription{
Xmlns: "http://a9.com/-/spec/opensearch/1.1/", Xmlns: "http://a9.com/-/spec/opensearch/1.1/",
ShortName: "Ocásek", ShortName: "Search Engine",
Description: "Search engine", Description: "Search engine",
Tags: "search, engine", Tags: "search, engine",
URL: URL{ URL: URL{
Type: "text/html", Type: "text/html",
Template: fmt.Sprintf("%s%s/search?q={searchTerms}", protocol, config.Domain), Template: fmt.Sprintf("%s/search?q={searchTerms}", baseURL),
}, },
} }