pull-through dist server

This commit is contained in:
Mikaël Cluseau
2025-07-08 22:20:26 +02:00
parent 08cbccc756
commit 899a0a9dab
7 changed files with 28 additions and 12 deletions

View File

@ -9,9 +9,11 @@ import (
"log"
"net/http"
"os"
"path"
gopath "path"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/dustin/go-humanize"
@ -22,8 +24,22 @@ var (
upstreamURL = flag.String("upstream", "https://dkl.novit.io/dist", "Upstream server for dist elements")
)
func (ctx *renderContext) distFetch(path ...string) (outPath string, err error) {
outPath = ctx.distFilePath(path...)
type upstreamServer struct{}
func (_ upstreamServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
path := path.Clean(req.URL.Path)
outPath, err := distFetch(strings.Split(path, "/")...)
if err != nil {
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte(err.Error() + "\n"))
return
}
http.ServeFile(w, req, outPath)
}
func distFetch(path ...string) (outPath string, err error) {
outPath = distFilePath(path...)
if _, err = os.Stat(outPath); err == nil {
return