Browse Source

Working version

Need some refactoring, but working.
main
Дмитрий 3 years ago
parent
commit
4d8b56b0b2
  1. 5
      Makefile
  2. 8
      readme.md
  3. 47
      src/client_server/handlers.go
  4. 6
      src/custom_types/partners_address.go
  5. 11
      src/requests_types/response_type.go

5
Makefile

@ -1,2 +1,5 @@
run: run:
go run src/main.go -p 5053 go run src/main.go -p 5053 -d "127.0.0.1:5059"
build:
go build -o bin/simple-choose-ad src/main.go

8
readme.md

@ -0,0 +1,8 @@
# Микросервис для выбора рекламных предложений от партнеров
Запуск
```shell
make build
./bin/simple-choose-ad -p PORT -d "IP:PORT"
```
где `PORT` это порт для входящих запросов, который слушает сервис, а `IP:PORT,IP2:PORT` список рекламных партнеров.

47
src/client_server/handlers.go

@ -11,6 +11,7 @@ import (
"net/http" "net/http"
customtypes "sample-choose-ad/src/custom_types" customtypes "sample-choose-ad/src/custom_types"
req_types "sample-choose-ad/src/requests_types" req_types "sample-choose-ad/src/requests_types"
"sort"
) )
// Create requset body based in incoming reqest `ir` and return // Create requset body based in incoming reqest `ir` and return
@ -79,7 +80,13 @@ func handleRequest(partners []customtypes.PartnersAddress) http.HandlerFunc {
p_body := constructPartnersRequestBody(&incReq) p_body := constructPartnersRequestBody(&incReq)
var partnersRespones []req_types.SuccesResponse // Two data structures:
// partnersRespones for getting price with O(1) complexity
// []prices as slice of actual prices
// var partnersRespones map[float64]req_types.RespImp
partnersRespones := make(map[uint]map[float64]req_types.RespImp)
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", p.Ip, p.Port)
@ -90,7 +97,14 @@ func handleRequest(partners []customtypes.PartnersAddress) http.HandlerFunc {
continue continue
} }
// adding only successful responses // adding only successful responses
partnersRespones = append(partnersRespones, re) for _, r := range re.Imp {
if partnersRespones[r.Id] == nil {
partnersRespones[r.Id] = make(map[float64]req_types.RespImp)
}
partnersRespones[r.Id][r.Price] = r
prices[r.Id] = append(prices[r.Id], r.Price)
}
} }
if len(partnersRespones) == 0 { if len(partnersRespones) == 0 {
@ -98,17 +112,34 @@ func handleRequest(partners []customtypes.PartnersAddress) http.HandlerFunc {
return return
} }
// для каждого tile в incReq надо найти // Sorting prices, now biggest price at index len-1
// maxPrice := 0 for _, p := range prices {
sort.Float64s(p)
}
// var bestOptionRespone req_types.SuccesResponse var bestOptions []req_types.RespImp
var bestOptions []req_types.Imp
_ = bestOptions
// for each tile peak best price
for _, tile := range incReq.Tiles { for _, tile := range incReq.Tiles {
for _, partner := range partnersRespones { last := len(prices[tile.Id]) - 1
biggestPrice := prices[tile.Id][last]
_ = biggestPrice
bestOptions = append(bestOptions, partnersRespones[tile.Id][biggestPrice])
} }
response := req_types.SuccesResponse{
Id: *incReq.Id,
Imp: bestOptions,
}
respJSON, err := json.Marshal(response)
if err != nil {
log.Println(err)
} }
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(respJSON)
} }
} }

6
src/custom_types/partners_address.go

@ -0,0 +1,6 @@
package customtypes
type PartnersAddress struct {
Ip string
Port int64
}

11
src/requests_types/response_type.go

@ -1,11 +1,12 @@
package req_types package req_types
type RespImp struct { type RespImp struct {
Width uint Id uint `json:"id"`
Height uint Width uint `json:"width"`
Tile string Height uint `json:"height"`
Url string Tile string `json:"tile"`
Price float64 Url string `json:"url"`
Price float64 `json:"price"`
} }
// Response from ad partners // Response from ad partners

Loading…
Cancel
Save