2023-06-11 00:32:02 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-06-17 04:08:50 +00:00
|
|
|
"net/url"
|
2023-06-11 00:32:02 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2023-06-30 19:06:46 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-06-11 00:32:02 +00:00
|
|
|
|
2023-11-25 02:17:09 +00:00
|
|
|
"gosimplenpm/internal/config"
|
|
|
|
"gosimplenpm/internal/storage"
|
2023-06-11 00:32:02 +00:00
|
|
|
)
|
|
|
|
|
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-11 00:32:02 +00:00
|
|
|
|
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-11 00:32:02 +00:00
|
|
|
|
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-11 00:32:02 +00:00
|
|
|
|
2023-06-30 19:06:46 +00:00
|
|
|
// serve file
|
|
|
|
http.ServeFile(w, r, fileToServe)
|
|
|
|
}
|
2023-06-11 00:32:02 +00:00
|
|
|
}
|