Skip to content

Commit

Permalink
update query parser
Browse files Browse the repository at this point in the history
  • Loading branch information
wenweihu86 committed Oct 28, 2017
1 parent b2a2d7e commit 9ee0a37
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
58 changes: 29 additions & 29 deletions adhandler/search_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package adhandler
import (
"encoding/json"
"github.com/valyala/fasthttp"
"strconv"
"time"
"math/rand"
"github.com/wenweihu86/ad-server/adserver"
Expand All @@ -15,45 +14,45 @@ import (
)

func SearchHandler(ctx *fasthttp.RequestCtx) {
beginTime := time.Now().Nanosecond()
args := ctx.QueryArgs()
req := new(adserver.Request)
// slot_id
if len(args.Peek("slot_id")) > 0 {
slotId, err := strconv.ParseUint(string(args.Peek("slot_id")), 10, 32)
if err != nil {
ctx.SetBody([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
req.SlotId = uint32(slotId)
slotId, err := args.GetUint("slot_id")
if err != nil {
ctx.SetBody([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
req.SlotId = uint32(slotId)

// ad_num
if len(args.Peek("ad_num")) > 0 {
reqAdNum, err := strconv.ParseUint(string(args.Peek("ad_num")), 10, 32)
if err != nil {
ctx.SetBody([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
req.ReqAdNum = uint32(reqAdNum)
reqAdNum, err := args.GetUint("ad_num")
if err != nil {
ctx.SetBody([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
req.ReqAdNum = uint32(reqAdNum)

// ip
if len(args.Peek("ip")) > 0 {
if args.Has("ip") {
req.Ip = string(args.Peek("ip"))
}

// device_id
if len(args.Peek("device_id")) > 0 {
if args.Has("device_id") {
req.DeviceId = string(args.Peek("device_id"))
}

// os
if len(args.Peek("os")) > 0 {
os, err := strconv.ParseUint(string(args.Peek("os")), 10, 32)
if err != nil {
ctx.SetBody([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
req.Os = uint32(os)
os, err := args.GetUint("os")
if err != nil {
ctx.SetBody([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
req.Os = uint32(os)

// os_version
if len(args.Peek("os_version")) > 0 {
if args.Has("os_version") {
req.OsVersion = string(args.Peek("os_version"))
}

Expand Down Expand Up @@ -141,11 +140,12 @@ func SearchHandler(ctx *fasthttp.RequestCtx) {
res.ResCode = 0
res.AdList = make([]adserver.AdInfo, 0, 1)
}
elapseTimeMs := (time.Now().Nanosecond() - beginTime) / 1000000
adserver.SearchLog.Info(fmt.Sprintf(
"searchId=%s slotId=%d adNum=%d iP=%s deviceId=%s oS=%d osVersion=%s " +
"unitId=%s creativeId=%s resAdNum=%d\n",
req.SearchId, req.SlotId, req.ReqAdNum, req.Ip, req.DeviceId, req.Os, req.OsVersion,
unitIdsStr, creativeIdsStr,resAdNum))
"searchId=%s elpaseMs=%d slotId=%d reqAdNum=%d resAdNum=%d " +
"iP=%s deviceId=%s oS=%d osVersion=%s unitId=%s creativeId=%s\n",
req.SearchId, elapseTimeMs, req.SlotId, req.ReqAdNum, resAdNum,
req.Ip, req.DeviceId, req.Os, req.OsVersion, unitIdsStr, creativeIdsStr))

resBytes, err := json.Marshal(res)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion adserver/location_dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (locationDict *IpDict) StartReloadTimer() {
locationCurrentModifiedTime := locationFileStat.ModTime().Unix()
// 如果文件有更新,则重新加载广告内容
if blockCurrentModifiedTime > locationDict.BlockLastModifiedTime || locationCurrentModifiedTime > locationDict.LocationLastModifiedTime {
AdServerLog.Info(fmt.Sprintf("start reload ad info dict at %s",
AdServerLog.Info(fmt.Sprintf("start reload location info dict at %s",
t1.Format("2006-01-02 03:04:05")))
ipDataInfo, err := LoadLocationDict(
GlobalConfObject.GeoBlockFileName,
Expand Down

0 comments on commit 9ee0a37

Please sign in to comment.