package server import ( pb "cfTest/cloudflareApi/cache/purge" "context" "github.com/cloudflare/cloudflare-go" "log" "strings" ) type PurgeServer struct { pb.UnimplementedPurgeCloudflareServer } func (s *PurgeServer) PurgeCloudflare(ctx context.Context, in *pb.PurgeRequestCloudflare) (*pb.PurgeReplyCloudflare, error) { if strings.HasPrefix(in.GetApiKey(), TOKEN_PREFIX) { api, err = cloudflare.NewWithAPIToken(in.GetApiKey()[TOKEN_PREFIX_LENGTH:]) } else { api, err = cloudflare.New(in.GetApiKey(), in.GetApiEmail()) } // alternatively, you can use a scoped API token // api, err := cloudflare.NewWithAPIToken(os.Getenv("CLOUDFLARE_API_TOKEN")) if err != nil { log.Fatal(err) return &pb.PurgeReplyCloudflare{Result: false}, err } // Most API calls require a Context ctxAPI := context.Background() id := in.GetZoneId() // set purgeRequest fileList := in.GetPurgeList() pcr := cloudflare.PurgeCacheRequest{ Files: fileList, } response, err := api.PurgeCache(ctxAPI, id, pcr) log.Printf("Cloudflare PurgeCloudflare API response: %v err:%s", response, err) if err != nil { return nil, err } log.Printf("%v", response.Response.Success) log.Printf("%v", response.Errors) return &pb.PurgeReplyCloudflare{Result: response.Response.Success}, nil } func (s *PurgeServer) PurgeCloudflareByPrefix(ctx context.Context, in *pb.PurgeRequestCloudflarePrefix) (*pb.PurgeReplyCloudflare, error) { if strings.HasPrefix(in.GetApiKey(), TOKEN_PREFIX) { api, err = cloudflare.NewWithAPIToken(in.GetApiKey()[TOKEN_PREFIX_LENGTH:]) } else { api, err = cloudflare.New(in.GetApiKey(), in.GetApiEmail()) } if err != nil { log.Fatal(err) return &pb.PurgeReplyCloudflare{Result: false}, err } // Most API calls require a Context ctxAPI := context.Background() id := in.GetZoneId() // set purgeRequest prefixList := in.GetPrefixList() pcr := cloudflare.PurgeCacheRequest{ Prefixes: prefixList, } response, err := api.PurgeCache(ctxAPI, id, pcr) log.Printf("Cloudflare PurgeCloudflareByPrefix API response: %v err:%s", response, err) if err != nil { return nil, err } log.Printf("%v", response.Response.Success) log.Printf("%v", response.Errors) return &pb.PurgeReplyCloudflare{Result: response.Response.Success}, nil } func (s *PurgeServer) PurgeCloudflareEverything(ctx context.Context, in *pb.PurgeRequestCloudflareEverything) (*pb.PurgeReplyCloudflare, error) { if strings.HasPrefix(in.GetApiKey(), TOKEN_PREFIX) { api, err = cloudflare.NewWithAPIToken(in.GetApiKey()[TOKEN_PREFIX_LENGTH:]) } else { api, err = cloudflare.New(in.GetApiKey(), in.GetApiEmail()) } if err != nil { log.Fatal(err) return &pb.PurgeReplyCloudflare{Result: false}, err } // Most API calls require a Context ctxAPI := context.Background() id := in.GetZoneId() // set purgeRequest response, err := api.PurgeEverything(ctxAPI, id) log.Printf("Cloudflare PurgeCloudflareEverything API response: %v err:%s", response, err) if err != nil { return nil, err } log.Printf("%v", response.Response.Success) log.Printf("%v", response.Errors) return &pb.PurgeReplyCloudflare{Result: response.Response.Success}, nil }