예제 #1
0
//CreateXMLSitemap creates xml sitemap for search engines, and saves in public/sitemap folder
func CreateXMLSitemap() {
	logrus.Info("Starting XML sitemap generation")
	folder := path.Join(GetConfig().Public, "sitemap")
	domain := GetConfig().Domain
	now := time.Now()
	items := make([]sitemap.Item, 1)

	//Home page
	items = append(items, sitemap.Item{
		Loc:        fmt.Sprintf("%s", domain),
		LastMod:    now,
		Changefreq: "daily",
		Priority:   1,
	})

	//Posts
	posts, err := models.GetPublishedPosts()
	if err != nil {
		logrus.Error(err)
		return
	}
	for i := range posts {
		items = append(items, sitemap.Item{
			Loc:        fmt.Sprintf("%s/posts/%d", domain, posts[i].ID),
			LastMod:    posts[i].UpdatedAt,
			Changefreq: "weekly",
			Priority:   0.9,
		})
	}

	//Static pages
	pages, err := models.GetPublishedPages()
	if err != nil {
		logrus.Error(err)
		return
	}
	for i := range pages {
		items = append(items, sitemap.Item{
			Loc:        fmt.Sprintf("%s/pages/%d", domain, pages[i].ID),
			LastMod:    pages[i].UpdatedAt,
			Changefreq: "monthly",
			Priority:   0.8,
		})
	}
	if err := sitemap.SiteMap(path.Join(folder, "sitemap1.xml.gz"), items); err != nil {
		logrus.Error(err)
		return
	}
	if err := sitemap.SiteMapIndex(folder, "sitemap_index.xml", domain+"/public/sitemap/"); err != nil {
		logrus.Error(err)
		return
	}
	logrus.Info("XML sitemap has been generated in " + folder)
}
예제 #2
0
파일: rss.go 프로젝트: denisbakhtin/ginblog
//RssGet handles GET /rss route
func RssGet(c *gin.Context) {
	now := time.Now()
	domain := system.GetConfig().Domain

	feed := &feeds.Feed{
		Title:       "ginblog",
		Link:        &feeds.Link{Href: domain},
		Description: "GIN-powered blog boilerplate",
		Author:      &feeds.Author{Name: "Blog Author", Email: "*****@*****.**"}, //hide email from spammers?
		Created:     now,
		Copyright:   "This work is copyright © Ginblog",
	}

	feed.Items = make([]*feeds.Item, 0)
	posts, err := models.GetPublishedPosts()
	if err != nil {
		c.HTML(404, "errors/404", nil)
		return
	}

	for i := range posts {
		feed.Items = append(feed.Items, &feeds.Item{
			Id:          fmt.Sprintf("%s/posts/%d", domain, posts[i].ID),
			Title:       posts[i].Name,
			Link:        &feeds.Link{Href: fmt.Sprintf("%s/posts/%d", domain, posts[i].ID)},
			Description: string(posts[i].Excerpt()),
			Author:      &feeds.Author{Name: posts[i].Author.Name},
			Created:     now,
		})
	}

	rss, err := feed.ToRss()
	if err != nil {
		c.HTML(500, "errors/500", nil)
		return
	}
	c.Data(200, "text/xml", []byte(rss))
}