feat: mem, local uploader
This commit is contained in:
55
internal/util/rerr/rerr.go
Normal file
55
internal/util/rerr/rerr.go
Normal file
@ -0,0 +1,55 @@
|
||||
package rerr
|
||||
|
||||
import (
|
||||
"github.com/loveuer/nf"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type RepositoryError struct {
|
||||
Status int
|
||||
Code string
|
||||
Message string
|
||||
}
|
||||
|
||||
func Error(c *nf.Ctx, err *RepositoryError) error {
|
||||
return c.Status(err.Status).JSON(nf.Map{
|
||||
"errors": []nf.Map{
|
||||
{
|
||||
"code": err.Code,
|
||||
"message": err.Message,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func ErrInternal(err error) *RepositoryError {
|
||||
return &RepositoryError{
|
||||
Status: http.StatusInternalServerError,
|
||||
Code: "INTERNAL_SERVER_ERROR",
|
||||
Message: err.Error(),
|
||||
}
|
||||
}
|
||||
|
||||
var ErrBlobUnknown = &RepositoryError{
|
||||
Status: http.StatusNotFound,
|
||||
Code: "BLOB_UNKNOWN",
|
||||
Message: "Unknown blob",
|
||||
}
|
||||
|
||||
var ErrUnsupported = &RepositoryError{
|
||||
Status: http.StatusMethodNotAllowed,
|
||||
Code: "UNSUPPORTED",
|
||||
Message: "Unsupported operation",
|
||||
}
|
||||
|
||||
var ErrDigestMismatch = &RepositoryError{
|
||||
Status: http.StatusBadRequest,
|
||||
Code: "DIGEST_INVALID",
|
||||
Message: "digest does not match contents",
|
||||
}
|
||||
|
||||
var ErrDigestInvalid = &RepositoryError{
|
||||
Status: http.StatusBadRequest,
|
||||
Code: "NAME_INVALID",
|
||||
Message: "invalid digest",
|
||||
}
|
22
internal/util/tools/ctx.go
Normal file
22
internal/util/tools/ctx.go
Normal file
@ -0,0 +1,22 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Timeout(seconds ...int) (ctx context.Context) {
|
||||
var (
|
||||
duration time.Duration
|
||||
)
|
||||
|
||||
if len(seconds) > 0 && seconds[0] > 0 {
|
||||
duration = time.Duration(seconds[0]) * time.Second
|
||||
} else {
|
||||
duration = time.Duration(30) * time.Second
|
||||
}
|
||||
|
||||
ctx, _ = context.WithTimeout(context.Background(), duration)
|
||||
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user