|  |  | @ -3,6 +3,7 @@ package clientserver | 
			
		
	
		
		
			
				
					
					|  |  |  | import ( |  |  |  | import ( | 
			
		
	
		
		
			
				
					
					|  |  |  | 	"bytes" |  |  |  | 	"bytes" | 
			
		
	
		
		
			
				
					
					|  |  |  | 	"encoding/json" |  |  |  | 	"encoding/json" | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	"errors" | 
			
		
	
		
		
			
				
					
					|  |  |  | 	"fmt" |  |  |  | 	"fmt" | 
			
		
	
		
		
			
				
					
					|  |  |  | 	"io" |  |  |  | 	"io" | 
			
		
	
		
		
			
				
					
					|  |  |  | 	"io/ioutil" |  |  |  | 	"io/ioutil" | 
			
		
	
	
		
		
			
				
					|  |  | @ -14,6 +15,8 @@ import ( | 
			
		
	
		
		
			
				
					
					|  |  |  | 	"sort" |  |  |  | 	"sort" | 
			
		
	
		
		
			
				
					
					|  |  |  | ) |  |  |  | ) | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | const PARTNER_ENDPOINT = "bid_request" | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | // Create requset body based in incoming reqest `ir` and return
 |  |  |  | // Create requset body based in incoming reqest `ir` and return
 | 
			
		
	
		
		
			
				
					
					|  |  |  | // `OutgoingRequest` as bytes.Reader from marshaled JSON
 |  |  |  | // `OutgoingRequest` as bytes.Reader from marshaled JSON
 | 
			
		
	
		
		
			
				
					
					|  |  |  | func constructPartnersRequestBody(ir *req_types.IncomingRequest) io.Reader { |  |  |  | func constructPartnersRequestBody(ir *req_types.IncomingRequest) io.Reader { | 
			
		
	
	
		
		
			
				
					|  |  | @ -38,44 +41,50 @@ func constructPartnersRequestBody(ir *req_types.IncomingRequest) io.Reader { | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | // Parsing and checking incoming request.
 |  |  |  | // Parsing and checking incoming request.
 | 
			
		
	
		
		
			
				
					
					|  |  |  | func parseAndCheckIncomingRequest(w http.ResponseWriter, r *http.Request) (req_types.IncomingRequest, error) { |  |  |  | func parseAndCheckIncomingRequest(w http.ResponseWriter, r *http.Request) (req_types.IncomingRequest, error) { | 
			
		
	
		
		
			
				
					
					|  |  |  | 	body, _ := ioutil.ReadAll(r.Body) |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 	var inpReqBody req_types.IncomingRequest |  |  |  | 	var inpReqBody req_types.IncomingRequest | 
			
		
	
		
		
			
				
					
					|  |  |  | 	var err error |  |  |  | 	var err error | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	//check request method. Only POST valid.
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	if r.Method == "GET" { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 		return inpReqBody, errors.New("Wrong request method") | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	} | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	// Check if body in incoming request is empty
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 	body, _ := ioutil.ReadAll(r.Body) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 	if json.Unmarshal(body, &inpReqBody) != nil { |  |  |  | 	if json.Unmarshal(body, &inpReqBody) != nil { | 
			
		
	
		
		
			
				
					
					|  |  |  | 		throwHTTPError("WRONG_SCHEMA", 400, &w) |  |  |  | 		return inpReqBody, throwHTTPError("WRONG_SCHEMA", 400, &w) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | 		return inpReqBody, err |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 	} |  |  |  | 	} | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 	// Check if Id is empty
 |  |  |  | 	// Check if Id is empty
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 	if inpReqBody.Id == nil { |  |  |  | 	if inpReqBody.Id == nil { | 
			
		
	
		
		
			
				
					
					|  |  |  | 		throwHTTPError("EMPTY_FIELD", 400, &w) |  |  |  | 		return inpReqBody, throwHTTPError("EMPTY_FIELD", 400, &w) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | 		return inpReqBody, err |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 	} |  |  |  | 	} | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 	// Check if tiles is empty
 |  |  |  | 	// Check if tiles is empty
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 	if len(inpReqBody.Tiles) == 0 { |  |  |  | 	if len(inpReqBody.Tiles) == 0 { | 
			
		
	
		
		
			
				
					
					|  |  |  | 		throwHTTPError("EMPTY_TILES", 400, &w) |  |  |  | 		return inpReqBody, throwHTTPError("EMPTY_TILES", 400, &w) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | 		return inpReqBody, err |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 	} |  |  |  | 	} | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 	// ipv4 validation
 |  |  |  | 	// ipv4 validation
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 	if wrongIPAddresFormat(inpReqBody.Context.Ip) { |  |  |  | 	if wrongIPAddresFormat(inpReqBody.Context.Ip) { | 
			
		
	
		
		
			
				
					
					|  |  |  | 		throwHTTPError("WRONG_SCHEMA", 400, &w) |  |  |  | 		return inpReqBody, throwHTTPError("WRONG_SCHEMA", 400, &w) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | 		return inpReqBody, err |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 	} |  |  |  | 	} | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 	return inpReqBody, err |  |  |  | 	return inpReqBody, err | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  | } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | // Request handler with closure (make request for each partner in `[]partners`).
 |  |  |  | // Request handler with wrapper (make request for each partner in `[]partners`).
 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | func handleRequest(partners []customtypes.PartnersAddress) http.HandlerFunc { |  |  |  | func handleRequest(partners []customtypes.PartnersAddress) http.HandlerFunc { | 
			
		
	
		
		
			
				
					
					|  |  |  | 	return func(w http.ResponseWriter, r *http.Request) { |  |  |  | 	return func(w http.ResponseWriter, r *http.Request) { | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 		// Parse incoming request and return an error, if it's empty
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 		// or contains wrong/empty fields
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 		incReq, err := parseAndCheckIncomingRequest(w, r) |  |  |  | 		incReq, err := parseAndCheckIncomingRequest(w, r) | 
			
		
	
		
		
			
				
					
					|  |  |  | 		if err != nil { |  |  |  | 		if err != nil { | 
			
		
	
		
		
			
				
					
					|  |  |  | 			log.Println(err) |  |  |  | 			log.Println(err) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 			return | 
			
		
	
		
		
			
				
					
					|  |  |  | 		} |  |  |  | 		} | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 		p_body := constructPartnersRequestBody(&incReq) |  |  |  | 		p_body := constructPartnersRequestBody(&incReq) | 
			
		
	
	
		
		
			
				
					|  |  | @ -88,7 +97,7 @@ func handleRequest(partners []customtypes.PartnersAddress) http.HandlerFunc { | 
			
		
	
		
		
			
				
					
					|  |  |  | 		prices := make(map[uint][]float64) |  |  |  | 		prices := make(map[uint][]float64) | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 		for _, p := range partners { |  |  |  | 		for _, p := range partners { | 
			
		
	
		
		
			
				
					
					|  |  |  | 			url := fmt.Sprintf("http://%v:%v", p.Ip, p.Port) |  |  |  | 			url := fmt.Sprintf("http://%v:%v/%v", p.Ip, p.Port, PARTNER_ENDPOINT) | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 			re, err := sendRequest(url, &p_body) |  |  |  | 			re, err := sendRequest(url, &p_body) | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
	
		
		
			
				
					|  |  | 
 |