Browse Source

支持windows

jiantaoli 4 years ago
parent
commit
9e8e2836e6
4 changed files with 61 additions and 6 deletions
  1. 4 6
      ads_handler.go
  2. 3 0
      adslib/redis_data/redis_data.go
  3. 0 0
      main_linux.go
  4. 54 0
      main_windows.go

+ 4 - 6
ads_handler.go

@@ -146,12 +146,10 @@ func adsHandler(c *gin.Context) {
146 146
 		}
147 147
 
148 148
 		log.WithField("request_id", dspInfo.RequestId).Infof("freq control conf: %+v", freqControlConf)
149
-		if freqControlConf != nil {
150
-			hour, _ := strconv.Atoi(time.Now().Format("01"))
151
-			tmpControlInterval, ok := freqControlConf.GetControlTime(hour)
152
-			if ok {
153
-				freqControlInterval = tmpControlInterval
154
-			}
149
+		hour, _ := strconv.Atoi(time.Now().Format("01"))
150
+		tmpControlInterval, ok := freqControlConf.GetControlTime(hour)
151
+		if ok {
152
+			freqControlInterval = tmpControlInterval
155 153
 		}
156 154
 	}
157 155
 

+ 3 - 0
adslib/redis_data/redis_data.go

@@ -27,6 +27,9 @@ type FreqControlConf struct {
27 27
 }
28 28
 
29 29
 func (self *FreqControlConf) GetControlTime(hour int) (int, bool) {
30
+	if self.confs == nil {
31
+		return 0, false
32
+	}
30 33
 	for _, v := range self.confs {
31 34
 		if v.BeginHour <= hour && hour <= v.EndHour {
32 35
 			return v.FreqTime, true

main.go → main_linux.go


+ 54 - 0
main_windows.go

@@ -0,0 +1,54 @@
1
+package main
2
+
3
+import (
4
+	"github.com/gin-gonic/gin"
5
+	rotatelogs "github.com/lestrrat-go/file-rotatelogs"
6
+	"io"
7
+	"miads/adslib"
8
+	"miads/adslib/ads_redis"
9
+	"os"
10
+	"time"
11
+
12
+	log "github.com/sirupsen/logrus"
13
+)
14
+
15
+func setupLogger(logName string, logLevel log.Level) {
16
+	writer, err := rotatelogs.New(
17
+		logName+".%Y%m%d",
18
+
19
+		// WithLinkName为最新的日志建立软连接,以方便随着找到当前日志文件
20
+		rotatelogs.WithLinkName(logName),
21
+
22
+		// WithRotationTime设置日志分割的时间,这里设置为一小时分割一次
23
+		rotatelogs.WithRotationTime(time.Hour),
24
+
25
+		// WithMaxAge和WithRotationCount二者只能设置一个,
26
+		// WithMaxAge设置文件清理前的最长保存时间,
27
+		// WithRotationCount设置文件清理前最多保存的个数。
28
+		rotatelogs.WithMaxAge(time.Hour*24*3),
29
+	)
30
+
31
+	if err != nil {
32
+		log.Errorf("config local file system for logger error: %v", err)
33
+	}
34
+
35
+	log.SetReportCaller(true)
36
+	log.SetLevel(logLevel)
37
+	log.SetOutput(io.MultiWriter(writer, os.Stdout))
38
+}
39
+
40
+func setupRouter() *gin.Engine {
41
+	r := gin.Default()
42
+
43
+	r.POST("/ads", adsHandler)
44
+	return r
45
+}
46
+
47
+func main() {
48
+	ads_redis.Setup()
49
+	adslib.GetConf()
50
+	setupLogger(adslib.GetConf().LogPath+"/ads.log", log.TraceLevel)
51
+
52
+	r := setupRouter()
53
+	r.Run(":8080")
54
+}