Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/homerunner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ HOMERUNNER_KEEP_BLUEPRINTS='name-of-blueprint' ./homerunner
```
This is neccessary to stop Homerunner from cleaning up the image. Then perform a single POST request:
```
curl -XPOST -d '{"blueprint_name":"name-of-blueprint"}'
curl -XPOST -d '{"blueprint_name":"name-of-blueprint"}' http://localhost:54321/create
{
"homeservers":{
"hs1":{
Expand Down
22 changes: 15 additions & 7 deletions cmd/homerunner/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

func Routes(rt *Runtime, cfg *Config) http.Handler {
mux := mux.NewRouter()
mux.Path("/create").Methods("POST").HandlerFunc(
util.WithCORSOptions(util.MakeJSONAPI(util.NewJSONRequestHandler(
mux.Path("/create").Methods("POST", "OPTIONS").HandlerFunc(
withCORS(util.MakeJSONAPI(util.NewJSONRequestHandler(
func(req *http.Request) util.JSONResponse {
rc := ReqCreate{}
if err := json.NewDecoder(req.Body).Decode(&rc); err != nil {
Expand All @@ -21,8 +21,8 @@ func Routes(rt *Runtime, cfg *Config) http.Handler {
},
))),
)
mux.Path("/destroy").Methods("POST").HandlerFunc(
util.WithCORSOptions(util.MakeJSONAPI(util.NewJSONRequestHandler(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, this was using matrix-org/util -> WithCORSOptions which only added the CORS headers to OPTIONS requests.

But we a) never accepted the OPTIONS method here and b) the CORS headers should also be on the non-OPTIONS requests.

mux.Path("/destroy").Methods("POST", "OPTIONS").HandlerFunc(
withCORS(util.MakeJSONAPI(util.NewJSONRequestHandler(
func(req *http.Request) util.JSONResponse {
rc := ReqDestroy{}
if err := json.NewDecoder(req.Body).Decode(&rc); err != nil {
Expand All @@ -32,10 +32,18 @@ func Routes(rt *Runtime, cfg *Config) http.Handler {
},
))),
)
mux.Path("/health").Methods("GET").HandlerFunc(
func(res http.ResponseWriter, req *http.Request) {
mux.Path("/health").Methods("GET", "OPTIONS").HandlerFunc(
withCORS(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(200)
},
}),
)
return mux
}

// withCORS intercepts all requests and adds CORS headers.
func withCORS(handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
util.SetCORSHeaders(w)
handler(w, req)
}
}
Loading