Skip to content

Commit

Permalink
added sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Sep 12, 2023
1 parent 121472a commit 669a242
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
60 changes: 60 additions & 0 deletions internal/pkg/handlers/sitemap.go
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()
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ func main() {
app.Get("/docs", handlers.RedirectToDocs)
app.Get("/documentation", handlers.RedirectToDocs)

// Sitemap
app.Get("/sitemap.xml", handlers.Sitemap)

// API
app.Get("/api/stats", handlers.AllStatsAPI)
app.Get("/api/stats/:user/:repo", handlers.RepoStatsAPI)
Expand Down
1 change: 1 addition & 0 deletions templates/lib/head.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>{{template "title" .}}</title>
<meta name="description" content="Instl is an installation script generator for GitHub projects. It does not need any setup, and can be used to install most GitHub projects on Linux, macOS and Windows.">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css">
<link rel="stylesheet" href="/css/global.css">
Expand Down
Empty file added templates/sitemap.gohtml
Empty file.

0 comments on commit 669a242

Please sign in to comment.