CloudflareServer.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package main
  2. import (
  3. "cfTest/cloudflareApi/cache/purge"
  4. "cfTest/cloudflareApi/certificate"
  5. "cfTest/cloudflareApi/dns"
  6. "cfTest/cloudflareApi/logPush"
  7. "cfTest/cloudflareApi/zone"
  8. "cfTest/server"
  9. "context"
  10. "fmt"
  11. "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
  12. grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery"
  13. "github.com/sirupsen/logrus"
  14. "google.golang.org/grpc"
  15. "gopkg.in/natefinch/lumberjack.v2"
  16. "io"
  17. "log"
  18. "net"
  19. "os"
  20. )
  21. import "github.com/grpc-ecosystem/go-grpc-middleware"
  22. const (
  23. port = ":50051"
  24. )
  25. // InterceptorLogger adapts logrus logger to interceptor logger.
  26. // This code is simple enough to be copied and not imported.
  27. func InterceptorLogger(l logrus.FieldLogger) logging.Logger {
  28. return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) {
  29. f := make(map[string]any, len(fields)/2)
  30. i := logging.Fields(fields).Iterator()
  31. for i.Next() {
  32. k, v := i.At()
  33. f[k] = v
  34. }
  35. l := l.WithFields(f)
  36. switch lvl {
  37. case logging.LevelDebug:
  38. l.Debug(msg)
  39. case logging.LevelInfo:
  40. l.Info(msg)
  41. case logging.LevelWarn:
  42. l.Warn(msg)
  43. case logging.LevelError:
  44. l.Error(msg)
  45. default:
  46. panic(fmt.Sprintf("unknown level %v", lvl))
  47. }
  48. })
  49. }
  50. func main() {
  51. //日志设置
  52. logger := logrus.New()
  53. loggerOutputs := []io.Writer{os.Stdout}
  54. logFilePath := "CloudflareServer.log"
  55. if len(os.Args) > 1 {
  56. logFilePath = os.Args[1]
  57. }
  58. file, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
  59. if err != nil {
  60. logger.Error(err)
  61. } else {
  62. loggerOutputs = append(loggerOutputs, file)
  63. }
  64. logger.SetOutput(io.MultiWriter(loggerOutputs...))
  65. opts := []logging.Option{
  66. logging.WithLogOnEvents(logging.StartCall, logging.FinishCall, logging.PayloadReceived, logging.PayloadSent),
  67. }
  68. log.SetOutput(&lumberjack.Logger{
  69. Filename: "../logs/CloudflareServer/CFLog.log", //日志文件保存位置
  70. MaxSize: 100, // 单位为 MB,当日志文件大小超过此值时滚动
  71. MaxBackups: 10, // 保留的旧日志文件的最大数量
  72. MaxAge: 30, // 单位为天,保留旧日志文件的最大天数
  73. Compress: true, // 是否压缩旧日志文件
  74. })
  75. log.Println("This log will be managed by lumberjack.")
  76. //监听端口
  77. lis, err := net.Listen("tcp", port)
  78. if err != nil {
  79. log.Fatalf("failed to listen: %v", err)
  80. }
  81. //注册GRPC中间件
  82. s := grpc.NewServer(
  83. grpc.ChainUnaryInterceptor(grpc_middleware.ChainUnaryServer(
  84. grpc_recovery.UnaryServerInterceptor(),
  85. logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...),
  86. )),
  87. grpc.ChainStreamInterceptor(grpc_middleware.ChainStreamServer(
  88. grpc_recovery.StreamServerInterceptor(),
  89. logging.StreamServerInterceptor(InterceptorLogger(logger), opts...),
  90. )),
  91. )
  92. //注册服务
  93. purge.RegisterPurgeCloudflareServer(s, &server.PurgeServer{})
  94. certificate.RegisterCertificateCloudflareServer(s, &server.CertificateServer{})
  95. zone.RegisterZoneCloudflareServer(s, &server.ZoneServer{})
  96. dns.RegisterDnsCloudflareServer(s, &server.DnsServer{})
  97. logPush.RegisterLogPushCloudflareServer(s, &server.LogPushCloudflareServer{})
  98. //启动服务
  99. log.Printf("server listening at %v", lis.Addr())
  100. if err := s.Serve(lis); err != nil {
  101. log.Fatalf("failed to serve: %v", err)
  102. }
  103. }