Files
portainer/api/http/registryproxy/proxy.go
Chaim Lev-Ari 92872435c4 feat(registry): integrate RM extension (#4)
* refactor(registries): move to portainer

* feat(registries): show browse link

* feat(registry): move registry extension code

* fix(registry): revert files

* refactor(registry): use component

* refactor(registry): replace $scope with this

* refactor(registry): use async await

* refactor(registry): rename and extract

* refactor(registry): rename progression-modal files

* refactor(registry): replace view with component

* refactor(registry): replace with component

* style(regirstries): sort handler keys

* feat(registry): force the recreation of a proxy client

* fix(registry): ignore 404 tags
2020-09-08 19:35:29 +12:00

58 lines
1.4 KiB
Go

package registryproxy
import (
"net/http"
"strings"
"github.com/orcaman/concurrent-map"
portainer "github.com/portainer/portainer/api"
)
// Service represents a service used to manage registry proxies.
type Service struct {
proxies cmap.ConcurrentMap
}
// NewService returns a pointer to a Service.
func NewService() *Service {
return &Service{
proxies: cmap.New(),
}
}
// GetProxy returns the registry proxy associated to a key if it exists.
// Otherwise, it will create it and return it.
func (service *Service) GetProxy(key, uri string, config *portainer.RegistryManagementConfiguration, forceCreate bool) (http.Handler, error) {
proxy, ok := service.proxies.Get(key)
if ok && !forceCreate {
return proxy.(http.Handler), nil
}
return service.createProxy(key, uri, config)
}
func (service *Service) createProxy(key, uri string, config *portainer.RegistryManagementConfiguration) (http.Handler, error) {
var proxy http.Handler
var err error
switch config.Type {
case portainer.AzureRegistry:
proxy, err = newTokenSecuredRegistryProxy(uri, config)
case portainer.GitlabRegistry:
if strings.Contains(key, "gitlab") {
proxy, err = newGitlabRegistryProxy(uri, config)
} else {
proxy, err = newTokenSecuredRegistryProxy(uri, config)
}
default:
proxy, err = newCustomRegistryProxy(uri, config)
}
if err != nil {
return nil, err
}
service.proxies.Set(key, proxy)
return proxy, nil
}