123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- 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
- }
|