proxymode/resolver.go (28 lines of code) (raw):
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License 2.0;
// you may not use this file except in compliance with the Elastic License 2.0.
package proxymode
import (
"fmt"
"net/http"
"net/url"
"github.com/elastic/package-registry/packages"
)
type proxyResolver struct {
destinationURL url.URL
}
func (pr proxyResolver) redirectRequest(w http.ResponseWriter, r *http.Request, remotePath string) {
remoteURL := pr.destinationURL.
ResolveReference(&url.URL{Path: remotePath})
http.Redirect(w, r, remoteURL.String(), http.StatusMovedPermanently)
}
func (pr proxyResolver) ArtifactsHandler(w http.ResponseWriter, r *http.Request, p *packages.Package) {
remotePath := fmt.Sprintf("/epr/package/%s-%s.zip", p.Name, p.Version)
pr.redirectRequest(w, r, remotePath)
}
func (pr proxyResolver) StaticHandler(w http.ResponseWriter, r *http.Request, p *packages.Package, resourcePath string) {
remotePath := fmt.Sprintf("/package/%s/%s/%s", p.Name, p.Version, resourcePath)
pr.redirectRequest(w, r, remotePath)
}
func (pr proxyResolver) SignaturesHandler(w http.ResponseWriter, r *http.Request, p *packages.Package) {
remotePath := fmt.Sprintf("/epr/package/%s-%s.zip.sig", p.Name, p.Version)
pr.redirectRequest(w, r, remotePath)
}
var _ packages.RemoteResolver = new(proxyResolver)