main.go 1.4 KB

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