Refactor storage to support shallow types.
Add Client to storage. Fix client resizing issues.
This commit is contained in:
@@ -2,21 +2,38 @@ package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/play-with-docker/play-with-docker/pwd/types"
|
||||
)
|
||||
|
||||
type SessionInfo struct {
|
||||
*types.Session
|
||||
Instances map[string]*types.Instance `json:"instances"`
|
||||
}
|
||||
|
||||
func GetSession(rw http.ResponseWriter, req *http.Request) {
|
||||
vars := mux.Vars(req)
|
||||
sessionId := vars["sessionId"]
|
||||
|
||||
session := core.SessionGet(sessionId)
|
||||
|
||||
if session == nil {
|
||||
rw.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(rw).Encode(session)
|
||||
instances, err := core.InstanceFindBySession(session)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
is := map[string]*types.Instance{}
|
||||
for _, i := range instances {
|
||||
is[i.Name] = i
|
||||
}
|
||||
|
||||
json.NewEncoder(rw).Encode(SessionInfo{session, is})
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/play-with-docker/play-with-docker/pwd"
|
||||
"github.com/play-with-docker/play-with-docker/pwd/types"
|
||||
)
|
||||
|
||||
@@ -19,13 +20,12 @@ func NewInstance(rw http.ResponseWriter, req *http.Request) {
|
||||
|
||||
s := core.SessionGet(sessionId)
|
||||
|
||||
if len(s.Instances) >= 5 {
|
||||
rw.WriteHeader(http.StatusConflict)
|
||||
return
|
||||
}
|
||||
|
||||
i, err := core.InstanceNew(s, body)
|
||||
if err != nil {
|
||||
if pwd.SessionComplete(err) {
|
||||
rw.WriteHeader(http.StatusConflict)
|
||||
return
|
||||
}
|
||||
log.Println(err)
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
@@ -19,16 +19,15 @@ func SessionSetup(rw http.ResponseWriter, req *http.Request) {
|
||||
|
||||
s := core.SessionGet(sessionId)
|
||||
|
||||
if len(s.Instances) > 0 {
|
||||
log.Println("Cannot setup a session that contains instances")
|
||||
rw.WriteHeader(http.StatusConflict)
|
||||
rw.Write([]byte("Cannot setup a session that contains instances"))
|
||||
return
|
||||
}
|
||||
|
||||
s.Host = req.Host
|
||||
err := core.SessionSetup(s, body)
|
||||
if err != nil {
|
||||
if pwd.SessionNotEmpty(err) {
|
||||
log.Println("Cannot setup a session that contains instances")
|
||||
rw.WriteHeader(http.StatusConflict)
|
||||
rw.Write([]byte("Cannot setup a session that contains instances"))
|
||||
return
|
||||
}
|
||||
log.Println(err)
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
@@ -32,8 +32,13 @@ func WS(so socketio.Socket) {
|
||||
|
||||
so.Join(session.Id)
|
||||
|
||||
instances, err := core.InstanceFindBySession(session)
|
||||
if err != nil {
|
||||
log.Printf("Couldn't find instances for session with id [%s]. Got: %v\n", sessionId, err)
|
||||
return
|
||||
}
|
||||
var rw sync.Mutex
|
||||
trackedTerminals := make(map[string]net.Conn, len(session.Instances))
|
||||
trackedTerminals := make(map[string]net.Conn, len(instances))
|
||||
|
||||
attachTerminalToSocket := func(instance *types.Instance, ws socketio.Socket) {
|
||||
rw.Lock()
|
||||
@@ -73,7 +78,7 @@ func WS(so socketio.Socket) {
|
||||
}(instance.Name, conn, ws)
|
||||
}
|
||||
// since this is a new connection, get all terminals of the session and attach
|
||||
for _, instance := range session.Instances {
|
||||
for _, instance := range instances {
|
||||
attachTerminalToSocket(instance, so)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user