Skip to content

Commit

Permalink
Added configurable CDN endpoint (gomods#214)
Browse files Browse the repository at this point in the history
* added configurable cdn endpoint
  • Loading branch information
michalpristas authored Jul 9, 2018
1 parent fda6ec5 commit c02e9c0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
23 changes: 23 additions & 0 deletions pkg/config/env/cdn.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion pkg/storage/azurecdn/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 20 additions & 2 deletions pkg/storage/gcp/saver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"log"
"net/url"
"time"

"cloud.google.com/go/storage"
Expand All @@ -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
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/s3/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c02e9c0

Please sign in to comment.