Giredore is now serving go-imports.

This commit is contained in:
Stanislav Nikitin 2019-10-07 20:39:11 +05:00
parent b8e02d48ab
commit 49f4078c8d
No known key found for this signature in database
GPG Key ID: 106900B32F8192EE
4 changed files with 67 additions and 7 deletions

View File

@ -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)
}

View File

@ -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})

View File

@ -0,0 +1,18 @@
package serverv1
const (
singlePackageTemplate string = `<!doctype html>
<html>
<head>
<meta name="go-import" content="{PKGNAME} {VCS} {REPOPATH}">
</head>
<body>
go get {PKGNAME}
</body>
</html>
`
)
// This might be added. ToDo: figure out why this is needed.
// <meta name="go-source" content="{PKGNAME} _ https://sources.dev.pztrn.name/pztrn/giredore/src/branch/master{/dir} https://sources.dev.pztrn.name/pztrn/giredore/src/branch/master{/dir}/{file}#L{line}">

View File

@ -3,6 +3,8 @@ package structs
const (
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"