stupid proofing missing "http" in config file
This commit is contained in:
parent
75921ef7b5
commit
fb32f3532e
1 changed files with 20 additions and 7 deletions
|
@ -21,25 +21,38 @@ type URL struct {
|
|||
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 {
|
||||
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://"
|
||||
// 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
|
||||
}
|
||||
return "https://" + domain
|
||||
}
|
||||
|
||||
func generateOpenSearchXML(config Config) {
|
||||
baseURL := addProtocol(config.Domain)
|
||||
|
||||
opensearch := OpenSearchDescription{
|
||||
Xmlns: "http://a9.com/-/spec/opensearch/1.1/",
|
||||
ShortName: "Ocásek",
|
||||
ShortName: "Search Engine",
|
||||
Description: "Search engine",
|
||||
Tags: "search, engine",
|
||||
URL: URL{
|
||||
Type: "text/html",
|
||||
Template: fmt.Sprintf("%s%s/search?q={searchTerms}", protocol, config.Domain),
|
||||
Template: fmt.Sprintf("%s/search?q={searchTerms}", baseURL),
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue