From c81a712a28eba960a4bcdc078cb51a1a4f832dbb Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Fri, 13 Jan 2017 19:22:57 -0300 Subject: [PATCH] Add CORS to create session and make it return text plain session ID --- api.go | 2 +- handlers/new_session.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/api.go b/api.go index 40e230c..62abeb8 100644 --- a/api.go +++ b/api.go @@ -79,7 +79,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) } }