|
1 | 1 | package api
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "adserver/cache" |
| 5 | + "adserver/util" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "log" |
4 | 9 | "net/http"
|
5 | 10 |
|
6 | 11 | "github.com/gin-gonic/gin"
|
| 12 | + "github.com/google/uuid" |
7 | 13 | )
|
8 | 14 |
|
9 |
| -type RequestParams struct { |
10 |
| - AdUnitId string `json:"adunit_id"` |
11 |
| - PublisherId string `json:"publisher_id"` |
12 |
| -} |
13 |
| - |
14 |
| -type Native struct { |
15 |
| - Request struct { |
16 |
| - Ver string `json:"ver"` |
17 |
| - Assets []struct { |
18 |
| - ID int `json:"id"` |
19 |
| - Required int `json:"required"` |
20 |
| - Img struct { |
21 |
| - Type int `json:"type"` |
22 |
| - W int `json:"w"` |
23 |
| - H int `json:"h"` |
24 |
| - } `json:"img"` |
25 |
| - } `json:"assets"` |
26 |
| - } `json:"request"` |
27 |
| - Ver string `json:"ver"` |
28 |
| -} |
29 |
| - |
30 |
| -type Impression struct { |
31 |
| - BidFloorCur string `json:"bidfloorcur"` |
32 |
| - ID string `json:"id"` |
33 |
| - Native Native `json:"native"` |
34 |
| -} |
35 |
| - |
36 |
| -type RequestBody struct { |
37 |
| - DPL string `json:"dpl"` |
38 |
| - ID string `json:"id"` |
39 |
| - Imp []Impression `json:"imp"` |
40 |
| -} |
41 |
| - |
42 | 15 | func (server *Server) adserve(ctx *gin.Context) {
|
43 |
| - var reqParams RequestParams |
| 16 | + var reqParams cache.RequestParams |
44 | 17 | if err := ctx.ShouldBindQuery(&reqParams); err != nil {
|
45 | 18 | ctx.JSON(http.StatusBadRequest, errResponse(err))
|
46 | 19 | return
|
47 | 20 | }
|
48 |
| - |
49 |
| - var reqBody RequestBody |
| 21 | + // fmt.Println("reqParams: ", reqParams) |
| 22 | + var reqBody cache.RequestBody |
50 | 23 | if err := ctx.ShouldBindJSON(&reqBody); err != nil {
|
51 | 24 | ctx.JSON(http.StatusBadRequest, errResponse(err))
|
52 | 25 | return
|
53 | 26 | }
|
54 |
| - adUnitAdress := server.config.AdManagerAddress + "/adunit?" + "adunit_id=" + reqParams.AdUnitId |
55 |
| - publisherAdress := server.config.AdManagerAddress + "/publisher?" + "publisher_id=" + reqParams.PublisherId |
| 27 | + // adUnitAdress := server.config.AdManagerAddress + "/adunit?" + "adunit_id=" + reqParams.AdUnitId |
| 28 | + // publisherAdress := server.config.AdManagerAddress + "/publisher?" + "publisher_id=" + reqParams.PublisherId |
56 | 29 |
|
57 |
| - resp, err := http.Get(publisherAdress) |
58 |
| - if err != nil { |
59 |
| - ctx.JSON(http.StatusInternalServerError, errResponse(err)) |
60 |
| - return |
61 |
| - } |
62 |
| - defer resp.Body.Close() |
| 30 | + // resp, err := http.Get(publisherAdress) |
| 31 | + // if err != nil { |
| 32 | + // ctx.JSON(http.StatusInternalServerError, errResponse(err)) |
| 33 | + // return |
| 34 | + // } |
| 35 | + // defer resp.Body.Close() |
63 | 36 |
|
64 |
| - if resp.StatusCode != http.StatusOK { |
65 |
| - ctx.JSON(http.StatusNotFound, errResponse(err)) |
66 |
| - return |
67 |
| - } |
| 37 | + // if resp.StatusCode != http.StatusOK { |
| 38 | + // ctx.JSON(http.StatusNotFound, errResponse(err)) |
| 39 | + // return |
| 40 | + // } |
| 41 | + |
| 42 | + // resp, err = http.Get(adUnitAdress) |
| 43 | + // if err != nil { |
| 44 | + // ctx.JSON(http.StatusInternalServerError, errResponse(err)) |
| 45 | + // return |
| 46 | + // } |
| 47 | + // defer resp.Body.Close() |
68 | 48 |
|
69 |
| - resp, err = http.Get(adUnitAdress) |
| 49 | + campaignKey := "campaigns" |
| 50 | + var campaignList []cache.Campaign |
| 51 | + campaigns, err := server.store.Get(campaignKey) |
70 | 52 | if err != nil {
|
71 | 53 | ctx.JSON(http.StatusInternalServerError, errResponse(err))
|
72 | 54 | return
|
73 | 55 | }
|
74 |
| - defer resp.Body.Close() |
| 56 | + json.Unmarshal([]byte(campaigns), &campaignList) |
| 57 | + var activeCampaigns []cache.Campaign |
| 58 | + var activeAds []cache.Ad |
| 59 | + var rankedAds []cache.Ad |
| 60 | + var bids *[]cache.Bid |
| 61 | + for _, campaign := range campaignList { |
| 62 | + startDate, err := util.GetTime(campaign.StartDate) |
| 63 | + if err != nil { |
| 64 | + ctx.JSON(http.StatusInternalServerError, errResponse(err)) |
| 65 | + return |
| 66 | + } |
| 67 | + endDate, err := util.GetTime(campaign.EndDate) |
| 68 | + if err != nil { |
| 69 | + ctx.JSON(http.StatusInternalServerError, errResponse(err)) |
| 70 | + return |
| 71 | + } |
| 72 | + if util.WithinDuration(startDate, endDate) { |
| 73 | + activeCampaigns = append(activeCampaigns, campaign) |
| 74 | + hkey := campaign.CampaignID |
| 75 | + ads, err := server.store.HGetAll(hkey) |
| 76 | + if err != nil { |
| 77 | + ctx.JSON(http.StatusInternalServerError, errResponse(err)) |
| 78 | + return |
| 79 | + } |
| 80 | + // fmt.Println("number of ads in campaign: ", len(ads)) |
| 81 | + for _, ad := range ads { |
| 82 | + var adObj cache.Ad |
| 83 | + json.Unmarshal([]byte(ad), &adObj) |
| 84 | + // fmt.Println("checking ad: ", adObj.AdID) |
| 85 | + adAvailable, err := util.IsAdAvailable(adObj, reqParams, reqBody) |
| 86 | + if err != nil { |
| 87 | + ctx.JSON(http.StatusInternalServerError, errResponse(err)) |
| 88 | + return |
| 89 | + } |
| 90 | + fmt.Println("can ad ", adObj.AdID, " be served: ", adAvailable) |
| 91 | + if adAvailable { |
| 92 | + activeAds = append(activeAds, adObj) |
| 93 | + } |
| 94 | + } |
75 | 95 |
|
| 96 | + // fmt.Println("no of active ads: ", len(activeAds)) |
| 97 | + rankedAds = util.RankAds(activeAds) |
| 98 | + fmt.Println("ranked ads: ", rankedAds) |
| 99 | + bidParams := cache.BidParams{ |
| 100 | + RankedAds: &rankedAds, |
| 101 | + RequestBody: reqBody, |
| 102 | + } |
| 103 | + bids, err = server.getBids(bidParams) |
| 104 | + if err != nil { |
| 105 | + log.Println(err.Error()) |
| 106 | + ctx.JSON(http.StatusInternalServerError, errResponse(err)) |
| 107 | + return |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + adServeResponse := cache.AdServeResponse{ |
| 112 | + Id: uuid.New().String(), |
| 113 | + Bid: *bids, |
| 114 | + Bidid: uuid.New().String(), |
| 115 | + Cur: "USD", |
| 116 | + } |
| 117 | + // fmt.Println("activeAds: ", activeAds) |
| 118 | + // fmt.Println("activeCampaigns: ", activeCampaigns) |
| 119 | + ctx.JSON(http.StatusOK, adServeResponse) |
76 | 120 | }
|
0 commit comments