1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-10-04 17:33:21 +08:00

Initial commit

This commit is contained in:
Jonathan Leibiusky (@xetorthio)
2016-10-08 03:12:48 +02:00
parent f2ae4344fd
commit dde49d8700
19 changed files with 5031 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
package handlers
import (
"net/http"
"github.com/go-zoo/bone"
"github.com/xetorthio/play-with-docker/services"
)
func DeleteInstance(rw http.ResponseWriter, req *http.Request) {
sessionId := bone.GetValue(req, "sessionId")
instanceId := bone.GetValue(req, "instanceId")
s := services.GetSession(sessionId)
i := services.GetInstance(s, instanceId)
err := services.DeleteInstance(s, i)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
}

36
handlers/exec.go Normal file
View File

@@ -0,0 +1,36 @@
package handlers
import (
"io"
"golang.org/x/net/context"
"golang.org/x/net/websocket"
"github.com/go-zoo/bone"
"github.com/xetorthio/play-with-docker/services"
)
// Echo the data received on the WebSocket.
func Exec(ws *websocket.Conn) {
id := bone.GetValue(ws.Request(), "id")
ctx := context.Background()
conn, err := services.GetExecConnection(id, ctx)
if err != nil {
return
}
defer conn.Close()
go func() {
io.Copy(ws, conn.Reader)
}()
go func() {
io.Copy(conn.Conn, ws)
}()
select {
case <-ctx.Done():
}
//io.Copy(ws, os.Stdout)
//go func() {
//io.Copy(*conn, ws)
//}()
}

22
handlers/get_session.go Normal file
View File

@@ -0,0 +1,22 @@
package handlers
import (
"encoding/json"
"net/http"
"github.com/go-zoo/bone"
"github.com/xetorthio/play-with-docker/services"
)
func GetSession(rw http.ResponseWriter, req *http.Request) {
sessionId := bone.GetValue(req, "sessionId")
session := services.GetSession(sessionId)
if session == nil {
rw.WriteHeader(http.StatusNotFound)
return
}
json.NewEncoder(rw).Encode(session)
}

23
handlers/new_instance.go Normal file
View File

@@ -0,0 +1,23 @@
package handlers
import (
"encoding/json"
"log"
"net/http"
"github.com/go-zoo/bone"
"github.com/xetorthio/play-with-docker/services"
)
func NewInstance(rw http.ResponseWriter, req *http.Request) {
sessionId := bone.GetValue(req, "sessionId")
s := services.GetSession(sessionId)
i, err := services.NewInstance(s)
if err != nil {
log.Println(err)
//TODO: Set a status error
} else {
json.NewEncoder(rw).Encode(i)
}
}

19
handlers/new_session.go Normal file
View File

@@ -0,0 +1,19 @@
package handlers
import (
"fmt"
"log"
"net/http"
"github.com/xetorthio/play-with-docker/services"
)
func NewSession(rw http.ResponseWriter, req *http.Request) {
s, err := services.NewSession()
if err != nil {
log.Println(err)
//TODO: Return some error code
} else {
http.Redirect(rw, req, fmt.Sprintf("/p/%s", s.Id), http.StatusFound)
}
}