Skip to content

Commit cf5356e

Browse files
Add sitemap
1 parent a9414cc commit cf5356e

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

apiserver/internal/server/server.go

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func (s *Server) Serve() error {
5050
apiV1.GET("/statusPages", s.statusPages)
5151
apiV1.GET("/statusPages/search", s.statusPageSearch)
5252
apiV1.GET("/statusPages/count", s.statusPageCount)
53+
apiV1.GET("/sitemap.xml", s.siteMap)
5354
}
5455
return errors.Wrap(r.Run(":80"), "Failed to start server")
5556
}

apiserver/internal/server/site_map.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package server
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"github.com/ikeikeikeike/go-sitemap-generator/v2/stm"
6+
"github.com/metoro-io/statusphere/common/api"
7+
"net/http"
8+
"net/url"
9+
)
10+
11+
func (s *Server) siteMap(context *gin.Context) {
12+
sm := stm.NewSitemap(1)
13+
sm.Create()
14+
pages := s.statusPageCache.Items()
15+
if len(pages) == 0 {
16+
s.logger.Warn("no status pages found")
17+
context.JSON(http.StatusInternalServerError, "no status pages found")
18+
return
19+
}
20+
for _, statusPage := range pages {
21+
page, ok := statusPage.Object.(api.StatusPage)
22+
if !ok {
23+
s.logger.Error("failed to cast status page")
24+
context.JSON(http.StatusInternalServerError, "failed to cast status page")
25+
return
26+
}
27+
escapeString := url.QueryEscape(page.Name)
28+
sm.Add(stm.URL{{"loc", "https://metoro.io/statusphere/status/" + escapeString}, {"changefreq", "always"}, {"mobile", true}, {"priority", 0.1}})
29+
}
30+
31+
content := sm.XMLContent()
32+
context.Data(http.StatusOK, "application/xml", []byte(content))
33+
}

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ go 1.22.1
55
require (
66
github.com/PuerkitoBio/goquery v1.9.1 // indirect
77
github.com/andybalholm/cascadia v1.3.2 // indirect
8+
github.com/beevik/etree v1.1.0 // indirect
89
github.com/bytedance/sonic v1.11.3 // indirect
910
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
1011
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
1112
github.com/chenzhuoyu/iasm v0.9.1 // indirect
1213
github.com/dghubble/go-twitter v0.0.0-20221104224141-912508c3888b // indirect
1314
github.com/dghubble/oauth1 v0.7.3 // indirect
1415
github.com/dghubble/sling v1.4.0 // indirect
16+
github.com/fatih/structs v1.1.0 // indirect
1517
github.com/g8rswimmer/go-twitter/v2 v2.1.5 // indirect
1618
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
1719
github.com/gin-contrib/cors v1.7.1 // indirect
@@ -23,6 +25,7 @@ require (
2325
github.com/go-playground/validator/v10 v10.19.0 // indirect
2426
github.com/goccy/go-json v0.10.2 // indirect
2527
github.com/google/go-querystring v1.1.0 // indirect
28+
github.com/ikeikeikeike/go-sitemap-generator/v2 v2.0.2 // indirect
2629
github.com/jackc/pgpassfile v1.0.0 // indirect
2730
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
2831
github.com/jackc/pgx/v5 v5.5.5 // indirect

go.sum

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/PuerkitoBio/goquery v1.9.1 h1:mTL6XjbJTZdpfL+Gwl5U2h1l9yEkJjhmlTeV9VP
22
github.com/PuerkitoBio/goquery v1.9.1/go.mod h1:cW1n6TmIMDoORQU5IU/P1T3tGFunOeXEpGP2WHRwkbY=
33
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
44
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
5+
github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs=
6+
github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A=
57
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
68
github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s=
79
github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
@@ -18,6 +20,8 @@ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpV
1820
github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
1921
github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0=
2022
github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
23+
github.com/clbanning/mxj v1.8.3 h1:2r/KCJi52w2MRz+K+UMa/1d7DdCjnLqYJfnbr7dYNWI=
24+
github.com/clbanning/mxj v1.8.3/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng=
2125
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2226
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2327
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -27,6 +31,8 @@ github.com/dghubble/oauth1 v0.7.3 h1:EkEM/zMDMp3zOsX2DC/ZQ2vnEX3ELK0/l9kb+vs4ptE
2731
github.com/dghubble/oauth1 v0.7.3/go.mod h1:oxTe+az9NSMIucDPDCCtzJGsPhciJV33xocHfcR2sVY=
2832
github.com/dghubble/sling v1.4.0 h1:/n8MRosVTthvMbwlNZgLx579OGVjUOy3GNEv5BIqAWY=
2933
github.com/dghubble/sling v1.4.0/go.mod h1:0r40aNsU9EdDUVBNhfCstAtFgutjgJGYbO1oNzkMoM8=
34+
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
35+
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
3036
github.com/g8rswimmer/go-twitter/v2 v2.1.5 h1:Uj9Yuof2UducrP4Xva7irnUJfB9354/VyUXKmc2D5gg=
3137
github.com/g8rswimmer/go-twitter/v2 v2.1.5/go.mod h1:/55xWb313KQs25X7oZrNSEwLQNkYHhPsDwFstc45vhc=
3238
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
@@ -57,6 +63,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
5763
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
5864
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
5965
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
66+
github.com/ikeikeikeike/go-sitemap-generator/v2 v2.0.2 h1:wIdDEle9HEy7vBPjC6oKz6ejs3Ut+jmsYvuOoAW2pSM=
67+
github.com/ikeikeikeike/go-sitemap-generator/v2 v2.0.2/go.mod h1:WtaVKD9TeruTED9ydiaOJU08qGoEPP/LyzTKiD3jEsw=
6068
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
6169
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
6270
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=

0 commit comments

Comments
 (0)