diff --git a/api.go b/api.go index 40e230c..743a9c8 100644 --- a/api.go +++ b/api.go @@ -16,6 +16,7 @@ import ( "github.com/franela/play-with-docker/templates" "github.com/gorilla/mux" "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/rs/cors" "github.com/urfave/negroni" ) @@ -40,6 +41,10 @@ func main() { } r := mux.NewRouter() + c := cors.New(cors.Options{ + AllowedOrigins: []string{"*"}, + AllowCredentials: true, + }) // Reverse proxy (needs to be the first route, to make sure it is the first thing we check) proxyHandler := handlers.NewMultipleHostReverseProxy() @@ -49,7 +54,7 @@ func main() { r.Host(`{node:ip[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}}.{tld:.*}`).Handler(proxyHandler) r.HandleFunc("/ping", handlers.Ping).Methods("GET") r.HandleFunc("/sessions/{sessionId}", handlers.GetSession).Methods("GET") - r.HandleFunc("/sessions/{sessionId}/instances", handlers.NewInstance).Methods("POST") + r.Handle("/sessions/{sessionId}/instances", c.Handler(http.HandlerFunc(handlers.NewInstance))).Methods("POST") r.HandleFunc("/sessions/{sessionId}/instances/{instanceName}", handlers.DeleteInstance).Methods("DELETE") r.HandleFunc("/sessions/{sessionId}/instances/{instanceName}/keys", handlers.SetKeys).Methods("POST") @@ -62,8 +67,11 @@ func main() { r.HandleFunc("/robots.txt", func(rw http.ResponseWriter, r *http.Request) { http.ServeFile(rw, r, "www/robots.txt") }) + r.HandleFunc("/sdk.js", func(rw http.ResponseWriter, r *http.Request) { + http.ServeFile(rw, r, "www/sdk.js") + }) - r.Handle("/sessions/{sessionId}/ws/", server) + r.Handle("/sessions/{sessionId}/ws/", c.Handler(server)) r.Handle("/metrics", promhttp.Handler()) // Generic routes @@ -79,7 +87,7 @@ func main() { } }).Methods("GET") - r.HandleFunc("/", handlers.NewSession).Methods("POST") + r.Handle("/", c.Handler(http.HandlerFunc(handlers.NewSession))).Methods("POST") n := negroni.Classic() n.UseHandler(r) diff --git a/handlers/new_session.go b/handlers/new_session.go index 8cb497c..9ced56f 100644 --- a/handlers/new_session.go +++ b/handlers/new_session.go @@ -21,6 +21,11 @@ func NewSession(rw http.ResponseWriter, req *http.Request) { log.Println(err) //TODO: Return some error code } else { + // If request is not a form, return sessionId in the body + if req.Header.Get("Content-Type") != "application/x-www-form-urlencoded" { + rw.Write([]byte(s.Id)) + return + } http.Redirect(rw, req, fmt.Sprintf("/p/%s", s.Id), http.StatusFound) } }