1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-07-15 02:37:27 +08:00
docker-labs/handlers/new_instance.go
Marcos Nils 61a0bb4db1 Add support for setting alias when creatign instance. (#140)
* Add support for setting alias when creatign instance.

The POST to create a instance now provides an `alias` field which
then can be used to access the instance services through the following
URL:

`http://<alias>-<short_session>-<port>.<tld>`

When creating a session you can now send an `alias`

* Remove unnecessary function

* Add alias support for DNS resolution
2017-05-11 17:39:17 -03:00

39 lines
734 B
Go

package handlers
import (
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/play-with-docker/play-with-docker/services"
)
func NewInstance(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
sessionId := vars["sessionId"]
body := struct{ ImageName, Alias 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, body.Alias)
if err != nil {
log.Println(err)
rw.WriteHeader(http.StatusInternalServerError)
return
//TODO: Set a status error
} else {
json.NewEncoder(rw).Encode(i)
}
}