PurgeCloudflareServer.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package server
  2. import (
  3. pb "cfTest/cloudflareApi/cache/purge"
  4. "context"
  5. "github.com/cloudflare/cloudflare-go"
  6. "log"
  7. "strings"
  8. )
  9. type PurgeServer struct {
  10. pb.UnimplementedPurgeCloudflareServer
  11. }
  12. func (s *PurgeServer) PurgeCloudflare(ctx context.Context, in *pb.PurgeRequestCloudflare) (*pb.PurgeReplyCloudflare, error) {
  13. if strings.HasPrefix(in.GetApiKey(), TOKEN_PREFIX) {
  14. api, err = cloudflare.NewWithAPIToken(in.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  15. } else {
  16. api, err = cloudflare.New(in.GetApiKey(), in.GetApiEmail())
  17. }
  18. // alternatively, you can use a scoped API token
  19. // api, err := cloudflare.NewWithAPIToken(os.Getenv("CLOUDFLARE_API_TOKEN"))
  20. if err != nil {
  21. log.Fatal(err)
  22. return &pb.PurgeReplyCloudflare{Result: false}, err
  23. }
  24. // Most API calls require a Context
  25. ctxAPI := context.Background()
  26. id := in.GetZoneId()
  27. // set purgeRequest
  28. fileList := in.GetPurgeList()
  29. pcr := cloudflare.PurgeCacheRequest{
  30. Files: fileList,
  31. }
  32. response, err := api.PurgeCache(ctxAPI, id, pcr)
  33. log.Printf("Cloudflare PurgeCloudflare API response: %v err:%s", response, err)
  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. if strings.HasPrefix(in.GetApiKey(), TOKEN_PREFIX) {
  43. api, err = cloudflare.NewWithAPIToken(in.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  44. } else {
  45. api, err = cloudflare.New(in.GetApiKey(), in.GetApiEmail())
  46. }
  47. if err != nil {
  48. log.Fatal(err)
  49. return &pb.PurgeReplyCloudflare{Result: false}, err
  50. }
  51. // Most API calls require a Context
  52. ctxAPI := context.Background()
  53. id := in.GetZoneId()
  54. // set purgeRequest
  55. prefixList := in.GetPrefixList()
  56. pcr := cloudflare.PurgeCacheRequest{
  57. Prefixes: prefixList,
  58. }
  59. response, err := api.PurgeCache(ctxAPI, id, pcr)
  60. log.Printf("Cloudflare PurgeCloudflareByPrefix API response: %v err:%s", response, err)
  61. if err != nil {
  62. return nil, err
  63. }
  64. log.Printf("%v", response.Response.Success)
  65. log.Printf("%v", response.Errors)
  66. return &pb.PurgeReplyCloudflare{Result: response.Response.Success}, nil
  67. }
  68. func (s *PurgeServer) PurgeCloudflareEverything(ctx context.Context, in *pb.PurgeRequestCloudflareEverything) (*pb.PurgeReplyCloudflare, error) {
  69. if strings.HasPrefix(in.GetApiKey(), TOKEN_PREFIX) {
  70. api, err = cloudflare.NewWithAPIToken(in.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  71. } else {
  72. api, err = cloudflare.New(in.GetApiKey(), in.GetApiEmail())
  73. }
  74. if err != nil {
  75. log.Fatal(err)
  76. return &pb.PurgeReplyCloudflare{Result: false}, err
  77. }
  78. // Most API calls require a Context
  79. ctxAPI := context.Background()
  80. id := in.GetZoneId()
  81. // set purgeRequest
  82. response, err := api.PurgeEverything(ctxAPI, id)
  83. log.Printf("Cloudflare PurgeCloudflareEverything API response: %v err:%s", response, err)
  84. if err != nil {
  85. return nil, err
  86. }
  87. log.Printf("%v", response.Response.Success)
  88. log.Printf("%v", response.Errors)
  89. return &pb.PurgeReplyCloudflare{Result: response.Response.Success}, nil
  90. }