-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
157 additions
and
30 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
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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
package config | ||
|
||
var ( | ||
ApiVersion string | ||
Port int | ||
Bind string | ||
Dev bool | ||
ModulesDir string | ||
Backend string | ||
ApiVersion string | ||
Port int | ||
Bind string | ||
Dev bool | ||
ModulesDir string | ||
Backend string | ||
CORSOrigins string | ||
FallbackProxyUrl string | ||
) |
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,107 @@ | ||
package middleware | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/dadav/gorge/internal/log" | ||
) | ||
|
||
// capturedResponseWriter is a custom response writer that captures the response status | ||
type capturedResponseWriter struct { | ||
http.ResponseWriter | ||
body []byte | ||
status int | ||
} | ||
|
||
func (w *capturedResponseWriter) WriteHeader(code int) { | ||
w.status = code | ||
} | ||
|
||
func (w *capturedResponseWriter) Write(body []byte) (int, error) { | ||
w.body = body | ||
return len(body), nil | ||
} | ||
|
||
func (w *capturedResponseWriter) sendOriginalResponse() { | ||
w.ResponseWriter.WriteHeader(w.status) | ||
w.ResponseWriter.Write(w.body) | ||
} | ||
|
||
func ProxyFallback(upstreamHost string) func(next http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// capture response | ||
capturedResponseWriter := &capturedResponseWriter{ResponseWriter: w} | ||
next.ServeHTTP(capturedResponseWriter, r) | ||
|
||
if capturedResponseWriter.status == http.StatusNotFound { | ||
log.Log.Infof("Forwarding request to %s\n", upstreamHost) | ||
forwardRequest(w, r, upstreamHost) | ||
return | ||
} | ||
|
||
// If the response status is not 404, serve the original response | ||
capturedResponseWriter.sendOriginalResponse() | ||
}) | ||
} | ||
} | ||
|
||
func forwardRequest(w http.ResponseWriter, r *http.Request, forwardHost string) { | ||
// Create a buffer to store the request body | ||
var requestBodyBytes []byte | ||
if r.Body != nil { | ||
requestBodyBytes, _ = io.ReadAll(r.Body) | ||
} | ||
|
||
// Clone the original request | ||
forwardUrl, err := url.JoinPath(forwardHost, r.URL.Path) | ||
if err != nil { | ||
http.Error(w, "Failed to create forwarded request", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
forwardedRequest, err := http.NewRequest(r.Method, forwardUrl, bytes.NewBuffer(requestBodyBytes)) | ||
if err != nil { | ||
http.Error(w, "Failed to create forwarded request", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// Copy headers from the original request | ||
forwardedRequest.Header = make(http.Header) | ||
for key, values := range r.Header { | ||
for _, value := range values { | ||
forwardedRequest.Header.Add(key, value) | ||
} | ||
} | ||
|
||
// Make the request to the forward host | ||
// TODO: Add caching | ||
client := http.Client{} | ||
resp, err := client.Do(forwardedRequest) | ||
if err != nil { | ||
http.Error(w, "Failed to forward request", http.StatusInternalServerError) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
// Copy the response headers | ||
for key, values := range resp.Header { | ||
for _, value := range values { | ||
w.Header().Add(key, value) | ||
} | ||
} | ||
|
||
// Write the response status code | ||
w.WriteHeader(resp.StatusCode) | ||
|
||
// Write the response body | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
http.Error(w, "Failed to read response body", http.StatusInternalServerError) | ||
return | ||
} | ||
w.Write(body) | ||
} |