-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #243 from PDOK/refactoring
refactor(go): polishing
- Loading branch information
Showing
9 changed files
with
194 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package engine | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/go-chi/chi/v5" | ||
) | ||
|
||
// Serve static assets either from local storage or through reverse proxy | ||
func newResourcesEndpoint(e *Engine) { | ||
if e.Config.Resources.Directory != nil && *e.Config.Resources.Directory != "" { | ||
resourcesPath := strings.TrimSuffix(*e.Config.Resources.Directory, "/resources") | ||
e.Router.Handle("/resources/*", http.FileServer(http.Dir(resourcesPath))) | ||
} else if e.Config.Resources.URL != nil && e.Config.Resources.URL.String() != "" { | ||
e.Router.Get("/resources/*", proxy(e.ReverseProxy, e.Config.Resources.URL.String())) | ||
} | ||
} | ||
|
||
type reverseProxy func(w http.ResponseWriter, r *http.Request, target *url.URL, prefer204 bool, overwrite string) | ||
|
||
func proxy(rp reverseProxy, resourcesURL string) func(w http.ResponseWriter, r *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
resourcePath, _ := url.JoinPath("/", chi.URLParam(r, "*")) | ||
target, err := url.ParseRequestURI(resourcesURL + resourcePath) | ||
if err != nil { | ||
log.Printf("invalid target url, can't proxy resources: %v", err) | ||
RenderProblem(ProblemServerError, w) | ||
return | ||
} | ||
rp(w, r, target, true, "") | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package engine | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type MockReverseProxy struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockReverseProxy) Proxy(w http.ResponseWriter, r *http.Request, target *url.URL, prefer204 bool, overwrite string) { | ||
m.Called(w, r, target, prefer204, overwrite) | ||
} | ||
|
||
func TestProxy(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
resourcesURL string | ||
urlParam string | ||
expectedStatus int | ||
expectedLog string | ||
expectProxy bool | ||
}{ | ||
{ | ||
name: "valid url", | ||
resourcesURL: "http://example.com/resources", | ||
urlParam: "file", | ||
expectedStatus: http.StatusOK, | ||
expectedLog: "", | ||
expectProxy: true, | ||
}, | ||
{ | ||
name: "invalid url", | ||
resourcesURL: "foo bar", | ||
urlParam: "file", | ||
expectedStatus: http.StatusInternalServerError, | ||
expectedLog: "invalid target url, can't proxy resources", | ||
expectProxy: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// given | ||
mockReverseProxy := MockReverseProxy{} | ||
if tt.expectProxy { | ||
mockReverseProxy.On("Proxy", mock.Anything, mock.Anything, mock.Anything, true, "").Return() | ||
} | ||
r := httptest.NewRequest(http.MethodGet, "/resources/"+tt.urlParam, nil) | ||
w := httptest.NewRecorder() | ||
var logOutput strings.Builder | ||
log.SetOutput(&logOutput) | ||
|
||
// when | ||
proxyHandler := proxy(mockReverseProxy.Proxy, tt.resourcesURL) | ||
proxyHandler(w, r) | ||
|
||
// then | ||
assert.Equal(t, tt.expectedStatus, w.Result().StatusCode) | ||
if tt.expectedLog != "" { | ||
assert.Contains(t, logOutput.String(), tt.expectedLog) | ||
} | ||
if tt.expectProxy { | ||
mockReverseProxy.AssertCalled(t, "Proxy", w, r, mock.Anything, true, "") | ||
} else { | ||
mockReverseProxy.AssertNotCalled(t, "Proxy", w, r, mock.Anything, true, "") | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.