PurgeCloudflareServer.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package server
  2. import (
  3. pb "cfTest/cloudflareApi/cache/purge"
  4. "context"
  5. "github.com/cloudflare/cloudflare-go"
  6. "log"
  7. )
  8. type PurgeServer struct {
  9. pb.UnimplementedPurgeCloudflareServer
  10. }
  11. func (s *PurgeServer) PurgeCloudflare(ctx context.Context, in *pb.PurgeRequestCloudflare) (*pb.PurgeReplyCloudflare, error) {
  12. log.Printf("purgeTaskByUrl")
  13. log.Printf("apiKey Received: %v", in.GetApiKey())
  14. log.Printf("apiEmail Received: %v", in.GetApiEmail())
  15. log.Printf("zoneId Received: %v", in.GetZoneId())
  16. log.Printf("purgeList Received: %v", in.GetPurgeList())
  17. // Construct a new API object using a global API key
  18. api, err := cloudflare.New(in.GetApiKey(), in.GetApiEmail())
  19. // alternatively, you can use a scoped API token
  20. // api, err := cloudflare.NewWithAPIToken(os.Getenv("CLOUDFLARE_API_TOKEN"))
  21. if err != nil {
  22. log.Fatal(err)
  23. return &pb.PurgeReplyCloudflare{Result: false}, err
  24. }
  25. // Most API calls require a Context
  26. ctxAPI := context.Background()
  27. id := in.GetZoneId()
  28. // set purgeRequest
  29. fileList := in.GetPurgeList()
  30. pcr := cloudflare.PurgeCacheRequest{
  31. Files: fileList,
  32. }
  33. response, err := api.PurgeCache(ctxAPI, id, pcr)
  34. if err != nil {
  35. return nil, err
  36. }
  37. log.Printf("%v", response.Response.Success)
  38. log.Printf("%v", response.Errors)
  39. return &pb.PurgeReplyCloudflare{Result: response.Response.Success}, nil
  40. }
  41. func (s *PurgeServer) PurgeCloudflareByPrefix(ctx context.Context, in *pb.PurgeRequestCloudflarePrefix) (*pb.PurgeReplyCloudflare, error) {
  42. log.Printf("purgeTaskByPrefix")
  43. log.Printf("apiKey Received: %v", in.GetApiKey())
  44. log.Printf("apiEmail Received: %v", in.GetApiEmail())
  45. log.Printf("zoneId Received: %v", in.GetZoneId())
  46. log.Printf("prefixList Received: %v", in.GetPrefixList())
  47. // Construct a new API object using a global API key
  48. api, err := cloudflare.New(in.GetApiKey(), in.GetApiEmail())
  49. // alternatively, you can use a scoped API token
  50. // api, err := cloudflare.NewWithAPIToken(os.Getenv("CLOUDFLARE_API_TOKEN"))
  51. if err != nil {
  52. log.Fatal(err)
  53. return &pb.PurgeReplyCloudflare{Result: false}, err
  54. }
  55. // Most API calls require a Context
  56. ctxAPI := context.Background()
  57. id := in.GetZoneId()
  58. // set purgeRequest
  59. prefixList := in.GetPrefixList()
  60. pcr := cloudflare.PurgeCacheRequest{
  61. Prefixes: prefixList,
  62. }
  63. response, err := api.PurgeCache(ctxAPI, id, pcr)
  64. if err != nil {
  65. return nil, err
  66. }
  67. log.Printf("%v", response.Response.Success)
  68. log.Printf("%v", response.Errors)
  69. return &pb.PurgeReplyCloudflare{Result: response.Response.Success}, nil
  70. }
  71. func (s *PurgeServer) PurgeCloudflareEverything(ctx context.Context, in *pb.PurgeRequestCloudflareEverything) (*pb.PurgeReplyCloudflare, error) {
  72. log.Printf("purgeEverythingTask")
  73. log.Printf("apiKey Received: %v", in.GetApiKey())
  74. log.Printf("apiEmail Received: %v", in.GetApiEmail())
  75. log.Printf("zoneId Received: %v", in.GetZoneId())
  76. // Construct a new API object using a global API key
  77. api, err := cloudflare.New(in.GetApiKey(), in.GetApiEmail())
  78. // alternatively, you can use a scoped API token
  79. // api, err := cloudflare.NewWithAPIToken(os.Getenv("CLOUDFLARE_API_TOKEN"))
  80. if err != nil {
  81. log.Fatal(err)
  82. return &pb.PurgeReplyCloudflare{Result: false}, err
  83. }
  84. // Most API calls require a Context
  85. ctxAPI := context.Background()
  86. id := in.GetZoneId()
  87. // set purgeRequest
  88. response, err := api.PurgeEverything(ctxAPI, id)
  89. if err != nil {
  90. return nil, err
  91. }
  92. log.Printf("%v", response.Response.Success)
  93. log.Printf("%v", response.Errors)
  94. return &pb.PurgeReplyCloudflare{Result: response.Response.Success}, nil
  95. }