Skip to content

Commit e9e35b0

Browse files
committed
Move AppConfig to appsettings with latest version in mythz/server-config
1 parent 017f5e8 commit e9e35b0

File tree

5 files changed

+40
-51
lines changed

5 files changed

+40
-51
lines changed

BlazorDiffusion.ServiceInterface/ArtifactMetaServices.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public async Task<object> Get(DownloadArtifact request)
7272
});
7373

7474
long? contentLength = null;
75-
var url = appConfig.AiServerUrl.CombineWith(artifact.FilePathLarge);
75+
var url = appConfig.AiServerBaseUrl.CombineWith(artifact.FilePathLarge);
7676
var imageBytes = await url.GetBytesFromUrlAsync(responseFilter:res => contentLength = res.GetContentLength());
7777
var headerValue = $"attachment; {HttpExt.GetDispositionFileName(artifact.FileName)}; " +
7878
(contentLength != null ? $"size={contentLength}; " : "") +

BlazorDiffusion.ServiceModel/AppConfig.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public class AppConfig
1212
public static AppConfig Instance { get; private set; } = new();
1313
public string? GitPagesBaseUrl { get; set; }
1414
public string BaseUrl { get; set; }
15-
public string AiServerUrl { get; set; }
15+
public string AiServerBaseUrl { get; set; }
16+
public string? AiServerApiKey { get; set; }
1617
public string ApiBaseUrl { get; set; }
1718
public string WwwBaseUrl { get; set; }
1819
public string CdnBaseUrl { get; set; }
@@ -35,6 +36,6 @@ public class AppConfig
3536
/// Ignore saving creatives + pre-rendering pages to avoid Hot Reload reloading page
3637
/// </summary>
3738
public bool DisableWrites { get; set; }
38-
public TimeSpan SyncTasksInterval { get; set; }
39+
public TimeSpan SyncTasksInterval { get; set; } = TimeSpan.FromMinutes(10);
3940
public static AppConfig Set(AppConfig instance) => Instance = instance;
4041
}

BlazorDiffusion.ServiceModel/AppData.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public class AppData
2222
public static List<NavItem> GetNavItems(bool isAdmin) => isAdmin ? Instance.AdminLinks : Instance.DefaultLinks;
2323

2424
public static List<Group> CategoryGroups = new Group[] {
25-
new() { Name = "Scene", Items = new[] { "Quality", "Style", "Aesthetic", "Features", "Medium", "Setting", "Theme" } },
26-
new() { Name = "Effects", Items = new[] { "Effects", "CGI", "Filters", "Lenses", "Photography", "Lighting", "Color" } },
27-
new() { Name = "Art Style", Items = new[] { "Art Movement", "Art Style", "18 Century", "19 Century", "20 Century", "21 Century" } },
28-
new() { Name = "Mood", Items = new[] { "Positive Mood", "Negative Mood" } },
25+
new() { Name = "Scene", Items = ["Quality", "Style", "Aesthetic", "Features", "Medium", "Setting", "Theme"] },
26+
new() { Name = "Effects", Items = ["Effects", "CGI", "Filters", "Lenses", "Photography", "Lighting", "Color"] },
27+
new() { Name = "Art Style", Items = ["Art Movement", "Art Style", "18 Century", "19 Century", "20 Century", "21 Century" ] },
28+
new() { Name = "Mood", Items = ["Positive Mood", "Negative Mood"] },
2929
}.ToList();
3030

3131
string[]? categories;

BlazorDiffusion/Configure.AppHost.cs

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,33 @@ public void Configure(IWebHostBuilder builder) => builder
2020
services.AddSingleton(AppData.Instance);
2121
services.AddSingleton<AppUserQuotas>();
2222

23+
context.Configuration.GetSection(nameof(AppConfig)).Bind(AppConfig.Instance);
24+
services.AddSingleton(AppConfig.Instance);
25+
var appConfig = AppConfig.Instance;
26+
2327
var baseUrl = Environment.GetEnvironmentVariable("VIRTUAL_HOST");
2428
baseUrl = !string.IsNullOrEmpty(baseUrl)
2529
? $"https://{baseUrl}"
2630
: LocalBaseUrl;
27-
2831
var apiUrl = Environment.GetEnvironmentVariable("DEPLOY_API");
2932
apiUrl = !string.IsNullOrEmpty(apiUrl)
3033
? $"https://{apiUrl}"
3134
: null;
32-
33-
var aiServerUrl = "https://ai-server-cdn.diffusion.works";
34-
#if DEBUG
35-
aiServerUrl = "https://localhost:5005";
36-
#endif
37-
38-
// set in launchSettings.json
35+
appConfig.BaseUrl = baseUrl;
36+
appConfig.ApiBaseUrl = apiUrl ?? baseUrl;
37+
var aiServerApiKey = Environment.GetEnvironmentVariable("AI_SERVER_APIKEY");
38+
if (aiServerApiKey != null)
39+
appConfig.AiServerApiKey = aiServerApiKey;
40+
41+
var r2AccountId = Environment.GetEnvironmentVariable("R2_ACCOUNT_ID");
42+
if (r2AccountId != null)
43+
appConfig.R2Account = r2AccountId;
3944
var r2AccessId = Environment.GetEnvironmentVariable("R2_ACCESS_KEY_ID");
45+
if (r2AccessId != null)
46+
appConfig.R2AccessId = r2AccessId;
4047
var r2AccessKey = Environment.GetEnvironmentVariable("R2_SECRET_ACCESS_KEY");
41-
42-
var appConfig = AppConfig.Set(new AppConfig
43-
{
44-
BaseUrl = baseUrl,
45-
ApiBaseUrl = apiUrl ?? baseUrl,
46-
AiServerUrl = aiServerUrl,
47-
WwwBaseUrl = baseUrl,
48-
CdnBaseUrl = baseUrl,
49-
R2Account = "b95f38ca3a6ac31ea582cd624e6eb385",
50-
R2AccessId = r2AccessId,
51-
R2AccessKey = r2AccessKey,
52-
ArtifactBucket = "diffusion",
53-
CdnBucket = "diffusion-client",
54-
AssetsBasePath = r2AccessId != null
55-
? "https://cdn.diffusion.works"
56-
: "/uploads",
57-
FallbackAssetsBasePath = r2AccessId != null ? "https://pub-97bba6b94a944260b10a6e7d4bf98053.r2.dev" : "/uploads",
58-
SyncTasksInterval = TimeSpan.FromMinutes(10),
59-
});
60-
services.AddSingleton(appConfig);
48+
if (r2AccessKey != null)
49+
appConfig.R2AccessKey = r2AccessKey;
6150

6251
var s3Client = new AmazonS3Client(appConfig.R2AccessId, appConfig.R2AccessKey, new AmazonS3Config
6352
{
@@ -66,16 +55,16 @@ public void Configure(IWebHostBuilder builder) => builder
6655
services.AddSingleton(s3Client);
6756

6857
services.AddPlugin(new CorsFeature(allowedHeaders: "Content-Type,Authorization",
69-
allowOriginWhitelist: new[]{
58+
allowOriginWhitelist: [
7059
"https://localhost:5002",
7160
"http://localhost:5000",
7261
"http://localhost:8080",
7362
"https://diffusion.works",
7463
appConfig.BaseUrl,
7564
"https://blazordiffusion.com",
7665
"https://www.blazordiffusion.com",
77-
"https://pub-e17dff5b2d09437a97efdbb7f6ee3701.r2.dev", // CDN diffusion-client public bucket
78-
}, allowCredentials: true));
66+
"https://pub-e17dff5b2d09437a97efdbb7f6ee3701.r2.dev" // CDN diffusion-client public bucket
67+
], allowCredentials: true));
7968

8069
var hasR2 = !string.IsNullOrEmpty(appConfig.R2AccessId);
8170
if (!hasR2)
@@ -103,26 +92,17 @@ public void Configure(IWebHostBuilder builder) => builder
10392
transformFile: ImageUtils.TransformAvatarAsync)
10493
));
10594

106-
#if DEBUG
107-
var aiServerClient = new JsonApiClient("https://localhost:5005/");
108-
#else
109-
var aiServerClient = new JsonApiClient("https://openai.servicestack.net/");
110-
#endif
111-
// If development, ignore SSL
95+
var aiServerClient = new JsonApiClient(appConfig.AiServerBaseUrl);
11296
if (context.HostingEnvironment.IsDevelopment())
11397
{
114-
aiServerClient = new JsonApiClient("https://localhost:5005/");
115-
HttpClientHandler? clientHandler = new HttpClientHandler
116-
{
98+
// If development, Ignore local SSL Errors
99+
var clientHandler = new HttpClientHandler {
117100
ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true
118101
};
119102
HttpUtils.CreateClient = () => new HttpClient(clientHandler);
120-
// Ignore local SSL Errors
121103
aiServerClient = IgnoreSslValidation(aiServerClient);
122104
}
123-
124-
if(!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("AI_SERVER_APIKEY")))
125-
aiServerClient.AddHeader("Authorization","Bearer " + Environment.GetEnvironmentVariable("AI_SERVER_APIKEY"));
105+
aiServerClient.AddHeader("Authorization","Bearer " + appConfig.AiServerApiKey);
126106

127107
services.AddSingleton<IStableDiffusionClient>(x => new AiServerClient
128108
{

BlazorDiffusion/appsettings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
"DefaultConnection": "DataSource=App_Data/db.sqlite;Cache=Shared",
44
"AnalyticsConnection": "DataSource=App_Data/analytics.sqlite;Cache=Shared"
55
},
6+
"AppConfig": {
7+
"AiServerBaseUrl": "https://localhost:5005",
8+
"AiServerApiKey": "ak-1D8820384B4B4E1E9E26E76D4B96F6BD",
9+
"ArtifactBucket": "diffusion",
10+
"CdnBucket": "diffusion-client",
11+
"AssetsBasePath": "/uploads",
12+
"FallbackAssetsBasePath": "/uploads"
13+
},
614
"Logging": {
715
"IncludeScopes": false,
816
"Debug": {

0 commit comments

Comments
 (0)