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. # NOTE: Enabling the file system store will disable serving assets over HTTP.
FILE_STORE=filesystem 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/ FILE_STORE_HTTP_ROOT=data/
# AWS credentials, required for the S3 store. # 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") 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 { type Config struct {
Environment Environment Environment Environment
BindAddr string BindAddr string
TLSCertFile string TLSCertFile string
TLSKeyFile string TLSKeyFile string
DatabaseURL string DatabaseURL string
FileStore FileStore FileStore FileStore
FileStoreHTTPRoot string FileStoreHTTPRoot string
AWSAccessKeyID string FileStoreHTTPBaseURL string
AWSSecretAccessKey string AWSAccessKeyID string
AWSRegion string AWSSecretAccessKey string
S3Bucket string AWSRegion string
AssetsHTTPRoot string S3Bucket string
AssetsHTTPRoot string
} }
func NewFromEnv() (Config, error) { func NewFromEnv() (Config, error) {
@ -76,6 +77,11 @@ func NewFromEnv() (Config, error) {
return Config{}, fmt.Errorf("invalid FILE_STORE value: %s", fileStoreString) 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 var awsAccessKeyID, awsSecretAccessKey, awsRegion, s3Bucket, fileStoreHTTPRoot string
if fileStore == S3Store { if fileStore == S3Store {
awsAccessKeyID = os.Getenv("AWS_ACCESS_KEY_ID") awsAccessKeyID = os.Getenv("AWS_ACCESS_KEY_ID")
@ -106,17 +112,18 @@ func NewFromEnv() (Config, error) {
assetsHTTPRoot := os.Getenv("ASSETS_HTTP_ROOT") assetsHTTPRoot := os.Getenv("ASSETS_HTTP_ROOT")
return Config{ return Config{
Environment: env, Environment: env,
BindAddr: bindAddr, BindAddr: bindAddr,
TLSCertFile: tlsCertFile, TLSCertFile: tlsCertFile,
TLSKeyFile: tlsKeyFile, TLSKeyFile: tlsKeyFile,
DatabaseURL: databaseURL, DatabaseURL: databaseURL,
FileStore: fileStore, FileStore: fileStore,
AWSAccessKeyID: awsAccessKeyID, AWSAccessKeyID: awsAccessKeyID,
AWSSecretAccessKey: awsSecretAccessKey, AWSSecretAccessKey: awsSecretAccessKey,
AWSRegion: awsRegion, AWSRegion: awsRegion,
S3Bucket: s3Bucket, S3Bucket: s3Bucket,
AssetsHTTPRoot: assetsHTTPRoot, AssetsHTTPRoot: assetsHTTPRoot,
FileStoreHTTPRoot: fileStoreHTTPRoot, FileStoreHTTPRoot: fileStoreHTTPRoot,
FileStoreHTTPBaseURL: fileStoreHTTPBaseURL,
}, nil }, 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. // GetURL returns an HTTP URL for the provided file path.
func (s *FileSystemStore) GetURL(ctx context.Context, key string) (string, error) { 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.Host = s.baseURL.Host
url.Path = fmt.Sprintf("%s%s", s.baseURL.Path, key) url.Path = fmt.Sprintf("%s%s", s.baseURL.Path, key)
return url.String(), nil return url.String(), nil