Skip to content

Commit

Permalink
重命名包&修复uuid生成
Browse files Browse the repository at this point in the history
  • Loading branch information
wenweihu86 committed Jun 13, 2018
1 parent 844cadf commit f3f52c8
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 48 deletions.
2 changes: 1 addition & 1 deletion adserver/ad_dict.go → core/ad_dict.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package adserver
package core

import (
"io"
Expand Down
2 changes: 1 addition & 1 deletion adserver/ad_log.go → core/ad_log.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package adserver
package core

import "github.com/ibbd-dev/go-async-log"

Expand Down
7 changes: 3 additions & 4 deletions adhandler/click_handler.go → core/click_handler.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package adhandler
package core

import (
"github.com/valyala/fasthttp"
"strconv"
"github.com/wenweihu86/ad-server/adserver"
"encoding/base64"
"fmt"
"net/url"
Expand Down Expand Up @@ -84,9 +83,9 @@ func ClickHandler(ctx *fasthttp.RequestCtx) {
if clickUrls, exist := paramMap["click_url"]; exist {
clickUrl = clickUrls[0]
}
adserver.AdServerLog.Debug(fmt.Sprintf("ClickHandler click_url=%s", clickUrl))
AdServerLog.Debug(fmt.Sprintf("ClickHandler click_url=%s", clickUrl))

adserver.ClickLog.Info(fmt.Sprintf(
ClickLog.Info(fmt.Sprintf(
"click=1 searchId=%s slotId=%d ip=%s deviceId=%s os=%d osVersion=%s unit_id=%d creativeId=%d",
searchId, slotId, ip, deviceId, os, osVersion, unitId, creativeId))
ctx.Redirect(clickUrl, fasthttp.StatusFound)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package adhandler
package core

import (
"github.com/valyala/fasthttp"
"strconv"
"github.com/wenweihu86/ad-server/adserver"
"encoding/base64"
"fmt"
"net/url"
Expand Down Expand Up @@ -97,7 +96,7 @@ func ConversionHandler(ctx *fasthttp.RequestCtx) {
creativeId = uint32(tmp)
}

adserver.ConversionLog.Info(fmt.Sprintf(
ConversionLog.Info(fmt.Sprintf(
"conversion=1 searchId=%s slotId=%d ip=%s deviceId=%s os=%d osVersion=%s unit_id=%d creativeId=%d",
searchId, slotId, ip, deviceId, os, osVersion, unitId, creativeId))
res := "{\"status\": 0}"
Expand Down
2 changes: 1 addition & 1 deletion adserver/global_conf.go → core/global_conf.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package adserver
package core

import (
"github.com/spf13/viper"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package adhandler
package core

import (
"github.com/valyala/fasthttp"
"strconv"
"github.com/wenweihu86/ad-server/adserver"
"encoding/base64"
"fmt"
"net/url"
Expand Down Expand Up @@ -97,7 +96,7 @@ func ImpressionHandler(ctx *fasthttp.RequestCtx) {
creativeId = uint32(tmp)
}

adserver.ImpressionLog.Info(fmt.Sprintf(
ImpressionLog.Info(fmt.Sprintf(
"impression=1 searchId=%s slotId=%d ip=%s deviceId=%s os=%d osVersion=%s unit_id=%d creativeId=%d",
searchId, slotId, ip, deviceId, os, osVersion, unitId, creativeId))
res := "{\"status\": 0}"
Expand Down
2 changes: 1 addition & 1 deletion adserver/location_dict.go → core/location_dict.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package adserver
package core

import (
"io"
Expand Down
2 changes: 1 addition & 1 deletion adserver/request.go → core/request.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package adserver
package core

type Request struct {
SearchId string
Expand Down
2 changes: 1 addition & 1 deletion adserver/response.go → core/response.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package adserver
package core

type AdInfo struct {
UnitId uint32
Expand Down
41 changes: 23 additions & 18 deletions adhandler/search_handler.go → core/search_handler.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package adhandler
package core

import (
"encoding/json"
"github.com/valyala/fasthttp"
"time"
"math/rand"
"github.com/wenweihu86/ad-server/adserver"
"strings"
"fmt"
"bytes"
Expand All @@ -16,7 +15,7 @@ import (
func SearchHandler(ctx *fasthttp.RequestCtx) {
beginTime := time.Now().Nanosecond()
args := ctx.QueryArgs()
req := new(adserver.Request)
req := new(Request)
// slot_id
slotId, err := args.GetUint("slot_id")
if err != nil {
Expand Down Expand Up @@ -57,18 +56,24 @@ func SearchHandler(ctx *fasthttp.RequestCtx) {
}

// searchId
req.SearchId = uuid.NewV4().String()
uuid, err := uuid.NewV4()
if err != nil {
AdServerLog.Warn("uuid NewV4 failed")
req.SearchId = string(rand.Uint64())
} else {
req.SearchId = uuid.String()
}

adData := adserver.AdDictObject.GetCurrentAdData()
adData := AdDictObject.GetCurrentAdData()
// search by request ip
var unitIdList1 []uint32
var exist1 bool
ipDataInfo := adserver.LocationDict.GetCurrentIpData()
ipDataInfo := LocationDict.GetCurrentIpData()
locationInfo := ipDataInfo.SearchLocationByIp(req.Ip)
if locationInfo != nil {
country := locationInfo.Country
city := locationInfo.City
adserver.AdServerLog.Debug(fmt.Sprintf(
AdServerLog.Debug(fmt.Sprintf(
"ip=%s country=%s city=%s\n", req.Ip, country, city))
key := strings.ToLower(country) + "_" + strings.ToLower(city)
unitIdList1, exist1 = adData.LocationUnitMap[key]
Expand All @@ -93,8 +98,8 @@ func SearchHandler(ctx *fasthttp.RequestCtx) {
}

// select one from unit id list
var res = &adserver.Response{}
adList := make([]adserver.AdInfo, 0, 1)
var res = &Response{}
adList := make([]AdInfo, 0, 1)
unitIdMap := make(map[int] bool)
var unitIdsStr, creativeIdsStr string
resAdNum := 0
Expand All @@ -112,7 +117,7 @@ func SearchHandler(ctx *fasthttp.RequestCtx) {
unitId := unitIdList[randIndex]
unitInfo := adData.AdUnitMap[unitId]
adCreative := adData.AdCreativeMap[unitInfo.CreativeId]
adInfo := adserver.AdInfo{
adInfo := AdInfo{
UnitId: unitInfo.UnitId,
CreativeId: adCreative.CreativeId,
Title: adCreative.Title,
Expand All @@ -138,10 +143,10 @@ func SearchHandler(ctx *fasthttp.RequestCtx) {
res.AdList = adList
} else {
res.ResCode = 0
res.AdList = make([]adserver.AdInfo, 0, 1)
res.AdList = make([]AdInfo, 0, 1)
}
elapseTimeMs := (time.Now().Nanosecond() - beginTime) / 1000000
adserver.SearchLog.Info(fmt.Sprintf(
SearchLog.Info(fmt.Sprintf(
"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,
Expand All @@ -155,7 +160,7 @@ func SearchHandler(ctx *fasthttp.RequestCtx) {
ctx.SetBody(resBytes)
}

func buildImpressionTrackUrl(req *adserver.Request, adInfo adserver.AdInfo) string {
func buildImpressionTrackUrl(req *Request, adInfo AdInfo) string {
var paramBuf bytes.Buffer
paramBuf.WriteString(fmt.Sprintf("search_id=%s", req.SearchId))
paramBuf.WriteString(fmt.Sprintf("&slot_id=%d", req.SlotId))
Expand All @@ -167,11 +172,11 @@ func buildImpressionTrackUrl(req *adserver.Request, adInfo adserver.AdInfo) stri
paramBuf.WriteString(fmt.Sprintf("&creative_id=%d", adInfo.CreativeId))
paramEncoded := base64.URLEncoding.EncodeToString(paramBuf.Bytes())
impressionTrackUrl := fmt.Sprintf("%s?i=%s",
adserver.GlobalConfObject.ImpressionTrackUrlPrefix, paramEncoded)
GlobalConfObject.ImpressionTrackUrlPrefix, paramEncoded)
return impressionTrackUrl
}

func buildClickTrackUrl(req *adserver.Request, adInfo adserver.AdInfo) string {
func buildClickTrackUrl(req *Request, adInfo AdInfo) string {
var paramBuf bytes.Buffer
paramBuf.WriteString(fmt.Sprintf("search_id=%s", req.SearchId))
paramBuf.WriteString(fmt.Sprintf("&slot_id=%d", req.SlotId))
Expand All @@ -184,11 +189,11 @@ func buildClickTrackUrl(req *adserver.Request, adInfo adserver.AdInfo) string {
paramBuf.WriteString(fmt.Sprintf("&click_url=%s", adInfo.ClickUrl))
paramEncoded := base64.URLEncoding.EncodeToString(paramBuf.Bytes())
impressionTrackUrl := fmt.Sprintf("%s?i=%s",
adserver.GlobalConfObject.ClickTrackUrlPrefix, paramEncoded)
GlobalConfObject.ClickTrackUrlPrefix, paramEncoded)
return impressionTrackUrl
}

func buildConversionTrackUrl(req *adserver.Request, adInfo adserver.AdInfo) string {
func buildConversionTrackUrl(req *Request, adInfo AdInfo) string {
var paramBuf bytes.Buffer
paramBuf.WriteString(fmt.Sprintf("search_id=%s", req.SearchId))
paramBuf.WriteString(fmt.Sprintf("&slot_id=%d", req.SlotId))
Expand All @@ -201,7 +206,7 @@ func buildConversionTrackUrl(req *adserver.Request, adInfo adserver.AdInfo) stri
paramBuf.WriteString(fmt.Sprintf("&click_url=%s", adInfo.ClickUrl))
paramEncoded := base64.URLEncoding.EncodeToString(paramBuf.Bytes())
conversionTrackUrl := fmt.Sprintf("%s?i=%s",
adserver.GlobalConfObject.ConversionTrackUrlPrefix, paramEncoded)
GlobalConfObject.ConversionTrackUrlPrefix, paramEncoded)
return conversionTrackUrl
}

27 changes: 13 additions & 14 deletions main/main.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
package main

import (
"github.com/wenweihu86/ad-server/adserver"
"github.com/wenweihu86/ad-server/adhandler"
"github.com/wenweihu86/ad-server/core"
"os"
"github.com/valyala/fasthttp"
"strconv"
)

func main() {
adserver.LoadGlobalConf("./conf", "ad_server")
adserver.InitLog(adserver.GlobalConfObject)
core.LoadGlobalConf("./conf", "ad_server")
core.InitLog(core.GlobalConfObject)
//加载位置字典
err := adserver.LocationDict.Load()
err := core.LocationDict.Load()
if err != nil {
os.Exit(-1)
}
adserver.LocationDict.StartReloadTimer()
core.LocationDict.StartReloadTimer()

// 初始化并加载广告信息
adserver.AdDictObject = adserver.NewAdDict(adserver.GlobalConfObject.AdFileName)
err = adserver.AdDictObject.Load()
core.AdDictObject = core.NewAdDict(core.GlobalConfObject.AdFileName)
err = core.AdDictObject.Load()
if err != nil {
os.Exit(-1)
}
adserver.AdDictObject.StartReloadTimer()
core.AdDictObject.StartReloadTimer()

requestHandler := func(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/ad/search":
adhandler.SearchHandler(ctx)
core.SearchHandler(ctx)
case "/ad/impression":
adhandler.ImpressionHandler(ctx)
core.ImpressionHandler(ctx)
case "/ad/click":
adhandler.ClickHandler(ctx)
core.ClickHandler(ctx)
case "/ad/conversion":
adhandler.ConversionHandler(ctx)
core.ConversionHandler(ctx)
default:
ctx.Error("Unsupported path", fasthttp.StatusNotFound)
}
}
listenPort := ":" + strconv.Itoa(adserver.GlobalConfObject.AdServerPort)
listenPort := ":" + strconv.Itoa(core.GlobalConfObject.AdServerPort)
fasthttp.ListenAndServe(listenPort, requestHandler)
}

0 comments on commit f3f52c8

Please sign in to comment.