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:
21
handlers/delete_instance.go
Normal file
21
handlers/delete_instance.go
Normal 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
36
handlers/exec.go
Normal 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
22
handlers/get_session.go
Normal 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
23
handlers/new_instance.go
Normal 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
19
handlers/new_session.go
Normal 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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user