github.com/ngocphuongnb/tetua@v0.0.7-alpha/app/web/sitemap/topic.go (about)

     1  package websitemap
     2  
     3  import (
     4  	"encoding/xml"
     5  	"net/http"
     6  	"time"
     7  
     8  	"github.com/ngocphuongnb/tetua/app/cache"
     9  	"github.com/ngocphuongnb/tetua/app/server"
    10  )
    11  
    12  func Topic(c server.Context) error {
    13  	sitemapTopic := SitemapUrlSets{
    14  		Xmlns:               "http://www.sitemaps.org/schemas/sitemap/0.9",
    15  		XmlnsXsi:            "http://www.w3.org/2001/XMLSchema-instance",
    16  		XmlnsImage:          "http://www.google.com/schemas/sitemap-image/1.1",
    17  		XmlnsSchemaLocation: "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd",
    18  	}
    19  
    20  	for _, topic := range cache.Topics {
    21  		sitemapTopic.Urls = append(sitemapTopic.Urls, &SitemapUrl{
    22  			Loc: &SitemapLoc{
    23  				Value: topic.Url(),
    24  			},
    25  			LastMod: &SitemapLastMod{
    26  				Value: topic.UpdatedAt.Format(time.RFC3339),
    27  			},
    28  		})
    29  	}
    30  
    31  	sitemapBytes, err := xml.Marshal(sitemapTopic)
    32  
    33  	if err != nil {
    34  		c.Logger().Error(err)
    35  		return c.Status(http.StatusInternalServerError).SendString("Error")
    36  	}
    37  
    38  	c.Response().Header("content-type", "text/xml; charset=UTF-8")
    39  	return c.SendString(`<?xml version="1.0" encoding="UTF-8"?>` + string(sitemapBytes))
    40  }