Skip to content

Commit

Permalink
serve repositories as subdomains if domain is defined
Browse files Browse the repository at this point in the history
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
  • Loading branch information
Adphi committed Oct 3, 2023
1 parent 646e535 commit c89b776
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
6 changes: 5 additions & 1 deletion cmd/artifact-registry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ const (
EnvAddr = "ARTIFACT_REGISTRY_ADDRESS"
EnvBackend = "ARTIFACT_REGISTRY_BACKEND"
EnvKey = "ARTIFACT_REGISTRY_KEY"
EnvDomain = "ARTIFACT_REGISTRY_DOMAIN"
)

var (
addr = ":9887"
// backend = "192.168.10.11:5000"
backend = "docker.io"

domain = ""

key = ""

cmd = &cobra.Command{
Expand All @@ -57,6 +60,7 @@ func main() {
cmd.Flags().StringVar(&addr, "addr", envDefault(EnvAddr, addr), "address to listen on [$"+EnvAddr+"]")
cmd.Flags().StringVar(&backend, "backend", envDefault(EnvBackend, backend), "registry backend [$"+EnvBackend+"]")
cmd.Flags().StringVar(&key, "key", envDefault(EnvKey, key), "key to encrypt the repositories keys [$"+EnvKey+"]")
cmd.Flags().StringVar(&domain, "domain", envDefault(EnvDomain, domain), "domain to use to serve the repositories as subdomains [$"+EnvDomain+"]")
if err := cmd.Execute(); err != nil {
logrus.Fatal(err)
}
Expand Down Expand Up @@ -111,7 +115,7 @@ func run(ctx context.Context) error {
})
h := sha256.New()
h.Write([]byte(key))
if err := packages.Init(ctx, r, backend, h.Sum(nil)); err != nil {
if err := packages.Init(ctx, r, backend, h.Sum(nil), domain); err != nil {
return err
}
logrus.Infof("starting server at %s", addr)
Expand Down
8 changes: 4 additions & 4 deletions pkg/codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ type Codec[T any] interface {
Name() string
}

type CodecFuncs[T any] struct {
type Funcs[T any] struct {
Format string
EncodeFunc func(v T) ([]byte, error)
DecodeFunc func(b []byte) (T, error)
}

func (c CodecFuncs[T]) Encode(a T) ([]byte, error) {
func (c Funcs[T]) Encode(a T) ([]byte, error) {
return c.EncodeFunc(a)
}

func (c CodecFuncs[T]) Decode(b []byte) (T, error) {
func (c Funcs[T]) Decode(b []byte) (T, error) {
return c.DecodeFunc(b)
}

func (c CodecFuncs[T]) Name() string {
func (c Funcs[T]) Name() string {
return c.Format
}
13 changes: 9 additions & 4 deletions pkg/packages/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,20 @@ func Register(name string, factory ProviderFactory) {
providers[name] = factory
}

func Init(ctx context.Context, r *mux.Router, backend string, key []byte) error {
func Init(ctx context.Context, r *mux.Router, backend string, key []byte, domain string) error {
for k, v := range providers {
p, err := v(ctx)
if err != nil {
return err
}
sub := r.PathPrefix("/" + k).Subrouter()
sub.Use(repository.StorageMiddleware(p.Repository(), backend, key)("repo"))
p.Register(sub)
subs := []*mux.Router{r.PathPrefix("/" + k).Subrouter()}
if domain != "" {
subs = append(subs, r.Host(k+"."+domain).Subrouter())
}
for _, v := range subs {
v.Use(repository.StorageMiddleware(p.Repository(), backend, key)("repo"))
p.Register(v)
}
}
return nil
}
2 changes: 1 addition & 1 deletion pkg/packages/rpm/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (r *repo) Name() string {
}

func (r *repo) Codec() repository.Codec {
return codec.CodecFuncs[repository.Artifact]{
return codec.Funcs[repository.Artifact]{
Format: "json",
EncodeFunc: func(v repository.Artifact) ([]byte, error) {
return json.Marshal(v)
Expand Down

0 comments on commit c89b776

Please sign in to comment.