mirror of
https://github.com/bingohuang/docker-labs.git
synced 2025-07-14 18:27:25 +08:00
Change import paths
Add session cleanup
This commit is contained in:
parent
0d693b344c
commit
b6b849e2c6
2
api.go
2
api.go
@ -6,9 +6,9 @@ import (
|
|||||||
|
|
||||||
"golang.org/x/net/websocket"
|
"golang.org/x/net/websocket"
|
||||||
|
|
||||||
|
"github.com/franela/play-with-docker/handlers"
|
||||||
"github.com/go-zoo/bone"
|
"github.com/go-zoo/bone"
|
||||||
"github.com/urfave/negroni"
|
"github.com/urfave/negroni"
|
||||||
"github.com/xetorthio/play-with-docker/handlers"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -3,8 +3,8 @@ package handlers
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/franela/play-with-docker/services"
|
||||||
"github.com/go-zoo/bone"
|
"github.com/go-zoo/bone"
|
||||||
"github.com/xetorthio/play-with-docker/services"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func DeleteInstance(rw http.ResponseWriter, req *http.Request) {
|
func DeleteInstance(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
@ -6,8 +6,8 @@ import (
|
|||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
"golang.org/x/net/websocket"
|
"golang.org/x/net/websocket"
|
||||||
|
|
||||||
|
"github.com/franela/play-with-docker/services"
|
||||||
"github.com/go-zoo/bone"
|
"github.com/go-zoo/bone"
|
||||||
"github.com/xetorthio/play-with-docker/services"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Echo the data received on the WebSocket.
|
// Echo the data received on the WebSocket.
|
||||||
|
@ -4,8 +4,8 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/franela/play-with-docker/services"
|
||||||
"github.com/go-zoo/bone"
|
"github.com/go-zoo/bone"
|
||||||
"github.com/xetorthio/play-with-docker/services"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetSession(rw http.ResponseWriter, req *http.Request) {
|
func GetSession(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
@ -5,8 +5,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/franela/play-with-docker/services"
|
||||||
"github.com/go-zoo/bone"
|
"github.com/go-zoo/bone"
|
||||||
"github.com/xetorthio/play-with-docker/services"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewInstance(rw http.ResponseWriter, req *http.Request) {
|
func NewInstance(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/xetorthio/play-with-docker/services"
|
"github.com/franela/play-with-docker/services"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewSession(rw http.ResponseWriter, req *http.Request) {
|
func NewSession(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
@ -4,7 +4,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
ptypes "github.com/xetorthio/play-with-docker/types"
|
ptypes "github.com/franela/play-with-docker/types"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
@ -39,6 +39,16 @@ func CreateNetwork(name string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteNetwork(id string) error {
|
||||||
|
err := c.NetworkRemove(context.Background(), id)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func GetExecConnection(id string, ctx context.Context) (*types.HijackedResponse, error) {
|
func GetExecConnection(id string, ctx context.Context) (*types.HijackedResponse, error) {
|
||||||
conf := types.ExecConfig{Tty: true, AttachStdin: true, AttachStderr: true, AttachStdout: true, Cmd: []string{"sh"}}
|
conf := types.ExecConfig{Tty: true, AttachStdin: true, AttachStderr: true, AttachStdout: true, Cmd: []string{"sh"}}
|
||||||
resp, err := c.ContainerExecCreate(ctx, id, conf)
|
resp, err := c.ContainerExecCreate(ctx, id, conf)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import "github.com/xetorthio/play-with-docker/types"
|
import "github.com/franela/play-with-docker/types"
|
||||||
|
|
||||||
var instances map[string]map[string]*types.Instance
|
var instances map[string]map[string]*types.Instance
|
||||||
|
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/franela/play-with-docker/types"
|
||||||
"github.com/twinj/uuid"
|
"github.com/twinj/uuid"
|
||||||
"github.com/xetorthio/play-with-docker/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var sessions map[string]*types.Session
|
var sessions map[string]*types.Session
|
||||||
@ -19,6 +22,16 @@ func NewSession() (*types.Session, error) {
|
|||||||
//TODO: Store in something like redis
|
//TODO: Store in something like redis
|
||||||
sessions[s.Id] = s
|
sessions[s.Id] = s
|
||||||
|
|
||||||
|
// Schedule cleanup of the session
|
||||||
|
time.AfterFunc(1*time.Minute, func() {
|
||||||
|
for _, i := range s.Instances {
|
||||||
|
if err := DeleteContainer(i.Name); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DeleteNetwork(s.Id)
|
||||||
|
})
|
||||||
|
|
||||||
if err := CreateNetwork(s.Id); err != nil {
|
if err := CreateNetwork(s.Id); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user