2019-10-07 18:21:26 +05:00
|
|
|
package serverv1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2019-10-07 20:39:11 +05:00
|
|
|
"strings"
|
2019-10-07 18:21:26 +05:00
|
|
|
|
2021-11-20 23:06:40 +05:00
|
|
|
"github.com/labstack/echo"
|
2019-10-13 15:12:54 +05:00
|
|
|
"go.dev.pztrn.name/giredore/internal/configuration"
|
|
|
|
"go.dev.pztrn.name/giredore/internal/structs"
|
2019-10-07 18:21:26 +05:00
|
|
|
)
|
|
|
|
|
|
|
|
// This function responsible for getting packages configuration.
|
2021-11-20 23:06:40 +05:00
|
|
|
func packagesGET(ectx echo.Context) error {
|
2022-06-29 15:04:57 +05:00
|
|
|
// nolint:exhaustruct
|
2019-10-07 18:21:26 +05:00
|
|
|
req := &structs.PackageGetRequest{}
|
2021-11-20 23:06:40 +05:00
|
|
|
if err := ectx.Bind(req); err != nil {
|
2019-10-07 18:21:26 +05:00
|
|
|
log.Error().Err(err).Msg("Failed to parse package get request")
|
2021-11-20 23:06:40 +05:00
|
|
|
|
2022-06-29 15:04:57 +05:00
|
|
|
// nolint:exhaustruct,wrapcheck
|
2021-11-20 23:06:40 +05:00
|
|
|
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingPackagesGetRequest}})
|
2019-10-07 18:21:26 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().Msgf("Received package(s) info get request: %+v", req)
|
|
|
|
|
|
|
|
var pkgs map[string]*structs.Package
|
2019-10-16 23:32:21 +05:00
|
|
|
|
2019-10-07 18:21:26 +05:00
|
|
|
var errors []structs.Error
|
2019-10-16 23:32:21 +05:00
|
|
|
|
2019-10-07 18:21:26 +05:00
|
|
|
if req.All {
|
|
|
|
pkgs = configuration.Cfg.GetAllPackagesInfo()
|
|
|
|
} else {
|
|
|
|
pkgs, errors = configuration.Cfg.GetPackagesInfo(req.PackageNames)
|
|
|
|
}
|
|
|
|
|
2019-10-07 21:51:45 +05:00
|
|
|
if len(errors) > 0 {
|
2021-11-20 23:06:40 +05:00
|
|
|
// nolint:wrapcheck
|
|
|
|
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errors, Data: pkgs})
|
2019-10-07 18:21:26 +05:00
|
|
|
}
|
|
|
|
|
2022-06-29 15:04:57 +05:00
|
|
|
// nolint:exhaustruct,wrapcheck
|
2021-11-20 23:06:40 +05:00
|
|
|
return ectx.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess, Data: pkgs})
|
2019-10-07 18:21:26 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function responsible for deleting package.
|
2021-11-20 23:06:40 +05:00
|
|
|
func packagesDELETE(ectx echo.Context) error {
|
2022-06-29 15:04:57 +05:00
|
|
|
// nolint:exhaustruct
|
2019-10-07 18:21:26 +05:00
|
|
|
req := &structs.PackageDeleteRequest{}
|
2021-11-20 23:06:40 +05:00
|
|
|
if err := ectx.Bind(req); err != nil {
|
2019-10-07 18:21:26 +05:00
|
|
|
log.Error().Err(err).Msg("Failed to parse package delete request")
|
2021-11-20 23:06:40 +05:00
|
|
|
|
2022-06-29 15:04:57 +05:00
|
|
|
// nolint:exhaustruct,wrapcheck
|
2021-11-20 23:06:40 +05:00
|
|
|
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingDeleteRequest}})
|
2019-10-07 18:21:26 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().Msgf("Received package delete request: %+v", req)
|
|
|
|
|
|
|
|
errs := configuration.Cfg.DeletePackage(req)
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
2022-06-29 15:04:57 +05:00
|
|
|
// nolint:exhaustruct,wrapcheck
|
2021-11-20 23:06:40 +05:00
|
|
|
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errs})
|
2019-10-07 18:21:26 +05:00
|
|
|
}
|
|
|
|
|
2022-06-29 15:04:57 +05:00
|
|
|
// nolint:exhaustruct,wrapcheck
|
2021-11-20 23:06:40 +05:00
|
|
|
return ectx.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess})
|
2019-10-07 18:21:26 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function responsible for setting or updating packages.
|
2021-11-20 23:06:40 +05:00
|
|
|
func packagesSET(ectx echo.Context) error {
|
2022-06-29 15:04:57 +05:00
|
|
|
// nolint:exhaustruct
|
2019-10-07 18:21:26 +05:00
|
|
|
req := &structs.Package{}
|
2021-11-20 23:06:40 +05:00
|
|
|
if err := ectx.Bind(req); err != nil {
|
2019-10-07 18:21:26 +05:00
|
|
|
log.Error().Err(err).Msg("Failed to parse package data")
|
2021-11-20 23:06:40 +05:00
|
|
|
|
|
|
|
// nolint:wrapcheck
|
|
|
|
return ectx.JSON(http.StatusBadRequest, nil)
|
2019-10-07 18:21:26 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().Msgf("Received package set/update request: %+v", req)
|
|
|
|
|
2019-10-07 20:39:11 +05:00
|
|
|
// Validate passed package data.
|
|
|
|
if !strings.HasPrefix(req.OriginalPath, "/") {
|
2022-06-29 15:04:57 +05:00
|
|
|
// nolint:exhaustruct,wrapcheck
|
2021-11-20 23:06:40 +05:00
|
|
|
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrPackageOrigPathShouldStartWithSlash}})
|
2019-10-07 20:39:11 +05:00
|
|
|
}
|
|
|
|
|
2019-10-07 18:21:26 +05:00
|
|
|
configuration.Cfg.AddOrUpdatePackage(req)
|
|
|
|
|
2022-06-29 15:04:57 +05:00
|
|
|
// nolint:exhaustruct,wrapcheck
|
2021-11-20 23:06:40 +05:00
|
|
|
return ectx.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess})
|
2019-10-07 18:21:26 +05:00
|
|
|
}
|