diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..560322a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/bin/ +/build/ +/ТЗ-на-тестовое-задание-на-вакансию-Go-junior-developer.pdf diff --git a/cmd/client_server/client.go b/cmd/client_server/client.go index 1931960..8e76823 100644 --- a/cmd/client_server/client.go +++ b/cmd/client_server/client.go @@ -13,6 +13,7 @@ import ( "time" ) +// Make request to partner, born to run as gorutine, 'cuz send partner response into channel func makeRequest(url string, body *[]byte, response chan<- []req_types.RespImp, wg *sync.WaitGroup) { defer wg.Done() @@ -32,35 +33,18 @@ func makeRequest(url string, body *[]byte, response chan<- []req_types.RespImp, } defer resp.Body.Close() - // maybe say smth to client? + // if response not good if resp.StatusCode != 200 { log.Println("Error: status code", resp.StatusCode) return } b, _ := ioutil.ReadAll(resp.Body) - log.Println(string(b)) if err := json.Unmarshal(b, &pResp); err != nil { log.Println("Error: response unmarshalling", err) return } - // try to convert prices to float - - // for _, imp := range pResp.Imp { - // // log.Printf("%v : %T", imp.PriceStr, imp.PriceStr) - // // imp.Price = imp.PriceStr.(float64) - // // switch imp.PriceStr.(type) { - // // case float64: - // // imp.Price = imp.PriceStr.(float64) - // // case string: - // // imp.Price, err = strconv.ParseFloat(imp.PriceStr.(string), 64) - // // if err != nil { - // // log.Println("Pasring price error, ", err) - // // } - // // } - // } - response <- pResp.Imp } diff --git a/cmd/client_server/handlers.go b/cmd/client_server/handlers.go index 9370ce0..1c14420 100644 --- a/cmd/client_server/handlers.go +++ b/cmd/client_server/handlers.go @@ -84,7 +84,7 @@ func handleRequest(partners []customtypes.PartnersAddress) http.HandlerFunc { for r := range responsesCh { respImps = append(respImps, r...) } - // У нас нет одинаковых пар цена и ид + //We have no identical pairs `id` and `price` partnersRespones := make(map[uint]req_types.RespImp) for _, resp := range respImps { if _, exist := partnersRespones[resp.Id]; !exist { @@ -99,17 +99,6 @@ func handleRequest(partners []customtypes.PartnersAddress) http.HandlerFunc { } } - // { drop this - log.Println("respImps") - for _, i := range respImps { - log.Printf("%v : %v", i.Id, i.Price) - } - - log.Println("partnersRespones") - for _, i := range partnersRespones { - log.Printf("%v : %v", i.Id, i.Price) - } - // } var bestOptions []req_types.RespImp // tile.Id == RespImp.Id @@ -121,13 +110,6 @@ func handleRequest(partners []customtypes.PartnersAddress) http.HandlerFunc { } - // if len(bestOptions) == 0 { - // // log.Println("Error: no responses from partners.") - // log.Println("Error: No Content") - // w.WriteHeader(http.StatusNoContent) - // return - // } - response := req_types.SuccesResponse{ Id: *incReq.Id, Imp: bestOptions, diff --git a/cmd/client_server/server.go b/cmd/client_server/server.go index 2287661..bee39a5 100644 --- a/cmd/client_server/server.go +++ b/cmd/client_server/server.go @@ -10,43 +10,8 @@ import ( const MAX_TIME_PER_REQUEST = time.Duration(250 * time.Millisecond) -type customHandler struct { - Parners []customtypes.PartnersAddress - // context? -} - -func (c *customHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - base := r.URL.Path - - switch base { - case "/placements/request": - - default: - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusBadRequest) - } -} - -func newCustomHandler() *customHandler { - return &customHandler{} -} - func StartServer(port string, partners []customtypes.PartnersAddress) { - // mux := http.NewServeMux() - // mux.HandleFunc("/placements/request", handleRequest(partners)) - - // s := &http.Server{ - // ReadTimeout: time.Duration(time.Millisecond * 20), - // WriteTimeout: time.Duration(time.Millisecond * 20), - // Handler: newCustomHandler(), - // } - // s.ListenAndServe() - // h := http.HandleFunc("/placements/request", handleRequest(partners)) h := http.TimeoutHandler(handleRequest(partners), MAX_TIME_PER_REQUEST, "") http.Handle("/placements/request", h) - // http.Handle("/placements/request", handleRequest(partners)) - // http.HandleFunc("/placements/request", decorate(test2)) - log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), nil)) - } diff --git a/cmd/client_server/validators.go b/cmd/client_server/validators.go index 093f7ef..85aab8c 100644 --- a/cmd/client_server/validators.go +++ b/cmd/client_server/validators.go @@ -24,6 +24,7 @@ func wrongIPAddresFormat(ipv4 string) bool { return !re.Match([]byte(ipv4)) } +// Wrapp and throw an error to log client func throwHTTPError(err_text string, code int, w *http.ResponseWriter) error { http.Error(*w, err_text, code) eText := fmt.Sprintf("Error: %d %vr", code, err_text) diff --git a/internal/curl_requests/main.sh b/internal/curl_requests/main.sh new file mode 100755 index 0000000..a29447c --- /dev/null +++ b/internal/curl_requests/main.sh @@ -0,0 +1,13 @@ +#!/bin/zsh +P=$(pwd) +curl -X POST http://127.0.0.1:5053/placements/request \ + -H "Content-Type: application/json" \ + -d @simple.json + +curl -X POST http://127.0.0.1:5053/placements/request \ + -H "Content-Type: application/json" \ + -d @skip.json + +curl -X POST http://127.0.0.1:5053/placements/request \ + -H "Content-Type: application/json" \ + -d @no_resp_for_imp.json diff --git a/internal/curl_requests/no_resp_for_imp.json b/internal/curl_requests/no_resp_for_imp.json new file mode 100644 index 0000000..de26b54 --- /dev/null +++ b/internal/curl_requests/no_resp_for_imp.json @@ -0,0 +1,29 @@ +{ + "id": "test", + "tiles": [ + { + "id": 554, + "width": 100, + "ratio": 1.5 + }, + { + "id": 555, + "width": 300, + "ratio": 1.5 + }, + { + "id": 556, + "width": 300, + "ratio": 2.5 + }, + { + "id": 400, + "width": 100, + "ratio": 2.5 + } + ], + "context": { + "ip": "192.168.1.1", + "user_agent": "curl" + } +} diff --git a/internal/curl_requests/simple.json b/internal/curl_requests/simple.json new file mode 100644 index 0000000..f961e7c --- /dev/null +++ b/internal/curl_requests/simple.json @@ -0,0 +1,19 @@ +{ + "id": "123", + "tiles": [ + { + "id": 1234, + "width": 100, + "ratio": 1.5 + }, + { + "id": 1235, + "width": 300, + "ratio": 1.5 + } + ], + "context": { + "ip": "192.168.1.1", + "user_agent": "curl" + } +} diff --git a/internal/curl_requests/skip.json b/internal/curl_requests/skip.json new file mode 100644 index 0000000..19af0e3 --- /dev/null +++ b/internal/curl_requests/skip.json @@ -0,0 +1,19 @@ +{ + "id": "skip", + "tiles": [ + { + "id": 123, + "width": 100, + "ratio": 1.5 + }, + { + "id": 124, + "width": 300, + "ratio": 1.5 + } + ], + "context": { + "ip": "192.168.1.1", + "user_agent": "curl" + } +}