gosimplenpm/internal/handler/publish.go

178 lines
5.3 KiB
Go
Raw Permalink Normal View History

package handler
import (
"encoding/json"
"fmt"
2023-11-25 02:17:09 +00:00
"gosimplenpm/internal/config"
"gosimplenpm/internal/serviceidos"
"gosimplenpm/internal/storage"
"net/http"
"net/url"
"path"
2023-12-25 08:56:48 +00:00
"strconv"
"strings"
2023-12-25 08:56:48 +00:00
"time"
"github.com/gorilla/mux"
2023-06-30 19:06:46 +00:00
"github.com/sirupsen/logrus"
)
type NPMClientPutRequest struct {
Request serviceidos.IndexJson
}
2023-12-25 08:56:48 +00:00
func Publish(lg *logrus.Logger, cfg config.Config, stg storage.Storage) http.HandlerFunc {
2023-06-30 19:06:46 +00:00
return func(w http.ResponseWriter, r *http.Request) {
// (1) Parse Json Body
// (2) Check if package exists in the folder.
// (a) if it does, ckeck if it is the same version. If it is, return error. Else modify index.json from (2)
// (b) If it does not, add the latest tag to the new index.json
2023-06-30 19:06:46 +00:00
escapedName := mux.Vars(r)["name"]
packageName, _ := url.PathUnescape(escapedName)
lg.WithFields(logrus.Fields{
"function": "publish",
}).Debugf("Package name => %s\n", packageName)
2023-06-30 19:06:46 +00:00
var cr NPMClientPutRequest
// Parse json body
err := json.NewDecoder(r.Body).Decode(&cr.Request)
if err != nil {
2023-06-30 19:06:46 +00:00
lg.WithFields(logrus.Fields{
"function": "publish",
}).Debugf("Error unmarshaling put request: %+v\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2023-06-30 19:06:46 +00:00
// Extract relevant data from index.json
2023-12-25 08:56:48 +00:00
fmt.Printf("cRequest => %+v\n", cr)
2023-06-30 19:06:46 +00:00
index := 0
var tag string
var version string
var versionData serviceidos.IndexJsonVersions
// TODO: Fix this as the order is not guaranteed
for key, value := range cr.Request.DistTags {
if index == 0 {
tag = key
version = value
break
}
index++
}
versionData = cr.Request.Versions[version]
2023-12-25 08:56:48 +00:00
2023-06-30 19:06:46 +00:00
lg.WithFields(logrus.Fields{
"function": "publish",
}).Debugf("For version(%s) with tag(%s), versionData => %+v\n", version, tag, versionData)
// Rewrite the tarball path
tarballFileName := strings.Split(versionData.Dist.Tarball, "/-/")[1]
2023-12-25 08:56:48 +00:00
tarballFileName, _ = url.PathUnescape(tarballFileName)
2023-06-30 19:06:46 +00:00
lg.WithFields(logrus.Fields{
"function": "publish",
}).Debugf("TarballName => %s\n", tarballFileName)
// versionData.Dist.Tarball = fmt.Sprintf("file://%s", packageFilePath)
versionData.Dist.Tarball = fmt.Sprintf("http://%s/%s/-/%s", r.Host, url.PathEscape(packageName), url.PathEscape(tarballFileName))
2023-12-25 08:56:48 +00:00
2023-06-30 19:06:46 +00:00
lg.WithFields(logrus.Fields{
"function": "publish",
}).Debugf("versionData.Dist.Tarball => %s\n", versionData.Dist.Tarball)
tarBallFile := strings.Split(tarballFileName, "/")[1]
packageFilePath := path.Join(cfg.RepoDir, packageName, tarBallFile)
2023-12-25 08:56:48 +00:00
lg.WithFields(logrus.Fields{
"function": "publish",
}).Debugf("PackageFilePath => %s\n", packageFilePath)
fmt.Printf("PackageFilePath => %s\n", packageFilePath)
2023-06-30 19:06:46 +00:00
// Try to get the index.json from the store
2023-12-25 08:56:48 +00:00
fileToServe, found, err := stg.GetIndexJsonFromStore(packageName, cfg.RepoDir, lg)
2023-06-30 19:06:46 +00:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2023-06-30 19:06:46 +00:00
var jsonFile serviceidos.IndexJson
if !found {
// new package
jsonFile = cr.Request
jsonFile.DistTags["latest"] = version
2023-12-25 08:56:48 +00:00
curTime := time.Now().Format(time.RFC3339)
jsonFile.TimesPackage = map[string]string{
version: curTime,
"created": curTime,
"modified": curTime,
"unpublished": "",
}
2023-06-30 19:06:46 +00:00
} else {
// old package
2023-12-25 08:56:48 +00:00
err = stg.ReadIndexJson(fileToServe, &jsonFile, lg)
2023-06-30 19:06:46 +00:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Checking that you are not publishing over a pervious published version
if jsonFile.Versions[version].Version == version {
lg.WithFields(logrus.Fields{
"function": "publish",
}).Debugf("Version %s of package %s already exists!!\n", version, packageName)
2023-12-25 08:56:48 +00:00
http.Error(w, fmt.Sprintf("Version %s of package %s already exists!!\n", version, packageName), http.StatusBadRequest)
2023-06-30 19:06:46 +00:00
return
}
// Rewrite attachments
jsonFile.DistTags[tag] = version
nAttachments := make(map[string]serviceidos.IndexJsonAttachments)
nAttachments[fmt.Sprintf("%s-%s.tgz", packageName, version)] = cr.Request.Attachments[fmt.Sprintf("%s-%s.tgz", packageName, version)]
jsonFile.Attachments = nAttachments
// Merge in the new version data
jsonFile.Versions[version] = versionData
2023-12-25 08:56:48 +00:00
// Update the time field
timesPackages := jsonFile.TimesPackage
curTime := time.Now().Format(time.RFC3339)
timesPackages["modified"] = curTime
timesPackages[version] = curTime
2023-06-30 19:06:46 +00:00
}
2023-06-30 19:06:46 +00:00
lg.WithFields(logrus.Fields{
"function": "publish",
}).Debugln("FiletoServe ==> ", fileToServe)
2023-06-30 19:06:46 +00:00
// Write index.json
2023-12-25 08:56:48 +00:00
err = stg.WriteIndexJson(fileToServe, &jsonFile, lg)
2023-06-30 19:06:46 +00:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2023-06-30 19:06:46 +00:00
lg.WithFields(logrus.Fields{
"function": "publish",
}).Debugln("Package path => ", packageFilePath)
// Write bundled package
packageData := jsonFile.Attachments[fmt.Sprintf("%s-%s.tgz", packageName, version)].Data
2023-12-25 08:56:48 +00:00
err = stg.WritePackageToStore(packageFilePath, packageData, lg)
2023-06-30 19:06:46 +00:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2023-12-25 08:56:48 +00:00
response := serviceidos.PublishPutResponse{
Ok: true,
Name: packageName,
}
jsonString, _ := json.Marshal(response)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.Header().Set("Content-Length", strconv.Itoa(len(jsonString)))
w.Write(jsonString)
}
}