diff --git a/domains/server/v1/goimports.go b/domains/server/v1/goimports.go index c426489..6f2faaf 100644 --- a/domains/server/v1/goimports.go +++ b/domains/server/v1/goimports.go @@ -3,11 +3,45 @@ package serverv1 import ( // stdlib "net/http" + "strings" + + // local + "sources.dev.pztrn.name/pztrn/giredore/internal/configuration" + "sources.dev.pztrn.name/pztrn/giredore/internal/structs" // other "github.com/labstack/echo" ) func throwGoImports(ec echo.Context) error { - return ec.String(http.StatusOK, "All OK here") + // Getting real path. This might be the package itself, or namespace + // to list available packages. + // For now only package itself is supported, all other features in ToDo. + packageNameRaw := ec.Request().URL.Path + pkgs, errs := configuration.Cfg.GetPackagesInfo([]string{packageNameRaw}) + if errs != nil { + log.Error().Str("package", packageNameRaw).Msgf("Failed to get package information: %+v", errs) + return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errs}) + } + + if len(pkgs) == 0 { + return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrNoPackagesFound}}) + } + + pkg, found := pkgs[packageNameRaw] + if !found { + return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrNoPackagesFound}}) + } + + // We should compose package name using our domain under which giredore + // is working. + domain := ec.Request().Host + packageName := domain + packageNameRaw + + tmpl := singlePackageTemplate + tmpl = strings.Replace(tmpl, "{PKGNAME}", packageName, -1) + tmpl = strings.Replace(tmpl, "{VCS}", pkg.VCS, 1) + tmpl = strings.Replace(tmpl, "{REPOPATH}", pkg.RealPath, 1) + + return ec.HTML(http.StatusOK, tmpl) } diff --git a/domains/server/v1/packagesapi.go b/domains/server/v1/packagesapi.go index eaf8273..0765660 100644 --- a/domains/server/v1/packagesapi.go +++ b/domains/server/v1/packagesapi.go @@ -3,6 +3,7 @@ package serverv1 import ( // stdlib "net/http" + "strings" // local "sources.dev.pztrn.name/pztrn/giredore/internal/configuration" @@ -66,6 +67,11 @@ func packagesSET(ec echo.Context) error { log.Info().Msgf("Received package set/update request: %+v", req) + // Validate passed package data. + if !strings.HasPrefix(req.OriginalPath, "/") { + return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrPackageOrigPathShouldStartWithSlash}}) + } + configuration.Cfg.AddOrUpdatePackage(req) return ec.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess}) diff --git a/domains/server/v1/templates.go b/domains/server/v1/templates.go new file mode 100644 index 0000000..4ec73d1 --- /dev/null +++ b/domains/server/v1/templates.go @@ -0,0 +1,18 @@ +package serverv1 + +const ( + singlePackageTemplate string = ` + +
+ + + + + go get {PKGNAME} + + +` +) + +// This might be added. ToDo: figure out why this is needed. +// diff --git a/internal/structs/error.go b/internal/structs/error.go index 56aff11..9deda47 100644 --- a/internal/structs/error.go +++ b/internal/structs/error.go @@ -1,12 +1,14 @@ package structs const ( - ErrInvalidAllowedIPDefined Error = "Invalid allowed IP address defined." - ErrIPAddressNotAllowed Error = "IP address not allowed to access configuration API." - ErrPackageWasntDefined Error = "Passed package wasn't defined." - ErrParsingAllowedIPsSetRequest Error = "Error parsing allowed IPs request." - ErrParsingDeleteRequest Error = "Delete request parsing failed" - ErrParsingPackagesGetRequest Error = "Error parsing package(s) info get request" + ErrInvalidAllowedIPDefined Error = "Invalid allowed IP address defined." + ErrIPAddressNotAllowed Error = "IP address not allowed to access configuration API." + ErrNoPackagesFound Error = "No packages found." + ErrPackageOrigPathShouldStartWithSlash Error = "Package's original name (path) should start with slash." + ErrPackageWasntDefined Error = "Passed package wasn't defined." + ErrParsingAllowedIPsSetRequest Error = "Error parsing allowed IPs request." + ErrParsingDeleteRequest Error = "Delete request parsing failed" + ErrParsingPackagesGetRequest Error = "Error parsing package(s) info get request" ) type Error string