Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Clutch
Commits
399c86f9
Commit
399c86f9
authored
4 years ago
by
Mike Cutalo
Browse files
Options
Download
Email Patches
Plain Diff
better error handling
parent
ccfc1fc4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
backend/gateway/mux/mux.go
+45
-5
backend/gateway/mux/mux.go
backend/gateway/mux/mux_test.go
+16
-0
backend/gateway/mux/mux_test.go
with
61 additions
and
5 deletions
+61
-5
backend/gateway/mux/mux.go
+
45
-
5
View file @
399c86f9
...
...
@@ -75,10 +75,20 @@ func (a *assetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Otherwise its difficult to know if the assets are being served from the configured provider.
w
.
Header
()
.
Set
(
"x-clutch-asset-passthrough"
,
"true"
)
asset
,
_
:=
a
.
assetProviderHandler
(
r
.
Context
(),
r
.
URL
.
Path
)
asset
,
err
:=
a
.
assetProviderHandler
(
r
.
Context
(),
r
.
URL
.
Path
)
if
err
!=
nil
{
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
_
,
_
=
w
.
Write
([]
byte
(
"Error getting assets from the configured asset provider"
))
return
}
defer
asset
.
Close
()
_
,
_
=
io
.
Copy
(
w
,
asset
)
_
,
err
=
io
.
Copy
(
w
,
asset
)
if
err
!=
nil
{
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
_
,
_
=
w
.
Write
([]
byte
(
"Error getting assets from the configured asset provider"
))
return
}
return
}
...
...
@@ -94,9 +104,9 @@ func (a *assetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func
(
a
*
assetHandler
)
assetProviderHandler
(
ctx
context
.
Context
,
urlPath
string
)
(
io
.
ReadCloser
,
error
)
{
switch
a
.
assetCfg
.
Provider
.
(
type
)
{
case
*
gatewayv1
.
Assets_S3
:
aws
,
ok
:=
service
.
Registry
[
awsservice
.
Name
]
if
!
ok
{
return
nil
,
fmt
.
Errorf
(
"The AWS service must be configured to use the asset s3 provider."
)
aws
,
err
:=
getAssetProviderService
(
a
.
assetCfg
)
if
err
!=
nil
{
return
nil
,
err
}
awsClient
,
ok
:=
aws
.
(
awsservice
.
Client
)
...
...
@@ -115,6 +125,27 @@ func (a *assetHandler) assetProviderHandler(ctx context.Context, urlPath string)
}
}
// getAssetProviderService is used in two different contexts
// Its invoked in the mux constructor which checks if the necessary service has been configured,
// if there is an asset provider which requires ones.
//
// Otherwise its used to get the service for an asset provider in assetProviderHandler() if necessary.
func
getAssetProviderService
(
assetCfg
*
gatewayv1
.
Assets
)
(
service
.
Service
,
error
)
{
switch
assetCfg
.
Provider
.
(
type
)
{
case
*
gatewayv1
.
Assets_S3
:
aws
,
ok
:=
service
.
Registry
[
awsservice
.
Name
]
if
!
ok
{
return
nil
,
fmt
.
Errorf
(
"The AWS service must be configured to use the asset s3 provider."
)
}
return
aws
,
nil
default
:
// An asset provider does not necessarily require a service to function properly
// if there is nothing configured for a provider type we cant necessarily throw an error here.
return
nil
,
nil
}
}
func
customResponseForwarder
(
ctx
context
.
Context
,
w
http
.
ResponseWriter
,
resp
proto
.
Message
)
error
{
md
,
ok
:=
runtime
.
ServerMetadataFromContext
(
ctx
)
if
!
ok
{
...
...
@@ -183,6 +214,15 @@ func New(unaryInterceptors []grpc.UnaryServerInterceptor, assets http.FileSystem
),
)
// If there is a configured asset provider, we check to see if the service is configured before proceeding.
// Bailing out early during the startup process instead of hitting this error at runtime when serving assets.
if
assetCfg
!=
nil
&&
assetCfg
.
Provider
!=
nil
{
_
,
err
:=
getAssetProviderService
(
assetCfg
)
if
err
!=
nil
{
panic
(
err
)
}
}
httpMux
:=
http
.
NewServeMux
()
httpMux
.
Handle
(
"/"
,
&
assetHandler
{
assetCfg
:
assetCfg
,
...
...
This diff is collapsed.
Click to expand it.
backend/gateway/mux/mux_test.go
+
16
-
0
View file @
399c86f9
...
...
@@ -50,3 +50,19 @@ func TestAssetProviderS3Handler(t *testing.T) {
_
,
err
:=
handler
.
assetProviderHandler
(
context
.
TODO
(),
"clutch.sh/static/main.js"
)
assert
.
Error
(
t
,
err
)
}
func
TestGetAssetProivderService
(
t
*
testing
.
T
)
{
assetCfg
:=
&
gatewayv1
.
Assets
{
Provider
:
&
gatewayv1
.
Assets_S3
{
S3
:
&
gatewayv1
.
Assets_S3Provider
{
Region
:
"us-east-1"
,
Bucket
:
"clutch"
,
Key
:
"static"
,
},
},
}
// Test that the aws service must be configured to use the S3 handler
_
,
err
:=
getAssetProviderService
(
assetCfg
)
assert
.
Error
(
t
,
err
)
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment