Add base URL to serve file system store objects over HTTP

This commit is contained in:
Rob Watson 2021-12-09 04:05:34 +01:00
parent 13224f75ea
commit fb3a497119
4 changed files with 40 additions and 27 deletions

View File

@ -15,7 +15,12 @@ ASSETS_HTTP_ROOT=
# NOTE: Enabling the file system store will disable serving assets over HTTP.
FILE_STORE=filesystem
# The base path for the file system store.
# The base URL used for serving file store assets.
# Example: http://localhost:8888
FILE_STORE_HTTP_BASE_URL=
# The root directory for the file system store.
FILE_STORE_HTTP_ROOT=data/
# AWS credentials, required for the S3 store.

View File

@ -101,5 +101,5 @@ func buildFileStore(ctx context.Context, c config.Config, logger *zap.Logger) (m
}
logger.Info("Initializing file sysytem store")
return filestore.NewFileSystemStore(c.FileStoreHTTPRoot, "/")
return filestore.NewFileSystemStore(c.FileStoreHTTPRoot, c.FileStoreHTTPBaseURL)
}

View File

@ -21,18 +21,19 @@ const (
)
type Config struct {
Environment Environment
BindAddr string
TLSCertFile string
TLSKeyFile string
DatabaseURL string
FileStore FileStore
FileStoreHTTPRoot string
AWSAccessKeyID string
AWSSecretAccessKey string
AWSRegion string
S3Bucket string
AssetsHTTPRoot string
Environment Environment
BindAddr string
TLSCertFile string
TLSKeyFile string
DatabaseURL string
FileStore FileStore
FileStoreHTTPRoot string
FileStoreHTTPBaseURL string
AWSAccessKeyID string
AWSSecretAccessKey string
AWSRegion string
S3Bucket string
AssetsHTTPRoot string
}
func NewFromEnv() (Config, error) {
@ -76,6 +77,11 @@ func NewFromEnv() (Config, error) {
return Config{}, fmt.Errorf("invalid FILE_STORE value: %s", fileStoreString)
}
fileStoreHTTPBaseURL := os.Getenv("FILE_STORE_HTTP_BASE_URL")
if fileStoreHTTPBaseURL == "" {
fileStoreHTTPBaseURL = "/"
}
var awsAccessKeyID, awsSecretAccessKey, awsRegion, s3Bucket, fileStoreHTTPRoot string
if fileStore == S3Store {
awsAccessKeyID = os.Getenv("AWS_ACCESS_KEY_ID")
@ -106,17 +112,18 @@ func NewFromEnv() (Config, error) {
assetsHTTPRoot := os.Getenv("ASSETS_HTTP_ROOT")
return Config{
Environment: env,
BindAddr: bindAddr,
TLSCertFile: tlsCertFile,
TLSKeyFile: tlsKeyFile,
DatabaseURL: databaseURL,
FileStore: fileStore,
AWSAccessKeyID: awsAccessKeyID,
AWSSecretAccessKey: awsSecretAccessKey,
AWSRegion: awsRegion,
S3Bucket: s3Bucket,
AssetsHTTPRoot: assetsHTTPRoot,
FileStoreHTTPRoot: fileStoreHTTPRoot,
Environment: env,
BindAddr: bindAddr,
TLSCertFile: tlsCertFile,
TLSKeyFile: tlsKeyFile,
DatabaseURL: databaseURL,
FileStore: fileStore,
AWSAccessKeyID: awsAccessKeyID,
AWSSecretAccessKey: awsSecretAccessKey,
AWSRegion: awsRegion,
S3Bucket: s3Bucket,
AssetsHTTPRoot: assetsHTTPRoot,
FileStoreHTTPRoot: fileStoreHTTPRoot,
FileStoreHTTPBaseURL: fileStoreHTTPBaseURL,
}, nil
}

View File

@ -63,7 +63,8 @@ func (s *FileSystemStore) GetObjectWithRange(ctx context.Context, key string, st
// GetURL returns an HTTP URL for the provided file path.
func (s *FileSystemStore) GetURL(ctx context.Context, key string) (string, error) {
url := url.URL{}
var url url.URL
url.Scheme = s.baseURL.Scheme
url.Host = s.baseURL.Host
url.Path = fmt.Sprintf("%s%s", s.baseURL.Path, key)
return url.String(), nil