From 8424479e76492c78cc43f7006f271d7cac1c3ae5 Mon Sep 17 00:00:00 2001 From: "Jonathan Leibiusky @xetorthio" Date: Mon, 24 Jul 2017 14:55:17 -0300 Subject: [PATCH] Update event names --- handlers/ws.go | 4 ++-- pwd/instance.go | 2 -- pwd/session.go | 11 ++++++++--- recaptcha/recaptcha.go | 4 ++-- storage/file.go | 15 ++++++++++++++- www/assets/app.js | 15 ++++++--------- 6 files changed, 32 insertions(+), 19 deletions(-) diff --git a/handlers/ws.go b/handlers/ws.go index e0fea5d..65eb6d2 100644 --- a/handlers/ws.go +++ b/handlers/ws.go @@ -32,12 +32,12 @@ func WS(so socketio.Socket) { core.SessionClose(session) }) - so.On("terminal in", func(name, data string) { + so.On("instance terminal in", func(name, data string) { // User wrote something on the terminal. Need to write it to the instance terminal core.InstanceWriteToTerminal(session.Id, name, data) }) - so.On("viewport resize", func(cols, rows uint) { + so.On("instance viewport resize", func(cols, rows uint) { // User resized his viewport core.ClientResizeViewPort(client, cols, rows) }) diff --git a/pwd/instance.go b/pwd/instance.go index 32f28da..dbd1d9e 100644 --- a/pwd/instance.go +++ b/pwd/instance.go @@ -170,7 +170,6 @@ func (p *pwd) InstanceDelete(session *types.Session, instance *types.Instance) e conn := getInstanceTermConn(session.Id, instance.Name) if conn != nil { conn.Close() - delete(terms[instance.SessionId], instance.Name) } err := p.docker.DeleteContainer(instance.Name) if err != nil && !strings.Contains(err.Error(), "No such container") { @@ -180,7 +179,6 @@ func (p *pwd) InstanceDelete(session *types.Session, instance *types.Instance) e p.event.Emit(event.INSTANCE_DELETE, session.Id, instance.Name) - delete(session.Instances, instance.Name) if err := p.storage.InstanceDelete(session.Id, instance.Name); err != nil { return err } diff --git a/pwd/session.go b/pwd/session.go index badf98e..09068e7 100644 --- a/pwd/session.go +++ b/pwd/session.go @@ -14,7 +14,7 @@ import ( "github.com/play-with-docker/play-with-docker/docker" "github.com/play-with-docker/play-with-docker/event" "github.com/play-with-docker/play-with-docker/pwd/types" - "github.com/twinj/uuid" + "github.com/rs/xid" ) var preparedSessions = map[string]bool{} @@ -44,7 +44,7 @@ func (p *pwd) SessionNew(duration time.Duration, stack, stackName, imageName str defer observeAction("SessionNew", time.Now()) s := &types.Session{} - s.Id = uuid.NewV4().String() + s.Id = xid.New().String() s.Instances = map[string]*types.Instance{} s.CreatedAt = time.Now() s.ExpiresAt = s.CreatedAt.Add(duration) @@ -186,7 +186,12 @@ func (p *pwd) SessionDeployStack(s *types.Session) error { func (p *pwd) SessionGet(sessionId string) *types.Session { defer observeAction("SessionGet", time.Now()) - s, _ := p.storage.SessionGet(sessionId) + s, err := p.storage.SessionGet(sessionId) + + if err != nil { + log.Println(err) + return nil + } if _, err := p.prepareSession(s); err != nil { log.Println(err) diff --git a/recaptcha/recaptcha.go b/recaptcha/recaptcha.go index 4e6b80b..66b1558 100644 --- a/recaptcha/recaptcha.go +++ b/recaptcha/recaptcha.go @@ -12,7 +12,7 @@ import ( "github.com/gorilla/securecookie" "github.com/play-with-docker/play-with-docker/config" - "github.com/twinj/uuid" + "github.com/rs/xid" ) func GetGoogleRecaptchaSiteKey() string { @@ -79,7 +79,7 @@ func IsHuman(req *http.Request, rw http.ResponseWriter) bool { return false } - encoded, _ := s.Encode("session_id", uuid.NewV4().String()) + encoded, _ := s.Encode("session_id", xid.New().String()) http.SetCookie(rw, &http.Cookie{ Name: "session_id", Value: encoded, diff --git a/storage/file.go b/storage/file.go index a27e465..1925c29 100644 --- a/storage/file.go +++ b/storage/file.go @@ -112,7 +112,20 @@ func (store *storage) InstanceCreate(sessionId string, instance *types.Instance) } func (store *storage) InstanceDelete(sessionId, name string) error { - panic("not implemented") + store.rw.Lock() + defer store.rw.Unlock() + + s, found := store.db[sessionId] + if !found { + return fmt.Errorf("Session %s", notFound) + } + + if _, found := s.Instances[name]; !found { + return nil + } + delete(s.Instances, name) + + return store.save() } func (store *storage) SessionCount() (int, error) { diff --git a/www/assets/app.js b/www/assets/app.js index c04d774..0f750ef 100644 --- a/www/assets/app.js +++ b/www/assets/app.js @@ -81,7 +81,7 @@ } $scope.resize = function(geometry) { - $scope.socket.emit('viewport resize', geometry.cols, geometry.rows); + $scope.socket.emit('instance viewport resize', geometry.cols, geometry.rows); } KeyboardShortcutService.setResizeFunc($scope.resize); @@ -163,7 +163,7 @@ $scope.builderTerminal.write(data); }); - socket.on('terminal out', function(name, data) { + socket.on('instance terminal out', function(name, data) { var instance = $scope.idx[name]; if (!instance) { @@ -183,10 +183,7 @@ $scope.isAlive = false; }); - socket.on('viewport', function(rows, cols) { - }); - - socket.on('new instance', function(name, ip, hostname) { + socket.on('instance new', function(name, ip, hostname) { $scope.upsertInstance({ name: name, ip: ip, hostname: hostname }); $scope.$apply(function() { if ($scope.instances.length == 1) { @@ -195,12 +192,12 @@ }); }); - socket.on('delete instance', function(name) { + socket.on('instance delete', function(name) { $scope.removeInstance(name); $scope.$apply(); }); - socket.on('viewport resize', function(cols, rows) { + socket.on('instance viewport resize', function(cols, rows) { // viewport has changed, we need to resize all terminals $scope.instances.forEach(function(instance) { @@ -342,7 +339,7 @@ }, 4); term.on('data', function(d) { - $scope.socket.emit('terminal in', instance.name, d); + $scope.socket.emit('instance terminal in', instance.name, d); }); instance.term = term;