生成sitemap.xml示范demo
package services
import (
"encoding/xml"
"fmt"
"log"
"os"
"github.com/gin-gonic/gin"
"time"
model "btcinfo/models"
"context"
"math"
"go.mongodb.org/mongo-driver/bson"
)
type URL struct {
XMLName xml.Name `xml:"url"`
Loc string `xml:"loc"`
LastMod string `xml:"lastmod,omitempty"`
ChangeFreq string `xml:"changefreq,omitempty"`
Priority float64 `xml:"priority,omitempty"`
}
type Sitemap struct {
XMLName xml.Name `xml:"urlset"`
Xmlns string `xml:"xmlns,attr"`
URLs []URL `xml:"url"`
}
func UpSitemap(c *gin.Context) {
var Urls []URL
log.Print("sitemap运行")
for _, lang := range Langs {
index := URL{
Loc: "https://info.com/"+lang,
LastMod: "2024-03-28",
ChangeFreq: "yearly",
Priority: 0.6,
}
Urls = append(Urls, index)
filter := bson.D{{Key: "dalei", Value: "faq"},{Key: "lang", Value: lang}}
zongtiaoshu, _ := model.FaqModel.CountDocuments(context.TODO(), filter)
zongyeshu := int64(math.Ceil(float64(zongtiaoshu) / 12))
log.Print("faq总页数",zongyeshu)
for i := int64(0); i < zongyeshu; i++ {
log.Print("循环",i+1)
lists1 := URL{
Loc: "https://info.com/"+lang+"/lists/faq/" +fmt.Sprint((i+1)),
LastMod: "2024-03-28",
ChangeFreq: "weekly",
Priority: 0.3,
}
Urls = append(Urls, lists1)
}
filter = bson.D{{Key: "dalei", Value: "article"},{Key: "lang", Value: lang}}
zongtiaoshu, _ = model.ArticleModel.CountDocuments(context.TODO(), filter)
zongyeshu = int64(math.Ceil(float64(zongtiaoshu) / 12))
log.Print("文章总页数",zongyeshu)
for i := int64(0); i < zongyeshu; i++ {
log.Print("循环",i+1)
lists1 := URL{
Loc: "https://info.com/"+lang+"/lists/article/" +fmt.Sprint((i+1)),
LastMod: "2024-03-28",
ChangeFreq: "weekly",
Priority: 0.3,
}
Urls = append(Urls, lists1)
}
var results []model.Article_modelcha
filter = bson.D{{Key: "lang", Value: lang}}
cursor, err := model.FaqModel.Find(context.TODO(), filter)
if err != nil {
c.JSON(400, gin.H{"db_error": "FAQ表查询错误"})
log.Print("faq表查询错误:", err)
return
}
if err = cursor.All(context.TODO(), &results); err != nil {
c.JSON(500, gin.H{"db_error": err})
log.Print("路由查询解码错误:", err)
return
}
for _, r := range results {
var date time.Time
if r.Update != (time.Time{}) {
date = r.Update
} else {
date = r.CreatedAt
}
list := URL{
Loc: "https://info.com/" + lang + "/article/faq/" + r.ID.Hex(),
LastMod: date.Format("2006-01-02"),
ChangeFreq: "monthly",
Priority: 0.7,
}
Urls = append(Urls, list)
}
cursor, err = model.ArticleModel.Find(context.TODO(), filter)
if err != nil {
c.JSON(400, gin.H{"db_error": "FAQ表查询错误"})
log.Print("faq表查询错误:", err)
return
}
if err = cursor.All(context.TODO(), &results); err != nil {
c.JSON(500, gin.H{"db_error": err})
log.Print("路由查询解码错误:", err)
return
}
for _, r := range results {
var date time.Time
if r.Update != (time.Time{}) {
date = r.Update
} else {
date = r.CreatedAt
}
list := URL{
Loc: "https://info.com/" + lang + "/article/article/" + r.ID.Hex(),
LastMod: date.Format("2006-01-02"),
ChangeFreq: "monthly",
Priority: 0.8,
}
Urls = append(Urls, list)
}
}
sitemap := Sitemap{
Xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9",
URLs: Urls,
}
file, err := os.Create("jingtai/sitemap.xml")
if err != nil {
log.Println("Error creating file:", err)
return
}
defer file.Close()
encoder := xml.NewEncoder(file)
encoder.Indent("", " ")
if err := encoder.Encode(sitemap); err != nil {
log.Println("写入Sitemap.xml失败:", err)
return
}
log.Println("Sitemap.xml更新完成!")
}