From 16e60f38f7789a29771fb0a1e0865abe047b183d Mon Sep 17 00:00:00 2001 From: Mya Pitzeruse Date: Thu, 20 Oct 2022 12:21:38 -0500 Subject: [PATCH] feat: support custom go modules by peaking at the request path when the go-get query parameter is sent --- internal/commands/host.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/internal/commands/host.go b/internal/commands/host.go index 8616cef..2bfaeb0 100644 --- a/internal/commands/host.go +++ b/internal/commands/host.go @@ -20,6 +20,7 @@ import ( "context" "mime" "net/http" + "path" "time" "github.com/urfave/cli/v2" @@ -90,7 +91,34 @@ var ( Methods(http.MethodPost) } - server.PublicMux.PathPrefix("/").Handler(http.FileServer(git.HTTP(gitService.FS))).Methods(http.MethodGet) + httpfs := http.FileServer(git.HTTP(gitService.FS)) + server.PublicMux.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + values := r.URL.Query() + + if values.Get("go-get") == "1" { + // peak for go-get requests + _, name := path.Split(r.URL.Path) + index := path.Join(r.URL.Path, "index.html") + + // if index.html exists, then use that + info, err := gitService.FS.Stat(index) + if err == nil { + file, err := gitService.FS.Open(index) + if err != nil { + http.Error(w, "", http.StatusInternalServerError) + return + } + + defer file.Close() + + http.ServeContent(w, r, name, info.ModTime(), file) + return + } + } + + httpfs.ServeHTTP(w, r) + return + }).Methods(http.MethodGet) log.Info("serving", zap.String("public", hostConfig.Public.Address),