Add error to GetSession return values

- Add button to copy instance SSH access
This commit is contained in:
marcos
2017-12-14 17:37:38 -03:00
parent 3ffaaaf934
commit dd6a332889
17 changed files with 94 additions and 26 deletions

View File

@@ -19,6 +19,7 @@ import (
"github.com/play-with-docker/play-with-docker/config"
"github.com/play-with-docker/play-with-docker/event"
"github.com/play-with-docker/play-with-docker/pwd"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/negroni"
)
@@ -27,8 +28,19 @@ var core pwd.PWDApi
var e event.EventApi
var landings = map[string][]byte{}
var latencyHistogramVec = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "pwd_handlers_duration_ms",
Help: "How long it took to process a specific handler, in a specific host",
Buckets: []float64{300, 1200, 5000},
}, []string{"action"})
type HandlerExtender func(h *mux.Router)
func init() {
prometheus.MustRegister(latencyHistogramVec)
}
func Bootstrap(c pwd.PWDApi, ev event.EventApi) {
core = c
e = ev
@@ -53,6 +65,10 @@ func Register(extend HandlerExtender) {
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}", DeleteInstance).Methods("DELETE")
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}/exec", Exec).Methods("POST")
r.HandleFunc("/sessions/{sessionId}/instances/{instanceName}/editor.html", func(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "www/editor.html")
})
r.HandleFunc("/ooc", func(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "./www/ooc.html")
}).Methods("GET")
@@ -64,9 +80,6 @@ func Register(extend HandlerExtender) {
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")
})
corsRouter.HandleFunc("/sessions/{sessionId}/ws/", WSH)
r.Handle("/metrics", promhttp.Handler())
@@ -90,6 +103,7 @@ func Register(extend HandlerExtender) {
}
n := negroni.Classic()
r.PathPrefix("/").Handler(negroni.New(negroni.Wrap(corsHandler(corsRouter))))
n.UseHandler(r)
@@ -128,7 +142,11 @@ func Register(extend HandlerExtender) {
rr.HandleFunc("/ping", Ping).Methods("GET")
rr.Handle("/metrics", promhttp.Handler())
rr.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
http.Redirect(rw, r, fmt.Sprintf("https://%s", r.Host), http.StatusMovedPermanently)
target := fmt.Sprintf("https://%s%s", r.Host, r.URL.Path)
if len(r.URL.RawQuery) > 0 {
target += "?" + r.URL.RawQuery
}
http.Redirect(rw, r, target, http.StatusMovedPermanently)
})
nr := negroni.Classic()
nr.UseHandler(rr)

View File

@@ -5,16 +5,20 @@ import (
"net/http"
"github.com/gorilla/mux"
"github.com/play-with-docker/play-with-docker/storage"
)
func CloseSession(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
sessionId := vars["sessionId"]
session := core.SessionGet(sessionId)
if session == nil {
session, err := core.SessionGet(sessionId)
if err == storage.NotFoundError {
rw.WriteHeader(http.StatusNotFound)
return
} else if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
if err := core.SessionClose(session); err != nil {

View File

@@ -4,6 +4,7 @@ import (
"net/http"
"github.com/gorilla/mux"
"github.com/play-with-docker/play-with-docker/storage"
)
func DeleteInstance(rw http.ResponseWriter, req *http.Request) {
@@ -11,7 +12,7 @@ func DeleteInstance(rw http.ResponseWriter, req *http.Request) {
sessionId := vars["sessionId"]
instanceName := vars["instanceName"]
s := core.SessionGet(sessionId)
s, err := core.SessionGet(sessionId)
if s != nil {
i := core.InstanceGet(s, instanceName)
err := core.InstanceDelete(s, i)
@@ -19,8 +20,11 @@ func DeleteInstance(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusInternalServerError)
return
}
} else {
rw.WriteHeader(http.StatusNotFound)
} else if err == storage.NotFoundError {
rw.WriteHeader(http.StatusInternalServerError)
return
} else if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
}

View File

@@ -28,7 +28,7 @@ func Exec(rw http.ResponseWriter, req *http.Request) {
return
}
s := core.SessionGet(sessionId)
s, _ := core.SessionGet(sessionId)
if s == nil {
rw.WriteHeader(http.StatusNotFound)
return

View File

@@ -7,6 +7,7 @@ import (
"path/filepath"
"github.com/gorilla/mux"
"github.com/play-with-docker/play-with-docker/storage"
)
func FileUpload(rw http.ResponseWriter, req *http.Request) {
@@ -14,7 +15,14 @@ func FileUpload(rw http.ResponseWriter, req *http.Request) {
sessionId := vars["sessionId"]
instanceName := vars["instanceName"]
s := core.SessionGet(sessionId)
s, err := core.SessionGet(sessionId)
if err == storage.NotFoundError {
rw.WriteHeader(http.StatusNotFound)
return
} else if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
i := core.InstanceGet(s, instanceName)
// allow up to 32 MB which is the default

View File

@@ -7,6 +7,7 @@ import (
"github.com/gorilla/mux"
"github.com/play-with-docker/play-with-docker/pwd/types"
"github.com/play-with-docker/play-with-docker/storage"
)
type SessionInfo struct {
@@ -18,8 +19,11 @@ func GetSession(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
sessionId := vars["sessionId"]
session := core.SessionGet(sessionId)
if session == nil {
session, err := core.SessionGet(sessionId)
if err == storage.NotFoundError {
rw.WriteHeader(http.StatusNotFound)
return
} else if err != nil {
rw.WriteHeader(http.StatusNotFound)
return
}

View File

@@ -7,17 +7,21 @@ import (
"path/filepath"
"github.com/gorilla/mux"
"github.com/play-with-docker/play-with-docker/storage"
)
func Home(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
sessionId := vars["sessionId"]
s := core.SessionGet(sessionId)
if s == nil {
s, err := core.SessionGet(sessionId)
if err == storage.NotFoundError {
// Session doesn't exist (can happen if closing the sessions an reloading the page, or similar).
w.WriteHeader(http.StatusNotFound)
return
} else if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if s.Stack != "" {
go core.SessionDeployStack(s)

View File

@@ -20,7 +20,11 @@ func NewInstance(rw http.ResponseWriter, req *http.Request) {
json.NewDecoder(req.Body).Decode(&body)
s := core.SessionGet(sessionId)
s, err := core.SessionGet(sessionId)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
playground := core.PlaygroundGet(s.PlaygroundId)
if playground == nil {

View File

@@ -12,6 +12,7 @@ import (
)
func Ping(rw http.ResponseWriter, req *http.Request) {
defer latencyHistogramVec.WithLabelValues("ping").Observe(float64(time.Since(time.Now()).Nanoseconds()) / 1000000)
// Get system load average of the last 5 minutes and compare it against a threashold.
c, err := docker.NewEnvClient()
@@ -27,6 +28,7 @@ func Ping(rw http.ResponseWriter, req *http.Request) {
if _, err := c.Info(ctx); err != nil && err == context.DeadlineExceeded {
log.Printf("Docker info took to long to respond\n")
rw.WriteHeader(http.StatusGatewayTimeout)
return
}
a, err := load.Avg()

View File

@@ -17,9 +17,13 @@ func SessionSetup(rw http.ResponseWriter, req *http.Request) {
json.NewDecoder(req.Body).Decode(&body)
s := core.SessionGet(sessionId)
s, err := core.SessionGet(sessionId)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
err := core.SessionSetup(s, body)
err = core.SessionSetup(s, body)
if err != nil {
if pwd.SessionNotEmpty(err) {
log.Println("Cannot setup a session that contains instances")

View File

@@ -10,6 +10,7 @@ import (
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/play-with-docker/play-with-docker/event"
"github.com/play-with-docker/play-with-docker/storage"
"github.com/satori/go.uuid"
)
@@ -144,8 +145,8 @@ func ws(so *socket) {
sessionId := vars["sessionId"]
session := core.SessionGet(sessionId)
if session == nil {
session, err := core.SessionGet(sessionId)
if err == storage.NotFoundError {
log.Printf("Session with id [%s] does not exist!\n", sessionId)
return
}