main.go 1.4 KB

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