Browse Source

支持热重启

jiantaoli 4 years ago
parent
commit
7d34dcd896
2 changed files with 39 additions and 33 deletions
  1. 0 29
      ads_handler.go
  2. 39 4
      main.go

+ 0 - 29
ads_handler.go

@@ -4,7 +4,6 @@ import (
4
 	"encoding/json"
4
 	"encoding/json"
5
 	"fmt"
5
 	"fmt"
6
 	"github.com/gin-gonic/gin"
6
 	"github.com/gin-gonic/gin"
7
-	"io"
8
 	"math/rand"
7
 	"math/rand"
9
 	"miads/adslib"
8
 	"miads/adslib"
10
 	"miads/adslib/addata"
9
 	"miads/adslib/addata"
@@ -17,12 +16,10 @@ import (
17
 	"miads/adslib/redis_data"
16
 	"miads/adslib/redis_data"
18
 	"miads/adslib/utils"
17
 	"miads/adslib/utils"
19
 	"net/url"
18
 	"net/url"
20
-	"os"
21
 	"strconv"
19
 	"strconv"
22
 	"strings"
20
 	"strings"
23
 	"time"
21
 	"time"
24
 
22
 
25
-	"github.com/lestrrat-go/file-rotatelogs"
26
 	log "github.com/sirupsen/logrus"
23
 	log "github.com/sirupsen/logrus"
27
 )
24
 )
28
 
25
 
@@ -33,33 +30,7 @@ type Response struct {
33
 	addata.AdData
30
 	addata.AdData
34
 }
31
 }
35
 
32
 
36
-func setupLogger(logName string, logLevel log.Level) {
37
-	writer, err := rotatelogs.New(
38
-		logName+".%Y%m%d",
39
-
40
-		// WithLinkName为最新的日志建立软连接,以方便随着找到当前日志文件
41
-		rotatelogs.WithLinkName(logName),
42
-
43
-		// WithRotationTime设置日志分割的时间,这里设置为一小时分割一次
44
-		rotatelogs.WithRotationTime(time.Hour),
45
-
46
-		// WithMaxAge和WithRotationCount二者只能设置一个,
47
-		// WithMaxAge设置文件清理前的最长保存时间,
48
-		// WithRotationCount设置文件清理前最多保存的个数。
49
-		rotatelogs.WithMaxAge(time.Hour*24*3),
50
-	)
51
-
52
-	if err != nil {
53
-		log.Errorf("config local file system for logger error: %v", err)
54
-	}
55
-
56
-	log.SetReportCaller(true)
57
-	log.SetLevel(logLevel)
58
-	log.SetOutput(io.MultiWriter(writer, os.Stdout))
59
-}
60
-
61
 func adsHandler(c *gin.Context) {
33
 func adsHandler(c *gin.Context) {
62
-	setupLogger(adslib.GetConf().LogPath+"/ads.log", log.TraceLevel)
63
 	c.Header("Content-Type", "application/json")
34
 	c.Header("Content-Type", "application/json")
64
 
35
 
65
 	request := adslib.Request{}
36
 	request := adslib.Request{}

+ 39 - 4
main.go

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