50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
/*
|
|
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
package http
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/locale"
|
|
)
|
|
|
|
type languageLinks struct {
|
|
http.ResponseWriter
|
|
schemaAuthority string
|
|
path string
|
|
locales locale.Locales
|
|
wroteHeader bool
|
|
}
|
|
|
|
func (w *languageLinks) WriteHeader(statusCode int) {
|
|
if statusCode >= 200 && statusCode < 300 {
|
|
for k := range w.locales {
|
|
tag := k.String()
|
|
w.Header().Add("Link", fmt.Sprintf(`<%[1]s/%[2]s%[3]s>; rel="alternate"; hreflang="%[2]s"`, w.schemaAuthority, tag, w.path))
|
|
}
|
|
}
|
|
w.wroteHeader = true
|
|
w.ResponseWriter.WriteHeader(statusCode)
|
|
}
|
|
|
|
func (w *languageLinks) Write(data []byte) (int, error) {
|
|
if !w.wroteHeader {
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
return w.ResponseWriter.Write(data)
|
|
}
|
|
|
|
func LanguageLinks(w http.ResponseWriter, https bool, authority string, path string, locales locale.Locales) http.ResponseWriter {
|
|
var schema string
|
|
if https {
|
|
schema = "https"
|
|
} else {
|
|
schema = "http"
|
|
}
|
|
return &languageLinks{w, schema + "://" + authority, path, locales, false}
|
|
}
|