Switch API server to chi and add badge endpoint
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
This commit is contained in:
parent
d2fa149ff4
commit
3bc4b676d5
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,59 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "embed"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
|
"go.elara.ws/lure/internal/db"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed badge-logo.txt
|
||||||
|
var logoData string
|
||||||
|
|
||||||
|
var _ http.HandlerFunc
|
||||||
|
|
||||||
|
func handleBadge(gdb *sqlx.DB) http.HandlerFunc {
|
||||||
|
return func(res http.ResponseWriter, req *http.Request) {
|
||||||
|
repo := chi.URLParam(req, "repo")
|
||||||
|
name := chi.URLParam(req, "pkg")
|
||||||
|
|
||||||
|
pkg, err := db.GetPkg(gdb, "name = ? AND repository = ?", name, repo)
|
||||||
|
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 *db.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()
|
||||||
|
}
|
|
@ -30,6 +30,7 @@ import (
|
||||||
"go.elara.ws/logger/log"
|
"go.elara.ws/logger/log"
|
||||||
"go.elara.ws/lure/internal/api"
|
"go.elara.ws/lure/internal/api"
|
||||||
"go.elara.ws/lure/internal/repos"
|
"go.elara.ws/lure/internal/repos"
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -60,16 +61,16 @@ func main() {
|
||||||
|
|
||||||
sigCh := make(chan struct{}, 200)
|
sigCh := make(chan struct{}, 200)
|
||||||
go repoPullWorker(ctx, sigCh)
|
go repoPullWorker(ctx, sigCh)
|
||||||
|
|
||||||
var handler http.Handler
|
apiServer := api.NewAPIServer(
|
||||||
|
|
||||||
handler = api.NewAPIServer(
|
|
||||||
lureWebAPI{db: gdb},
|
lureWebAPI{db: gdb},
|
||||||
twirp.WithServerPathPrefix(""),
|
twirp.WithServerPathPrefix(""),
|
||||||
)
|
)
|
||||||
handler = withAcceptLanguage(handler)
|
|
||||||
handler = allowAllCORSHandler(handler)
|
r := chi.NewRouter()
|
||||||
handler = handleWebhook(handler, sigCh)
|
r.With(allowAllCORSHandler, withAcceptLanguage).Handle("/*", apiServer)
|
||||||
|
r.Post("/webhook", handleWebhook(sigCh))
|
||||||
|
r.Get("/badge/{repo}/{pkg}", handleBadge(gdb))
|
||||||
|
|
||||||
ln, err := net.Listen("tcp", *addr)
|
ln, err := net.Listen("tcp", *addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -78,7 +79,7 @@ func main() {
|
||||||
|
|
||||||
log.Info("Starting HTTP API server").Str("addr", ln.Addr().String()).Send()
|
log.Info("Starting HTTP API server").Str("addr", ln.Addr().String()).Send()
|
||||||
|
|
||||||
err = http.Serve(ln, handler)
|
err = http.Serve(ln, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error while running server").Err(err).Send()
|
log.Fatal("Error while running server").Err(err).Send()
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,30 +33,21 @@ import (
|
||||||
"go.elara.ws/lure/internal/repos"
|
"go.elara.ws/lure/internal/repos"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleWebhook(next http.Handler, sigCh chan<- struct{}) http.Handler {
|
func handleWebhook(sigCh chan<- struct{}) http.HandlerFunc {
|
||||||
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
if req.URL.Path == "/webhook" {
|
if req.Header.Get("X-GitHub-Event") != "push" {
|
||||||
if req.Method != http.MethodPost {
|
http.Error(res, "Only push events are accepted by this bot", http.StatusBadRequest)
|
||||||
res.WriteHeader(http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if req.Header.Get("X-GitHub-Event") != "push" {
|
|
||||||
http.Error(res, "Only push events are accepted by this bot", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err := verifySecure(req)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(res, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
sigCh <- struct{}{}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
next.ServeHTTP(res, req)
|
err := verifySecure(req)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(res, err.Error(), http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sigCh <- struct{}{}
|
||||||
|
return
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -9,6 +9,7 @@ require (
|
||||||
github.com/charmbracelet/bubbles v0.14.0
|
github.com/charmbracelet/bubbles v0.14.0
|
||||||
github.com/charmbracelet/bubbletea v0.23.1
|
github.com/charmbracelet/bubbletea v0.23.1
|
||||||
github.com/charmbracelet/lipgloss v0.6.0
|
github.com/charmbracelet/lipgloss v0.6.0
|
||||||
|
github.com/go-chi/chi/v5 v5.0.8
|
||||||
github.com/go-git/go-billy/v5 v5.3.1
|
github.com/go-git/go-billy/v5 v5.3.1
|
||||||
github.com/go-git/go-git/v5 v5.4.2
|
github.com/go-git/go-git/v5 v5.4.2
|
||||||
github.com/goreleaser/nfpm/v2 v2.20.0
|
github.com/goreleaser/nfpm/v2 v2.20.0
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -79,6 +79,8 @@ github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3
|
||||||
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
|
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
|
||||||
github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0=
|
github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0=
|
||||||
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
|
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
|
||||||
|
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
|
||||||
|
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
||||||
github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=
|
github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=
|
||||||
github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E=
|
github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E=
|
||||||
github.com/go-git/go-billy/v5 v5.2.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0=
|
github.com/go-git/go-billy/v5 v5.2.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0=
|
||||||
|
|
Loading…
Reference in New Issue