gosimplenpm/internal/handler/get.go

39 lines
879 B
Go
Raw Normal View History

package handler
import (
"fmt"
"net/http"
"net/url"
"github.com/gorilla/mux"
2023-06-30 19:06:46 +00:00
"github.com/sirupsen/logrus"
2023-11-25 02:17:09 +00:00
"gosimplenpm/internal/config"
"gosimplenpm/internal/storage"
)
2023-06-30 19:06:46 +00:00
func GetPackage(lg *logrus.Logger, cfg config.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
escapedName := mux.Vars(r)["name"]
packageName, _ := url.PathUnescape(escapedName)
lg.WithFields(logrus.Fields{
"function": "get-package",
}).Debugf("Package name => %s\n", packageName)
2023-06-30 19:06:46 +00:00
fileToServe, found, err := storage.GetIndexJsonFromStore(packageName, cfg.RepoDir, lg)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2023-06-30 19:06:46 +00:00
if !found {
ret := fmt.Sprintf("Package not found: %s", packageName)
http.Error(w, ret, http.StatusNotFound)
return
}
2023-06-30 19:06:46 +00:00
// serve file
http.ServeFile(w, r, fileToServe)
}
}