Skip to content

Commit

Permalink
add toml config
Browse files Browse the repository at this point in the history
  • Loading branch information
wenweihu86 committed Oct 21, 2017
1 parent 87442ce commit 5416d3c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
4 changes: 4 additions & 0 deletions conf/ad_server.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

GeoBlockFileName = "./data/GeoLiteCity-Blocks.csv"
GeoLocationFileName = "./data/GeoLiteCity-Location.csv"
AdFileName = "./data/ad_info.txt"
36 changes: 36 additions & 0 deletions src/adserver/global_conf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package adserver

import (
"github.com/spf13/viper"
"fmt"
)

type GlobalConf struct {
GeoBlockFileName string
GeoLocationFileName string
AdFileName string
}

var GlobalConfObject *GlobalConf

func init() {
GlobalConfObject = new(GlobalConf)
}

func LoadGlobalConf(configPath, configFileName string) {
viper.AddConfigPath(configPath)
viper.SetConfigName(configFileName)
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("Error reading config file, %s\n", err)
panic(-1)
}
err := viper.Unmarshal(GlobalConfObject)
if err != nil {
fmt.Printf("unable to decode into struct, %v", err)
panic(-1)
}
fmt.Printf("GeoBlockFileName=%s GeoLocationFileName=%s AdFileName=%s\n",
GlobalConfObject.GeoBlockFileName,
GlobalConfObject.GeoLocationFileName,
GlobalConfObject.AdFileName)
}
7 changes: 4 additions & 3 deletions src/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
)

func main() {
adserver.LoadGlobalConf("./conf", "ad_server")
adserver.LoadLocationDict(
"./data/GeoLiteCity-Blocks.csv",
"./data/GeoLiteCity-Location.csv")
adserver.ReadAdDict("./data/ad_info.txt")
adserver.GlobalConfObject.GeoBlockFileName,
adserver.GlobalConfObject.GeoLocationFileName)
adserver.ReadAdDict(adserver.GlobalConfObject.AdFileName)
http.HandleFunc("/ad/search", adhandler.SearchHandler)
http.HandleFunc("/ad/impression",adhandler.ImpressionHandler)
http.HandleFunc("/ad/click",adhandler.ClickHandler)
Expand Down

0 comments on commit 5416d3c

Please sign in to comment.