PurgeCloudflareServer.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. if err != nil {
  34. return nil, err
  35. }
  36. log.Printf("%v", response.Response.Success)
  37. log.Printf("%v", response.Errors)
  38. return &pb.PurgeReplyCloudflare{Result: response.Response.Success}, nil
  39. }
  40. func (s *PurgeServer) PurgeCloudflareByPrefix(ctx context.Context, in *pb.PurgeRequestCloudflarePrefix) (*pb.PurgeReplyCloudflare, error) {
  41. if strings.HasPrefix(in.GetApiKey(), TOKEN_PREFIX) {
  42. api, err = cloudflare.NewWithAPIToken(in.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  43. } else {
  44. api, err = cloudflare.New(in.GetApiKey(), in.GetApiEmail())
  45. }
  46. if err != nil {
  47. log.Fatal(err)
  48. return &pb.PurgeReplyCloudflare{Result: false}, err
  49. }
  50. // Most API calls require a Context
  51. ctxAPI := context.Background()
  52. id := in.GetZoneId()
  53. // set purgeRequest
  54. prefixList := in.GetPrefixList()
  55. pcr := cloudflare.PurgeCacheRequest{
  56. Prefixes: prefixList,
  57. }
  58. response, err := api.PurgeCache(ctxAPI, id, pcr)
  59. if err != nil {
  60. return nil, err
  61. }
  62. log.Printf("%v", response.Response.Success)
  63. log.Printf("%v", response.Errors)
  64. return &pb.PurgeReplyCloudflare{Result: response.Response.Success}, nil
  65. }
  66. func (s *PurgeServer) PurgeCloudflareEverything(ctx context.Context, in *pb.PurgeRequestCloudflareEverything) (*pb.PurgeReplyCloudflare, error) {
  67. if strings.HasPrefix(in.GetApiKey(), TOKEN_PREFIX) {
  68. api, err = cloudflare.NewWithAPIToken(in.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  69. } else {
  70. api, err = cloudflare.New(in.GetApiKey(), in.GetApiEmail())
  71. }
  72. if err != nil {
  73. log.Fatal(err)
  74. return &pb.PurgeReplyCloudflare{Result: false}, err
  75. }
  76. // Most API calls require a Context
  77. ctxAPI := context.Background()
  78. id := in.GetZoneId()
  79. // set purgeRequest
  80. response, err := api.PurgeEverything(ctxAPI, id)
  81. if err != nil {
  82. return nil, err
  83. }
  84. log.Printf("%v", response.Response.Success)
  85. log.Printf("%v", response.Errors)
  86. return &pb.PurgeReplyCloudflare{Result: response.Response.Success}, nil
  87. }