forked from play-with-docker/play-with-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_session.go
43 lines (36 loc) · 923 Bytes
/
get_session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package handlers
import (
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/play-with-docker/play-with-docker/pwd/types"
"github.com/play-with-docker/play-with-docker/storage"
)
type SessionInfo struct {
*types.Session
Instances map[string]*types.Instance `json:"instances"`
}
func GetSession(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
sessionId := vars["sessionId"]
session, err := core.SessionGet(sessionId)
if err == storage.NotFoundError {
rw.WriteHeader(http.StatusNotFound)
return
} else if err != nil {
rw.WriteHeader(http.StatusNotFound)
return
}
instances, err := core.InstanceFindBySession(session)
if err != nil {
log.Println(err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
is := map[string]*types.Instance{}
for _, i := range instances {
is[i.Name] = i
}
json.NewEncoder(rw).Encode(SessionInfo{session, is})
}