1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-07-14 01:57:32 +08:00
docker-labs/handlers/new_instance.go

39 lines
706 B
Go

package handlers
import (
"encoding/json"
"log"
"net/http"
"github.com/franela/play-with-docker/services"
"github.com/gorilla/mux"
)
func NewInstance(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
sessionId := vars["sessionId"]
body := struct{ ImageName string }{}
json.NewDecoder(req.Body).Decode(&body)
s := services.GetSession(sessionId)
s.Lock()
defer s.Unlock()
if len(s.Instances) >= 5 {
rw.WriteHeader(http.StatusConflict)
return
}
i, err := services.NewInstance(s, body.ImageName)
if err != nil {
log.Println(err)
rw.WriteHeader(http.StatusInternalServerError)
return
//TODO: Set a status error
} else {
json.NewEncoder(rw).Encode(i)
}
}