diff --git a/main.go b/main.go index 3ac8e47c..0688a4b1 100644 --- a/main.go +++ b/main.go @@ -48,39 +48,15 @@ func main() { l.Err(err).Send() return } - e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{ - Skipper: func(echo.Context) bool { - return false - }, - Format: "method=${method}, uri=${uri}, status=${status} latency=${latency}\n", - Output: os.Stdout, - })) - - e.Use(middleware.RateLimiterWithConfig(middleware.RateLimiterConfig{ - Skipper: middleware.DefaultSkipper, - BeforeFunc: nil, - IdentifierExtractor: func(ctx echo.Context) (string, error) { - return ctx.RealIP(), nil - }, - Store: middleware.NewRateLimiterMemoryStoreWithConfig(middleware.RateLimiterMemoryStoreConfig{ - Rate: 3, - Burst: 0, - ExpiresIn: time.Hour * 10, - }), - ErrorHandler: func(ctx echo.Context, err error) error { - return ctx.JSON(http.StatusForbidden, echo.Map{ - "error": "Too many requests, try after some time!", - }) - }, - })) - + e.Use(echoLogger()) e.Use(middleware.Recover()) - e.Use(middleware.CORS()) internal := e.Group("/internal") authRouter := e.Group("/auth") betaRouter := e.Group("/beta") + betaRouter.Use(rateLimiter()) + authRouter.Add(http.MethodPost, "/signup", authSvc.SignUp) authRouter.Add(http.MethodPost, "/signin", authSvc.SignIn) authRouter.Add(http.MethodPost, "/token", authSvc.SignIn) @@ -140,6 +116,9 @@ func main() { e.Add(http.MethodGet, "/v2/", reg.ApiVersion, BasicAuth(authSvc.BasicAuth)) + router.Add(http.MethodDelete, "/blobs/:digest", reg.DeleteLayer) + router.Add(http.MethodDelete, "/manifests/:digest", reg.DeleteImage) + log.Println(e.Start(cfg.Address())) } @@ -202,3 +181,30 @@ func BasicAuth(authfn func(string, string) (map[string]interface{}, error)) echo return true, nil }) } + +func echoLogger() echo.MiddlewareFunc { + return middleware.LoggerWithConfig(middleware.LoggerConfig{ + Skipper: func(echo.Context) bool { + return false + }, + Format: "method=${method}, uri=${uri}, status=${status} latency=${latency}\n", + Output: os.Stdout, + }) +} + +func rateLimiter() echo.MiddlewareFunc { + return middleware.RateLimiterWithConfig(middleware.RateLimiterConfig{ + Skipper: middleware.DefaultSkipper, + BeforeFunc: nil, + IdentifierExtractor: func(ctx echo.Context) (string, error) { + return ctx.RealIP(), nil + }, + Store: middleware.NewRateLimiterMemoryStoreWithConfig(middleware.RateLimiterMemoryStoreConfig{Rate: 3, Burst: 0, ExpiresIn: time.Hour * 10}), + ErrorHandler: func(ctx echo.Context, err error) error { + return ctx.JSON(http.StatusForbidden, echo.Map{"error": "Too many requests, try after some time!"}) + }, + DenyHandler: func(ctx echo.Context, identifier string, err error) error { + return ctx.JSON(http.StatusForbidden, echo.Map{"error": "Too many requests, try after some time!"}) + }, + }) +} diff --git a/registry/v2/registry.go b/registry/v2/registry.go index 42bb3283..ec455b06 100644 --- a/registry/v2/registry.go +++ b/registry/v2/registry.go @@ -12,7 +12,6 @@ import ( skynetsdk "github.com/NebulousLabs/go-skynet/v2" "github.com/docker/distribution/uuid" - "github.com/fatih/color" "github.com/jay-dee7/OpenRegistry/cache" "github.com/jay-dee7/OpenRegistry/skynet" "github.com/jay-dee7/OpenRegistry/types" @@ -103,6 +102,7 @@ func (r *registry) CompleteUpload(ctx echo.Context) error { buf := bytes.NewBuffer(r.b.uploads[uuid]) buf.Write(bz) ourHash := digest(buf.Bytes()) + delete(r.b.uploads, uuid) if ourHash != dig { details := map[string]interface{}{ @@ -221,7 +221,6 @@ func (r *registry) ManifestExists(ctx echo.Context) error { "clientDigest": ref, } r.debugf(details) - color.Magenta("digests do not match: %s - %s - %s\n", manifest.Reference, manifest.Digest, ref) errMsg := r.errorResponse(RegistryErrorCodeManifestInvalid, "manifest digest does not match", nil) return ctx.JSONBlob(http.StatusBadRequest, errMsg) } @@ -264,7 +263,6 @@ func (r *registry) PullManifest(ctx echo.Context) error { return ctx.JSON(http.StatusNotFound, errMsg) } - color.Magenta("skylink: %s - ref: %s namespace: %s\n", skynetLink, ref, namespace) resp, err := r.skynet.Download(skynetLink) if err != nil { errMsg := r.errorResponse(RegistryErrorCodeManifestInvalid, err.Error(), nil)