39 lines
861 B
Go
39 lines
861 B
Go
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"gosimplenpm/config"
|
|
"gosimplenpm/storage"
|
|
)
|
|
|
|
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)
|
|
|
|
fileToServe, found, err := storage.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)
|
|
}
|
|
}
|