|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + log "github.com/Sirupsen/logrus" |
| 8 | + "jsonwire-grid/jsonwire" |
| 9 | + "jsonwire-grid/pool" |
| 10 | + "jsonwire-grid/proxy" |
| 11 | + "jsonwire-grid/selenium" |
| 12 | + "jsonwire-grid/wda" |
| 13 | + "io/ioutil" |
| 14 | + "net/http" |
| 15 | + "net/http/httputil" |
| 16 | + "net/url" |
| 17 | +) |
| 18 | + |
| 19 | +type CreateSession struct { |
| 20 | + Pool *pool.Pool |
| 21 | +} |
| 22 | + |
| 23 | +func (h *CreateSession) ServeHTTP(rw http.ResponseWriter, r *http.Request) { |
| 24 | + if r.Method != http.MethodPost { |
| 25 | + http.Error(rw, "Method Not Allowed", http.StatusMethodNotAllowed) |
| 26 | + return |
| 27 | + } |
| 28 | + var capabilities map[string]Capabilities |
| 29 | + body, err := ioutil.ReadAll(r.Body) |
| 30 | + if err != nil { |
| 31 | + errorMessage := "Error reading request: " + err.Error() |
| 32 | + log.Error(errorMessage) |
| 33 | + http.Error(rw, errorMessage, http.StatusInternalServerError) |
| 34 | + return |
| 35 | + } |
| 36 | + err = r.Body.Close() |
| 37 | + if err != nil { |
| 38 | + log.Error(err.Error()) |
| 39 | + http.Error(rw, err.Error(), http.StatusInternalServerError) |
| 40 | + return |
| 41 | + } |
| 42 | + rc := ioutil.NopCloser(bytes.NewReader(body)) |
| 43 | + r.Body = rc |
| 44 | + log.Infof("requested session with params: %s", string(body)) |
| 45 | + err = json.Unmarshal(body, &capabilities) |
| 46 | + if err != nil { |
| 47 | + errorMessage := "Error unmarshal json: " + err.Error() |
| 48 | + log.Error(errorMessage) |
| 49 | + http.Error(rw, errorMessage, http.StatusInternalServerError) |
| 50 | + return |
| 51 | + } |
| 52 | + desiredCapabilities, ok := capabilities["desiredCapabilities"] |
| 53 | + if !ok { |
| 54 | + errorMessage := "Not passed 'desiredCapabilities'" |
| 55 | + log.Error(errorMessage) |
| 56 | + http.Error(rw, errorMessage, http.StatusInternalServerError) |
| 57 | + return |
| 58 | + } |
| 59 | + poolCapabilities := pool.Capabilities(desiredCapabilities) |
| 60 | + tw, err := h.tryCreateSession(r, &poolCapabilities) |
| 61 | + if err != nil { |
| 62 | + http.Error(rw, "Can't create session: "+err.Error(), http.StatusInternalServerError) |
| 63 | + return |
| 64 | + } |
| 65 | + rw.WriteHeader(tw.StatusCode) |
| 66 | + rw.Write(tw.Output) |
| 67 | +} |
| 68 | + |
| 69 | +func (h *CreateSession) tryCreateSession(r *http.Request, capabilities *pool.Capabilities) (*proxy.ResponseWriter, error) { |
| 70 | + //todo: если запрос отменить, все равно получение ноды будет повторяться, придумать как это предотвратить |
| 71 | + node, err := h.Pool.ReserveAvailableNode(*capabilities) |
| 72 | + if err != nil { |
| 73 | + return nil, errors.New("reserve node error: " + err.Error()) |
| 74 | + } |
| 75 | + //todo: посылать в мониторинг событие, если вернулся не 0 |
| 76 | + seleniumClient, err := createClient(node.Address, capabilities) |
| 77 | + if err != nil { |
| 78 | + return nil, errors.New("create Client error: " + err.Error()) |
| 79 | + } |
| 80 | + seleniumNode := jsonwire.NewNode(seleniumClient) |
| 81 | + _, err = seleniumNode.RemoveAllSessions() |
| 82 | + if err != nil { |
| 83 | + log.Warn("Can't remove all sessions from node, go to next available node: " + node.String()) |
| 84 | + h.Pool.Remove(node) |
| 85 | + return h.tryCreateSession(r, capabilities) |
| 86 | + } |
| 87 | + reverseProxy := httputil.NewSingleHostReverseProxy(&url.URL{ |
| 88 | + Scheme: "http", |
| 89 | + Host: node.Address, |
| 90 | + }) |
| 91 | + transport := proxy.NewCreateSessionTransport(h.Pool, node) |
| 92 | + reverseProxy.Transport = transport |
| 93 | + tw := proxy.NewResponseWriter() |
| 94 | + reverseProxy.ServeHTTP(tw, r) |
| 95 | + |
| 96 | + if !transport.IsSuccess { |
| 97 | + log.Warn("Failure proxy request on node " + node.String() + ": " + string(tw.Output)) |
| 98 | + h.Pool.Remove(node) |
| 99 | + return h.tryCreateSession(r, capabilities) |
| 100 | + } |
| 101 | + |
| 102 | + return tw, nil |
| 103 | +} |
| 104 | + |
| 105 | +func createClient(addr string, capabilities *pool.Capabilities) (jsonwire.ClientInterface, error) { |
| 106 | + if capabilities == nil { |
| 107 | + return nil, errors.New("capabilities must be not nil") |
| 108 | + } |
| 109 | + platformName := (*capabilities)["platformName"] |
| 110 | + switch platformName { |
| 111 | + case "WDA": |
| 112 | + return wda.NewClient(addr), nil |
| 113 | + default: |
| 114 | + return selenium.NewClient(addr), nil |
| 115 | + } |
| 116 | +} |
0 commit comments