mirror of
https://github.com/gomods/athens
synced 2026-02-03 11:00:32 +00:00
Added configurable CDN endpoint (#214)
* added configurable cdn endpoint
This commit is contained in:
Vendored
+23
@@ -0,0 +1,23 @@
|
||||
package env
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/gobuffalo/envy"
|
||||
)
|
||||
|
||||
// CDNEndpointWithDefault returns CDN endpoint if set
|
||||
// if not it should default to clouds default blob storage endpoint e.g
|
||||
func CDNEndpointWithDefault(value *url.URL) *url.URL {
|
||||
rawURI, err := envy.MustGet("CDN_ENDPOINT")
|
||||
if err != nil {
|
||||
return value
|
||||
}
|
||||
|
||||
uri, err := url.Parse(rawURI)
|
||||
if err != nil {
|
||||
return value
|
||||
}
|
||||
|
||||
return uri
|
||||
}
|
||||
@@ -37,7 +37,7 @@ func New(accountName, accountKey string) (*Storage, error) {
|
||||
//
|
||||
// <meta name="go-import" content="gomods.com/athens mod BaseURL()">
|
||||
func (s Storage) BaseURL() *url.URL {
|
||||
return s.accountURL
|
||||
return env.CDNEndpointWithDefault(s.accountURL)
|
||||
}
|
||||
|
||||
// Save implements the (github.com/gomods/athens/pkg/storage).Saver interface.
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"cloud.google.com/go/storage"
|
||||
@@ -18,7 +19,8 @@ import (
|
||||
// Storage implements the Saver interface
|
||||
// (./pkg/storage).Saver
|
||||
type Storage struct {
|
||||
bucket *storage.BucketHandle
|
||||
bucket *storage.BucketHandle
|
||||
baseURI *url.URL
|
||||
}
|
||||
|
||||
// New returns a new Storage instance authenticated using the provided
|
||||
@@ -37,7 +39,23 @@ func New(ctx context.Context, cred option.ClientOption) (*Storage, error) {
|
||||
return nil, err
|
||||
}
|
||||
bkt := client.Bucket(bucketname)
|
||||
return &Storage{bucket: bkt}, nil
|
||||
|
||||
u, err := url.Parse(fmt.Sprintf("https://storage.googleapis.com/%s", bucketname))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Storage{bucket: bkt, baseURI: u}, nil
|
||||
}
|
||||
|
||||
// BaseURL returns the base URL that stores all modules. It can be used
|
||||
// in the "meta" tag redirect response to vgo.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// <meta name="go-import" content="gomods.com/athens mod BaseURL()">
|
||||
func (s *Storage) BaseURL() *url.URL {
|
||||
return env.CDNEndpointWithDefault(s.baseURI)
|
||||
}
|
||||
|
||||
// Save uploads the module .mod, .zip and .info files for a given version.
|
||||
|
||||
@@ -73,7 +73,7 @@ func NewWithClient(bucketName string, client s3iface.S3API) (*Storage, error) {
|
||||
//
|
||||
// <meta name="go-import" content="gomods.com/athens mod BaseURL()">
|
||||
func (s Storage) BaseURL() *url.URL {
|
||||
return s.baseURI
|
||||
return env.CDNEndpointWithDefault(s.baseURI)
|
||||
}
|
||||
|
||||
// Save implements the (github.com/gomods/athens/pkg/storage).Saver interface.
|
||||
|
||||
Reference in New Issue
Block a user