1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-07-14 10:17:26 +08:00

Add CORS handlers to support SDK

This commit is contained in:
Marcos Lilljedahl 2017-01-13 19:29:39 -03:00
parent c1cfc7958a
commit 4616cb1f5d
2 changed files with 16 additions and 3 deletions

14
api.go
View File

@ -16,6 +16,7 @@ import (
"github.com/franela/play-with-docker/templates" "github.com/franela/play-with-docker/templates"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/cors"
"github.com/urfave/negroni" "github.com/urfave/negroni"
) )
@ -40,6 +41,10 @@ func main() {
} }
r := mux.NewRouter() 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) // Reverse proxy (needs to be the first route, to make sure it is the first thing we check)
proxyHandler := handlers.NewMultipleHostReverseProxy() 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.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("/ping", handlers.Ping).Methods("GET")
r.HandleFunc("/sessions/{sessionId}", handlers.GetSession).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}", handlers.DeleteInstance).Methods("DELETE")
r.HandleFunc("/sessions/{sessionId}/instances/{instanceName}/keys", handlers.SetKeys).Methods("POST") 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) { r.HandleFunc("/robots.txt", func(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "www/robots.txt") 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()) r.Handle("/metrics", promhttp.Handler())
// Generic routes // Generic routes
@ -79,7 +87,7 @@ func main() {
} }
}).Methods("GET") }).Methods("GET")
r.HandleFunc("/", handlers.NewSession).Methods("POST") r.Handle("/", c.Handler(http.HandlerFunc(handlers.NewSession))).Methods("POST")
n := negroni.Classic() n := negroni.Classic()
n.UseHandler(r) n.UseHandler(r)

View File

@ -21,6 +21,11 @@ func NewSession(rw http.ResponseWriter, req *http.Request) {
log.Println(err) log.Println(err)
//TODO: Return some error code //TODO: Return some error code
} else { } 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) http.Redirect(rw, req, fmt.Sprintf("/p/%s", s.Id), http.StatusFound)
} }
} }