mirror of
https://github.com/bingohuang/docker-labs.git
synced 2025-08-05 00:39:50 +08:00
* Add support for setting stacks when creating session * Add exec endpoint and move dns stuff to another package * Rename command and status code
41 lines
727 B
Go
41 lines
727 B
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/play-with-docker/play-with-docker/services"
|
|
)
|
|
|
|
type execRequest struct {
|
|
Cmd []string `json:"command"`
|
|
}
|
|
|
|
type execResponse struct {
|
|
ExitCode int `json:"status_code"`
|
|
}
|
|
|
|
func Exec(rw http.ResponseWriter, req *http.Request) {
|
|
vars := mux.Vars(req)
|
|
instanceName := vars["instanceName"]
|
|
|
|
var er execRequest
|
|
err := json.NewDecoder(req.Body).Decode(&er)
|
|
if err != nil {
|
|
rw.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
code, err := services.Exec(instanceName, er.Cmd)
|
|
|
|
if err != nil {
|
|
log.Println(err)
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
json.NewEncoder(rw).Encode(execResponse{code})
|
|
}
|