PurgeCloudflareServer.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // Construct a new API object using a global API key
  13. api, err := cloudflare.New(in.GetApiKey(), in.GetApiEmail())
  14. // alternatively, you can use a scoped API token
  15. // api, err := cloudflare.NewWithAPIToken(os.Getenv("CLOUDFLARE_API_TOKEN"))
  16. if err != nil {
  17. log.Fatal(err)
  18. return &pb.PurgeReplyCloudflare{Result: false}, err
  19. }
  20. // Most API calls require a Context
  21. ctxAPI := context.Background()
  22. id := in.GetZoneId()
  23. // set purgeRequest
  24. fileList := in.GetPurgeList()
  25. pcr := cloudflare.PurgeCacheRequest{
  26. Files: fileList,
  27. }
  28. response, err := api.PurgeCache(ctxAPI, id, pcr)
  29. if err != nil {
  30. return nil, err
  31. }
  32. log.Printf("%v", response.Response.Success)
  33. log.Printf("%v", response.Errors)
  34. return &pb.PurgeReplyCloudflare{Result: response.Response.Success}, nil
  35. }
  36. func (s *PurgeServer) PurgeCloudflareByPrefix(ctx context.Context, in *pb.PurgeRequestCloudflarePrefix) (*pb.PurgeReplyCloudflare, error) {
  37. // Construct a new API object using a global API key
  38. api, err := cloudflare.New(in.GetApiKey(), in.GetApiEmail())
  39. // alternatively, you can use a scoped API token
  40. // api, err := cloudflare.NewWithAPIToken(os.Getenv("CLOUDFLARE_API_TOKEN"))
  41. if err != nil {
  42. log.Fatal(err)
  43. return &pb.PurgeReplyCloudflare{Result: false}, err
  44. }
  45. // Most API calls require a Context
  46. ctxAPI := context.Background()
  47. id := in.GetZoneId()
  48. // set purgeRequest
  49. prefixList := in.GetPrefixList()
  50. pcr := cloudflare.PurgeCacheRequest{
  51. Prefixes: prefixList,
  52. }
  53. response, err := api.PurgeCache(ctxAPI, id, pcr)
  54. if err != nil {
  55. return nil, err
  56. }
  57. log.Printf("%v", response.Response.Success)
  58. log.Printf("%v", response.Errors)
  59. return &pb.PurgeReplyCloudflare{Result: response.Response.Success}, nil
  60. }
  61. func (s *PurgeServer) PurgeCloudflareEverything(ctx context.Context, in *pb.PurgeRequestCloudflareEverything) (*pb.PurgeReplyCloudflare, error) {
  62. // Construct a new API object using a global API key
  63. api, err := cloudflare.New(in.GetApiKey(), in.GetApiEmail())
  64. // alternatively, you can use a scoped API token
  65. // api, err := cloudflare.NewWithAPIToken(os.Getenv("CLOUDFLARE_API_TOKEN"))
  66. if err != nil {
  67. log.Fatal(err)
  68. return &pb.PurgeReplyCloudflare{Result: false}, err
  69. }
  70. // Most API calls require a Context
  71. ctxAPI := context.Background()
  72. id := in.GetZoneId()
  73. // set purgeRequest
  74. response, err := api.PurgeEverything(ctxAPI, id)
  75. if err != nil {
  76. return nil, err
  77. }
  78. log.Printf("%v", response.Response.Success)
  79. log.Printf("%v", response.Errors)
  80. return &pb.PurgeReplyCloudflare{Result: response.Response.Success}, nil
  81. }