59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
_ "embed"
|
||
|
"net/http"
|
||
|
"net/url"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/go-chi/chi/v5"
|
||
|
"go.elara.ws/lure/pkg/search"
|
||
|
)
|
||
|
|
||
|
//go:embed badge-logo.txt
|
||
|
var logoData string
|
||
|
|
||
|
var _ http.HandlerFunc
|
||
|
|
||
|
func handleBadge() http.HandlerFunc {
|
||
|
return func(res http.ResponseWriter, req *http.Request) {
|
||
|
repo := chi.URLParam(req, "repo")
|
||
|
name := chi.URLParam(req, "pkg")
|
||
|
|
||
|
pkg, err := search.GetPkg(repo, name)
|
||
|
if err != nil {
|
||
|
http.Error(res, err.Error(), http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
http.Redirect(res, req, genBadgeURL(pkg.Name, genVersion(pkg)), http.StatusFound)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func genVersion(pkg search.Package) string {
|
||
|
sb := strings.Builder{}
|
||
|
if pkg.Epoch != 0 {
|
||
|
sb.WriteString(strconv.Itoa(int(pkg.Epoch)))
|
||
|
sb.WriteByte(':')
|
||
|
}
|
||
|
|
||
|
sb.WriteString(pkg.Version)
|
||
|
|
||
|
if pkg.Release != 0 {
|
||
|
sb.WriteByte('-')
|
||
|
sb.WriteString(strconv.Itoa(pkg.Release))
|
||
|
}
|
||
|
return sb.String()
|
||
|
}
|
||
|
|
||
|
func genBadgeURL(pkgName, pkgVersion string) string {
|
||
|
v := url.Values{}
|
||
|
v.Set("label", pkgName)
|
||
|
v.Set("message", pkgVersion)
|
||
|
v.Set("logo", logoData)
|
||
|
v.Set("color", "blue")
|
||
|
u := &url.URL{Scheme: "https", Host: "img.shields.io", Path: "/static/v1", RawQuery: v.Encode()}
|
||
|
return u.String()
|
||
|
}
|