gosimplenpm/internal/handler/get.go

39 lines
896 B
Go

package handler
import (
"fmt"
"net/http"
"net/url"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
"gosimplenpm/internal/config"
"gosimplenpm/internal/storage"
)
func GetPackage(lg *logrus.Logger, cfg config.Config, stg storage.Storage) 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)
fileToServe, found, err := stg.GetIndexJsonFromStore(packageName, cfg.RepoDir, lg)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if !found {
ret := fmt.Sprintf("Package not found: %s", packageName)
http.Error(w, ret, http.StatusNotFound)
return
}
// serve file
http.ServeFile(w, r, fileToServe)
}
}