CloudflareServer.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "io"
  16. "log"
  17. "net"
  18. "os"
  19. )
  20. import "github.com/grpc-ecosystem/go-grpc-middleware"
  21. const (
  22. port = ":50051"
  23. )
  24. // InterceptorLogger adapts logrus logger to interceptor logger.
  25. // This code is simple enough to be copied and not imported.
  26. func InterceptorLogger(l logrus.FieldLogger) logging.Logger {
  27. return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) {
  28. f := make(map[string]any, len(fields)/2)
  29. i := logging.Fields(fields).Iterator()
  30. for i.Next() {
  31. k, v := i.At()
  32. f[k] = v
  33. }
  34. l := l.WithFields(f)
  35. switch lvl {
  36. case logging.LevelDebug:
  37. l.Debug(msg)
  38. case logging.LevelInfo:
  39. l.Info(msg)
  40. case logging.LevelWarn:
  41. l.Warn(msg)
  42. case logging.LevelError:
  43. l.Error(msg)
  44. default:
  45. panic(fmt.Sprintf("unknown level %v", lvl))
  46. }
  47. })
  48. }
  49. func main() {
  50. //日志设置
  51. logger := logrus.New()
  52. loggerOutputs := []io.Writer{os.Stdout}
  53. logFilePath := "CloudflareServer.log"
  54. if len(os.Args) > 1 {
  55. logFilePath = os.Args[1]
  56. }
  57. file, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
  58. if err != nil {
  59. logger.Error(err)
  60. } else {
  61. loggerOutputs = append(loggerOutputs, file)
  62. }
  63. logger.SetOutput(io.MultiWriter(loggerOutputs...))
  64. opts := []logging.Option{
  65. logging.WithLogOnEvents(logging.StartCall, logging.FinishCall, logging.PayloadReceived, logging.PayloadSent),
  66. }
  67. //监听端口
  68. lis, err := net.Listen("tcp", port)
  69. if err != nil {
  70. log.Fatalf("failed to listen: %v", err)
  71. }
  72. //注册GRPC中间件
  73. s := grpc.NewServer(
  74. grpc.ChainUnaryInterceptor(grpc_middleware.ChainUnaryServer(
  75. grpc_recovery.UnaryServerInterceptor(),
  76. logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...),
  77. )),
  78. grpc.ChainStreamInterceptor(grpc_middleware.ChainStreamServer(
  79. grpc_recovery.StreamServerInterceptor(),
  80. logging.StreamServerInterceptor(InterceptorLogger(logger), opts...),
  81. )),
  82. )
  83. //注册服务
  84. purge.RegisterPurgeCloudflareServer(s, &server.PurgeServer{})
  85. certificate.RegisterCertificateCloudflareServer(s, &server.CertificateServer{})
  86. zone.RegisterZoneCloudflareServer(s, &server.ZoneServer{})
  87. dns.RegisterDnsCloudflareServer(s, &server.DnsServer{})
  88. logPush.RegisterLogPushCloudflareServer(s, &server.LogPushCloudflareServer{})
  89. //启动服务
  90. log.Printf("server listening at %v", lis.Addr())
  91. if err := s.Serve(lis); err != nil {
  92. log.Fatalf("failed to serve: %v", err)
  93. }
  94. }