package server import ( pb "cfTest/cloudflareApi/cache/purge" "context" "github.com/cloudflare/cloudflare-go" "log" ) type PurgeServer struct { pb.UnimplementedPurgeCloudflareServer } func (s *PurgeServer) PurgeCloudflare(ctx context.Context, in *pb.PurgeRequestCloudflare) (*pb.PurgeReplyCloudflare, error) { log.Printf("purgeTaskByUrl") log.Printf("apiKey Received: %v", in.GetApiKey()) log.Printf("apiEmail Received: %v", in.GetApiEmail()) log.Printf("zoneId Received: %v", in.GetZoneId()) log.Printf("purgeList Received: %v", in.GetPurgeList()) // Construct a new API object using a global API key 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) 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) { log.Printf("purgeTaskByPrefix") log.Printf("apiKey Received: %v", in.GetApiKey()) log.Printf("apiEmail Received: %v", in.GetApiEmail()) log.Printf("zoneId Received: %v", in.GetZoneId()) log.Printf("prefixList Received: %v", in.GetPrefixList()) // Construct a new API object using a global API key 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 prefixList := in.GetPrefixList() pcr := cloudflare.PurgeCacheRequest{ Prefixes: prefixList, } response, err := api.PurgeCache(ctxAPI, id, pcr) 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) { log.Printf("purgeEverythingTask") log.Printf("apiKey Received: %v", in.GetApiKey()) log.Printf("apiEmail Received: %v", in.GetApiEmail()) log.Printf("zoneId Received: %v", in.GetZoneId()) // Construct a new API object using a global API key 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 response, err := api.PurgeEverything(ctxAPI, id) 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 }