Browse Source

Remove dead code

master
Дмитрий 2 years ago
parent
commit
5985eeb241
  1. 3
      .gitignore
  2. 20
      cmd/client_server/client.go
  3. 20
      cmd/client_server/handlers.go
  4. 35
      cmd/client_server/server.go
  5. 1
      cmd/client_server/validators.go
  6. 13
      internal/curl_requests/main.sh
  7. 29
      internal/curl_requests/no_resp_for_imp.json
  8. 19
      internal/curl_requests/simple.json
  9. 19
      internal/curl_requests/skip.json

3
.gitignore vendored

@ -0,0 +1,3 @@
/bin/
/build/
/ТЗ-на-тестовое-задание-на-вакансию-Go-junior-developer.pdf

20
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
}

20
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,

35
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))
}

1
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)

13
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

29
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"
}
}

19
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"
}
}

19
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"
}
}
Loading…
Cancel
Save