Configure backend to serve assets over HTTP
This commit is contained in:
parent
bcb43e3517
commit
4c999cd5a2
|
@ -6,3 +6,6 @@ AWS_REGION=
|
||||||
S3_BUCKET=
|
S3_BUCKET=
|
||||||
|
|
||||||
DATABASE_URL=
|
DATABASE_URL=
|
||||||
|
|
||||||
|
# If set, files in this location will be served over HTTP at /.
|
||||||
|
ASSETS_HTTP_BASE_PATH=
|
||||||
|
|
|
@ -20,6 +20,7 @@ type Config struct {
|
||||||
AWSSecretAccessKey string
|
AWSSecretAccessKey string
|
||||||
AWSRegion string
|
AWSRegion string
|
||||||
S3Bucket string
|
S3Bucket string
|
||||||
|
AssetsHTTPBasePath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFromEnv() (Config, error) {
|
func NewFromEnv() (Config, error) {
|
||||||
|
@ -61,6 +62,8 @@ func NewFromEnv() (Config, error) {
|
||||||
return Config{}, errors.New("S3_BUCKET not set")
|
return Config{}, errors.New("S3_BUCKET not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assetsHTTPBasePath := os.Getenv("ASSETS_HTTP_BASE_PATH")
|
||||||
|
|
||||||
return Config{
|
return Config{
|
||||||
Environment: env,
|
Environment: env,
|
||||||
DatabaseURL: databaseURL,
|
DatabaseURL: databaseURL,
|
||||||
|
@ -68,5 +71,6 @@ func NewFromEnv() (Config, error) {
|
||||||
AWSSecretAccessKey: awsSecretAccessKey,
|
AWSSecretAccessKey: awsSecretAccessKey,
|
||||||
AWSRegion: awsRegion,
|
AWSRegion: awsRegion,
|
||||||
S3Bucket: s3Bucket,
|
S3Bucket: s3Bucket,
|
||||||
|
AssetsHTTPBasePath: assetsHTTPBasePath,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -247,13 +247,28 @@ func Start(options Options) error {
|
||||||
// TODO: configure CORS
|
// TODO: configure CORS
|
||||||
grpcWebServer := grpcweb.WrapServer(grpcServer, grpcweb.WithOriginFunc(func(string) bool { return true }))
|
grpcWebServer := grpcweb.WrapServer(grpcServer, grpcweb.WithOriginFunc(func(string) bool { return true }))
|
||||||
|
|
||||||
|
log := logger.Sugar()
|
||||||
|
fileHandler := http.NotFoundHandler()
|
||||||
|
if options.Config.AssetsHTTPBasePath != "" {
|
||||||
|
log.With("basePath", options.Config.AssetsHTTPBasePath).Info("Configured to serve assets over HTTP")
|
||||||
|
fileHandler = http.FileServer(http.Dir(options.Config.AssetsHTTPBasePath))
|
||||||
|
}
|
||||||
|
|
||||||
httpServer := http.Server{
|
httpServer := http.Server{
|
||||||
Addr: options.BindAddr,
|
Addr: options.BindAddr,
|
||||||
ReadTimeout: options.Timeout,
|
ReadTimeout: options.Timeout,
|
||||||
WriteTimeout: options.Timeout,
|
WriteTimeout: options.Timeout,
|
||||||
Handler: http.HandlerFunc(grpcWebServer.ServeHTTP),
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !grpcWebServer.IsGrpcWebRequest(r) && !grpcWebServer.IsAcceptableGrpcCorsRequest(r) {
|
||||||
|
fileHandler.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
grpcWebServer.ServeHTTP(w, r)
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Infof("Listening at %s", options.BindAddr)
|
||||||
|
|
||||||
return httpServer.ListenAndServe()
|
return httpServer.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue