Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 0 additions & 42 deletions internal/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/jgndev/jgn.dev/internal/contentmanager"
"github.com/jgndev/jgn.dev/internal/site"
"github.com/labstack/echo/v4"
)

// Application represents the core structure of the application, managing blog post and cheatsheet content through dedicated managers.
Expand Down Expand Up @@ -52,44 +51,3 @@ func New() *Application {
CheatsheetManager: csm,
}
}

// SitemapXML generates an XML sitemap containing all posts and cheatsheets, and writes it to the HTTP response as XML.
func (app *Application) SitemapXML(c echo.Context) error {
posts := app.ContentManager.GetAll()
cheatsheets := app.CheatsheetManager.GetAll()

type urlEntry struct {
Loc string
LastMod string
}

var urls []urlEntry

// Add posts
for _, post := range posts {
urls = append(urls, urlEntry{
Loc: site.URL + "/posts/" + post.Slug,
LastMod: post.Date.Format("2006-01-02"),
})
}
// Add cheatsheets
for _, cs := range cheatsheets {
urls = append(urls, urlEntry{
Loc: site.URL + "/cheatsheets/" + cs.Slug,
LastMod: cs.Date.Format("2006-01-02"),
})
}

xml := `<?xml version="1.0" encoding="UTF-8"?>\n` +
`<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n`
for _, u := range urls {
xml += " <url>\n"
xml += " <loc>" + u.Loc + "</loc>\n"
xml += " <lastmod>" + u.LastMod + "</lastmod>\n"
xml += " </url>\n"
}
xml += "</urlset>\n"

c.Response().Header().Set("Content-Type", "application/xml")
return c.String(200, xml)
}
48 changes: 48 additions & 0 deletions internal/application/sitemap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package application

import (
"github.com/jgndev/jgn.dev/internal/site"
"github.com/labstack/echo/v4"
)

// SitemapXML generates an XML sitemap containing all posts and cheatsheets and writes it to the HTTP response as XML.
func (app *Application) SitemapXML(c echo.Context) error {
posts := app.ContentManager.GetAll()
cheatsheets := app.CheatsheetManager.GetAll()

type urlEntry struct {
Loc string
LastMod string
}

var urls []urlEntry

// Add posts
for _, post := range posts {
urls = append(urls, urlEntry{
Loc: site.URL + "/posts/" + post.Slug,
LastMod: post.Date.Format("2006-01-02"),
})
}
// Add cheatsheets
for _, cs := range cheatsheets {
urls = append(urls, urlEntry{
Loc: site.URL + "/cheatsheets/" + cs.Slug,
LastMod: cs.Date.Format("2006-01-02"),
})
}

xml := `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
`
for _, u := range urls {
xml += " <url>\n"
xml += " <loc>" + u.Loc + "</loc>\n"
xml += " <lastmod>" + u.LastMod + "</lastmod>\n"
xml += " </url>\n"
}
xml += "</urlset>"

c.Response().Header().Set("Content-Type", "application/xml")
return c.String(200, xml)
}