-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from DubyaDude/feature/cloudflare
Feature/cloudflare
- Loading branch information
Showing
5 changed files
with
186 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using CloudFlare.Client; | ||
using CloudFlare.Client.Api.Authentication; | ||
using VRCX_API.Configs; | ||
|
||
namespace VRCX_API.Services | ||
{ | ||
public class CloudflareService : IHostedService | ||
{ | ||
private const string _zoneName = "vrcx.app"; | ||
private readonly ILogger<CloudflareService> _logger; | ||
private readonly CloudFlareClient _client; | ||
private string _zoneId = string.Empty; | ||
|
||
public CloudflareService(ILogger<CloudflareService> logger) | ||
{ | ||
_logger = logger; | ||
|
||
var authentication = new ApiTokenAuthentication(CommonConfig.Config.Instance.CloudflareAPIKey); | ||
_client = new CloudFlareClient(authentication); | ||
} | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
var zones = await _client.Zones.GetAsync(); | ||
|
||
if (!zones.Success) | ||
{ | ||
throw new Exception("Failed to get zones from Cloudflare"); | ||
} | ||
|
||
var vrcxZone = zones.Result.FirstOrDefault(x => x.Name == _zoneName); | ||
|
||
if (string.IsNullOrWhiteSpace(vrcxZone?.Id)) | ||
{ | ||
throw new Exception($"Failed to get zone id for {_zoneName}"); | ||
} | ||
|
||
_zoneId = vrcxZone.Id; | ||
} | ||
|
||
public async Task PurgeCache() | ||
{ | ||
_logger.LogInformation("Purging cache for {zoneName}", _zoneName); | ||
var result = await _client.Zones.PurgeAllFilesAsync(_zoneId, true); | ||
if (!result.Success) | ||
{ | ||
throw new Exception($"Failed to purge cache for {_zoneName}"); | ||
} | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters