Initial commit

This commit is contained in:
Ivan 2025-06-16 13:50:33 +05:00
commit e3da3b9c7a
Signed by: landlord
GPG key ID: 416A03BC4ADA4CD7
9 changed files with 929 additions and 0 deletions

80
sitemap.go Normal file
View file

@ -0,0 +1,80 @@
package sitemap
import (
"net/http"
"strconv"
"time"
)
type ChangeFreq string
const (
ChangeFreqAlways ChangeFreq = "always"
ChangeFreqNever ChangeFreq = "never"
ChangeFreqHourly ChangeFreq = "hourly"
ChangeFreqDaily ChangeFreq = "daily"
ChangeFreqWeekly ChangeFreq = "weekly"
ChangeFreqMonthly ChangeFreq = "monthly"
ChangeFreqYearly ChangeFreq = "yearly"
)
type URL struct {
Loc string
ChangeFreq ChangeFreq
Priority float64
LastMod time.Time
}
type internalURL struct {
Loc string
ChangeFreq ChangeFreq
Priority string
// Date in ISO 8601 format.
LastMod string
}
func convertURLs(urlset []URL) []internalURL {
res := make([]internalURL, 0, len(urlset))
for _, url := range urlset {
res = append(res, internalURL{
Loc: url.Loc,
ChangeFreq: url.ChangeFreq,
Priority: formatPriority(url.Priority),
LastMod: formatLastMod(url.LastMod),
})
}
return res
}
func formatLastMod(lastMod time.Time) string {
if lastMod.IsZero() {
return ""
}
return lastMod.Format("2006-01-02")
}
func formatPriority(priority float64) string {
if priority < 0.0 {
priority = 0.0
}
if priority > 1.0 {
priority = 1.0
}
return strconv.FormatFloat(priority, 'f', 1, 64)
}
func Execute(w http.ResponseWriter, urlset []URL) error {
if parsingError != nil {
return parsingError
}
w.Header().Set("Content-Type", "application/xml")
internalURLSet := convertURLs(urlset)
return parsedTemplate.Execute(w, internalURLSet)
}