-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor server to use the Echo framework #26
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
797b07c
First Cut of conversion to echo
miketonks-form3 d5ffb99
support proxy path prefix
kevin-intri-form3 17ca5cb
mac issues
kevin-intri-form3 1968830
Restore api struct methods and remove custom context
miketonks-form3 ae7ee6b
Remove TODOs
miketonks-form3 a842acc
Add extra options to curl command
miketonks-form3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,60 +1,57 @@ | ||
package configuration | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/form3tech-oss/pact-proxy/internal/app/httpresponse" | ||
"github.com/labstack/echo/v4" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func ServeAdminAPI(port int) *http.Server { | ||
adminServerHandler := http.NewServeMux() | ||
s := &http.Server{ | ||
Addr: fmt.Sprintf(":%d", port), | ||
Handler: adminServerHandler, | ||
} | ||
func ServeAdminAPI(port int) *echo.Echo { | ||
adminServer := echo.New() | ||
adminServer.HideBanner = true | ||
|
||
adminServerHandler.HandleFunc("/proxies", adminHandler) | ||
adminServer.DELETE("/proxies", deleteProxiesHandler) | ||
adminServer.POST("/proxies", postProxiesHandler) | ||
|
||
go func() { | ||
if err := s.ListenAndServe(); err != nil { | ||
address := fmt.Sprintf(":%d", port) | ||
if err := adminServer.Start(address); err != nil { | ||
log.Fatal(err) | ||
} | ||
}() | ||
|
||
return s | ||
return adminServer | ||
} | ||
|
||
func adminHandler(w http.ResponseWriter, req *http.Request) { | ||
if req.Method == http.MethodDelete { | ||
log.Infof("closing all proxies") | ||
CloseAllServers() | ||
return | ||
} | ||
func deleteProxiesHandler(c echo.Context) error { | ||
log.Infof("closing all proxies") | ||
CloseAllServers() | ||
return c.NoContent(http.StatusNoContent) | ||
} | ||
|
||
if req.Method == http.MethodPost { | ||
configBytes, err := ioutil.ReadAll(req.Body) | ||
if err != nil { | ||
httpresponse.Errorf(w, http.StatusBadRequest, "unable to read constraint. %s", err.Error()) | ||
return | ||
} | ||
func postProxiesHandler(c echo.Context) error { | ||
|
||
proxyConfig := ProxyConfig{} | ||
err = json.Unmarshal(configBytes, &proxyConfig) | ||
if err != nil { | ||
httpresponse.Errorf(w, http.StatusBadRequest, "unable to parse interactionConstraint from data. %s", err.Error()) | ||
return | ||
} | ||
proxyConfig := ProxyConfig{} | ||
err := c.Bind(&proxyConfig) | ||
if err != nil { | ||
return c.JSON( | ||
http.StatusBadRequest, | ||
httpresponse.Errorf("unable to parse interactionConstraint from data. %s", err.Error()), | ||
) | ||
} | ||
|
||
log.Infof("setting up proxy from %s to %s", proxyConfig.ServerAddress, proxyConfig.Target) | ||
log.Infof("setting up proxy from %s to %s", proxyConfig.ServerAddress, proxyConfig.Target) | ||
|
||
err = ConfigureProxy(proxyConfig) | ||
if err != nil { | ||
httpresponse.Errorf(w, http.StatusInternalServerError, "unable to create proxy from configuration. %s", err.Error()) | ||
return | ||
} | ||
err = ConfigureProxy(proxyConfig) | ||
if err != nil { | ||
return c.JSON( | ||
http.StatusInternalServerError, | ||
httpresponse.Errorf("unable to create proxy from configuration. %s", err.Error()), | ||
) | ||
} | ||
|
||
return c.NoContent(http.StatusNoContent) | ||
} |
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,33 +1,19 @@ | ||
package httpresponse | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func Error(w http.ResponseWriter, code int, error string) { | ||
func Error(error string) *APIError { | ||
log.Error(error) | ||
e := &APIError{ | ||
ErrorMessage: error, | ||
} | ||
|
||
w.WriteHeader(code) | ||
w.Header().Add("Content-Type", "application/json") | ||
|
||
payload, err := json.Marshal(e) | ||
if err != nil { | ||
log.WithError(err).Error("could not marshal error into json") | ||
} | ||
|
||
_, err = fmt.Fprintln(w, payload) | ||
if err != nil { | ||
log.WithError(err).Error("could not write httpresponse error") | ||
} | ||
return e | ||
} | ||
|
||
func Errorf(w http.ResponseWriter, code int, error string, a ...interface{}) { | ||
Error(w, code, fmt.Sprintf(error, a...)) | ||
func Errorf(error string, a ...interface{}) *APIError { | ||
return Error(fmt.Sprintf(error, a...)) | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we're changing this, can we do
-sSLf
perhaps all the options directly after curl, rather it being in the middle?
-o
could remain where it is I guess.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍