|
@@ -1,23 +1,59 @@
|
1
|
1
|
package main
|
2
|
2
|
|
3
|
3
|
import (
|
|
4
|
+ "context"
|
4
|
5
|
"fmt"
|
5
|
6
|
"github.com/gin-gonic/gin"
|
|
7
|
+ rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
|
8
|
+ "io"
|
6
|
9
|
"miads/adslib"
|
7
|
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
|
46
|
func setupRouter() *gin.Engine {
|
11
|
47
|
r := gin.Default()
|
12
|
48
|
|
13
|
49
|
r.POST("/ads", adsHandler)
|
14
|
|
- r.GET("/ads", adsHandler)
|
15
|
50
|
return r
|
16
|
51
|
}
|
17
|
52
|
|
18
|
53
|
func main() {
|
19
|
54
|
ads_redis.Setup()
|
20
|
55
|
adslib.GetConf()
|
|
56
|
+ setupLogger(adslib.GetConf().LogPath+"/ads.log", log.TraceLevel)
|
21
|
57
|
|
22
|
58
|
if ads_redis.RedisConn.ActiveCount() == 0 {
|
23
|
59
|
fmt.Printf("setup redis failed, no active redis")
|
|
@@ -25,6 +61,5 @@ func main() {
|
25
|
61
|
}
|
26
|
62
|
|
27
|
63
|
r := setupRouter()
|
28
|
|
- // Listen and Server in 0.0.0.0:8080
|
29
|
|
- r.Run(":8080")
|
30
|
|
-}
|
|
64
|
+ endless.ListenAndServe(":8080", r)
|
|
65
|
+}
|