//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) }
//CreateXMLSitemap creates xml sitemap for search engines, and saves in public/sitemap folder func CreateXMLSitemap() { log.Printf("INFO: Starting XML sitemap generation\n") db := models.GetDB() folder := path.Join(GetConfig().Public, "sitemap") domain := "http://www." + GetConfig().Domain now := time.Now() items := make([]sitemap.Item, 0, 500) //Home page items = append(items, sitemap.Item{ Loc: fmt.Sprintf("%s", domain), LastMod: now, Changefreq: "daily", Priority: 1, }) //Articles items = append(items, sitemap.Item{ Loc: fmt.Sprintf("%s%s", domain, "/articles"), LastMod: now, Changefreq: "monthly", Priority: 0.9, }) var articles []models.Article db.Where("published = ?", true).Order("id desc").Find(&articles) for i := range articles { items = append(items, sitemap.Item{ Loc: fmt.Sprintf("%s%s", domain, articles[i].URL()), LastMod: articles[i].UpdatedAt, Changefreq: "weekly", Priority: 0.9, }) } //Static pages var pages []models.Page db.Where("published = ?", true).Order("id desc").Find(&pages) for i := range pages { items = append(items, sitemap.Item{ Loc: fmt.Sprintf("%s%s", domain, pages[i].URL()), LastMod: pages[i].UpdatedAt, Changefreq: "monthly", Priority: 0.8, }) } //Reviews items = append(items, sitemap.Item{ Loc: fmt.Sprintf("%s%s", domain, "/reviews"), LastMod: now, Changefreq: "monthly", Priority: 0.7, }) var reviews []models.Review db.Where("published = ?", true).Order("id desc").Find(&reviews) for i := range reviews { items = append(items, sitemap.Item{ Loc: fmt.Sprintf("%s%s", domain, reviews[i].URL()), LastMod: reviews[i].UpdatedAt, Changefreq: "monthly", Priority: 0.7, }) } if err := sitemap.SiteMap(path.Join(folder, "sitemap1.xml.gz"), items); err != nil { log.Printf("ERROR: %s\n", err) return } if err := sitemap.SiteMapIndex(folder, "sitemap_index.xml", domain+"/public/sitemap/"); err != nil { log.Printf("ERROR: %s\n", err) return } log.Printf("INFO: XML sitemap has been generated in %s\n", folder) }