-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
121472a
commit 669a242
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package handlers | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gofiber/fiber/v2" | ||
"go.etcd.io/bbolt" | ||
"strings" | ||
) | ||
|
||
func Sitemap(c *fiber.Ctx) error { | ||
var pages []string | ||
|
||
pages = append(pages, "https://instl.sh/") | ||
pages = append(pages, "https://instl.sh/stats") | ||
|
||
var statsMap = map[string]struct{}{} | ||
err := db.View(func(tx *bbolt.Tx) error { | ||
b := tx.Bucket([]byte("installations")) | ||
if b == nil { | ||
return nil | ||
} | ||
return b.ForEach(func(k, v []byte) error { | ||
var repo string | ||
parts := strings.Split(string(k), "/") | ||
repo = strings.Join(parts[:2], "/") | ||
statsMap[repo] = struct{}{} | ||
return nil | ||
}) | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for repo := range statsMap { | ||
pages = append(pages, fmt.Sprintf("https://instl.sh/%s", repo)) | ||
} | ||
|
||
c.Set("Content-Type", "application/xml") | ||
return c.SendString(generateSitemap(pages)) | ||
} | ||
|
||
func generateSitemap(urls []string) string { | ||
var sitemap strings.Builder | ||
|
||
sitemap.WriteString(`<?xml version="1.0" encoding="UTF-8"?>`) | ||
sitemap.WriteString("\n") | ||
sitemap.WriteString(`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`) | ||
sitemap.WriteString("\n") | ||
for _, url := range urls { | ||
sitemap.WriteString(" <url>") | ||
sitemap.WriteString("\n") | ||
sitemap.WriteString(fmt.Sprintf(" <loc>%s</loc>", url)) | ||
sitemap.WriteString("\n") | ||
sitemap.WriteString(" </url>") | ||
sitemap.WriteString("\n") | ||
} | ||
sitemap.WriteString(`</urlset>`) | ||
|
||
return sitemap.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.