11
"xelbot.com/reprogl/container"
12
"xelbot.com/reprogl/models"
13
"xelbot.com/reprogl/models/repositories"
16
func SitemapAction(app *container.Application) http.HandlerFunc {
17
return func(w http.ResponseWriter, r *http.Request) {
18
if container.IsCDN(r) {
19
w.Header().Set("Content-Type", "application/xml")
20
cacheControl(w, container.FeedTTL)
21
w.Write([]byte(xml.Header + "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"></urlset>\n"))
26
repo := repositories.ArticleRepository{DB: app.DB}
27
articles, err := repo.GetSitemapCollection()
29
app.ServerError(w, err)
34
for _, location := range articles {
35
location.URL = container.GenerateAbsoluteURL("article", "slug", location.Slug)
39
dt1, _ = time.Parse("02 Jan 2006 15:04:05 -0700", "25 Jun 2024 22:51:33 +0300")
40
articles = append(articles, &models.SitemapItem{
41
URL: container.GenerateAbsoluteURL("info-page"),
42
UpdatedAt: models.SitemapTime(dt1),
45
dt1, _ = time.Parse("02 Jan 2006 15:04:05 -0700", "25 Apr 2024 17:24:39 +0300")
46
articles = append(articles, &models.SitemapItem{
47
URL: container.GenerateAbsoluteURL("statistics"),
48
UpdatedAt: models.SitemapTime(dt1),
52
urlSet := models.SitemapURLSet{Items: articles}
54
bytes, err := xml.Marshal(urlSet)
56
app.ServerError(w, err)
61
w.Header().Set("Content-Type", "application/xml")
62
cacheControl(w, container.FeedTTL)
63
_, err = w.Write([]byte(xml.Header + `<?xml-stylesheet type="text/xsl" href="/sitemap.xsl"?>` + "\n"))
65
app.ServerError(w, err)
70
_, err = w.Write(bytes)
72
app.ServerError(w, err)
77
func FeedAction(app *container.Application, feedType int) http.HandlerFunc {
78
return func(w http.ResponseWriter, r *http.Request) {
79
var feed models.FeedGeneratorInterface
81
repo := repositories.ArticleRepository{DB: app.DB}
82
articles, err := repo.GetFeedCollection()
84
app.ServerError(w, err)
89
for _, location := range articles {
90
location.URL = container.GenerateAbsoluteURL("article", "slug", location.Slug)
94
case models.AtomFeedType:
95
feed = models.CreateFeed(new(models.Atom), channelData(articles))
96
case models.RssFeedType:
97
feed = models.CreateFeed(new(models.Rss), channelData(articles))
99
app.ServerError(w, errors.New("undefined feed type"))
104
bytes, err := feed.AsXML()
106
app.ServerError(w, err)
111
w.Header().Set("Content-Type", feed.ContentType())
112
cacheControl(w, container.FeedTTL)
113
_, err = w.Write([]byte(xml.Header))
115
app.ServerError(w, err)
120
_, err = w.Write(bytes)
122
app.ServerError(w, err)
127
func channelData(items models.FeedItemList) models.FeedChannelData {
128
cfg := container.GetConfig()
130
channel := models.FeedChannelData{
132
Link: "https://" + cfg.Host + "/",
133
Description: cfg.Host + " - последние записи",
136
Author: cfg.Author.FullName,
137
Email: cfg.Author.Email,
138
Generator: fmt.Sprintf(
140
container.GitRevision,