|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | +"encoding/json" |
| 5 | +"fmt" |
| 6 | +"net/http" |
| 7 | + |
| 8 | +log "github.com/sirupsen/logrus" |
| 9 | + |
| 10 | +"github.com/qa-dev/jsonwire-grid/pool" |
| 11 | +) |
| 12 | + |
| 13 | +// SessionInfo - Returns a session info (node address, status, etc) |
| 14 | +type SessionInfo struct { |
| 15 | +Pool *pool.Pool |
| 16 | +} |
| 17 | + |
| 18 | +type sessionInfoResponse struct { |
| 19 | +NodeAddress string `json:"node_address"` |
| 20 | +NodeType pool.NodeType `json:"node_type"` |
| 21 | +Status pool.NodeStatus `json:"node_status"` |
| 22 | +SessionID string `json:"session_id"` |
| 23 | +Updated int64 `json:"updated"` |
| 24 | +Registered int64 `json:"registered"` |
| 25 | +} |
| 26 | + |
| 27 | +func (h *SessionInfo) ServeHTTP(rw http.ResponseWriter, r *http.Request) { |
| 28 | +sessionId := r.URL.Query().Get("sessionid") |
| 29 | +if sessionId == "" { |
| 30 | +http.Error(rw, fmt.Sprint("session id must be specified,"), http.StatusBadRequest) |
| 31 | +return |
| 32 | +} |
| 33 | +node, err := h.Pool.GetNodeBySessionID(sessionId) |
| 34 | +if err != nil { |
| 35 | +http.Error(rw, fmt.Sprint("trying to get a session data,", err), http.StatusInternalServerError) |
| 36 | +return |
| 37 | +} |
| 38 | + |
| 39 | +resp := sessionInfoResponse{ |
| 40 | +node.Address, |
| 41 | +node.Type, |
| 42 | +node.Status, |
| 43 | +node.SessionID, |
| 44 | +node.Updated, |
| 45 | +node.Registered, |
| 46 | +} |
| 47 | +respJSON, err := json.Marshal(resp) |
| 48 | +if err != nil { |
| 49 | +http.Error(rw, fmt.Sprint("trying to encode a response,", err), http.StatusInternalServerError) |
| 50 | +return |
| 51 | +} |
| 52 | + |
| 53 | +_, err = rw.Write(respJSON) |
| 54 | +if err != nil { |
| 55 | +log.Error("session/info: write a response,", err) |
| 56 | +} |
| 57 | +} |
0 commit comments