-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathHostBuilderExtensions.cs
132 lines (104 loc) · 5.04 KB
/
HostBuilderExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using Bet.Extensions.Options;
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using TextCopy;
using Console = Colorful.Console;
namespace AuthApp
{
internal static class HostBuilderExtensions
{
internal static IHostBuilder CreateDefaultBuilder(HostBuilderOptions options)
{
var builder = new HostBuilder();
builder.UseEnvironment(options.HostingEnviroment);
var fullPath = Directory.GetCurrentDirectory();
if (!string.IsNullOrWhiteSpace(Path.GetDirectoryName(options.ConfigFile)))
{
fullPath = Path.GetDirectoryName(options.ConfigFile);
}
builder.UseContentRoot(fullPath);
var defaultConfigName = !string.IsNullOrWhiteSpace(options.ConfigFile) ? Path.GetFileName(options.ConfigFile) : "appsettings.json";
if (options.Verbose)
{
Console.WriteLine($"ContentRoot:{fullPath}", color: Color.Green);
}
builder
.UseOptionValidation()
.ConfigureAppConfiguration((context, config) =>
{
// appsettings file or others
config.AddJsonFile(Path.Combine(fullPath, $"{defaultConfigName.Split(".")[0]}.json"), optional: true)
.AddJsonFile(Path.Combine(fullPath, $"{defaultConfigName.Split(".")[0]}.{options.HostingEnviroment}.json"), optional: true);
// add secrets if specified
if (options.UserSecrets)
{
config.AddUserSecrets(ConsoleHandler.GetUserSecretsId());
}
// configure Azure Vault from the other settings.
var appAzureVaultUrl = config.Build().Bind<AzureVaultOptions>("AzureVault", enableValidation: false);
var inputValues = new Dictionary<string, string>
{
{ $"{options.SectionName}:ClientId", options?.Settings?.ClientId ?? string.Empty },
{ $"{options.SectionName}:ClientSecret", options?.Settings?.ClientSecret ?? string.Empty },
{ $"{options.SectionName}:LoginUrl", options?.Settings?.LoginUrl ?? string.Empty },
{ $"{options.SectionName}:OAuthUri", options?.Settings?.OAuthUri ?? string.Empty },
{ $"{options.SectionName}:OAuthorizeUri", options?.Settings?.OAuthorizeUri ?? string.Empty },
};
config.AddInMemoryCollection(inputValues);
// build azure key vault from passed in parameter
if (!string.IsNullOrWhiteSpace(options?.AzureVault))
{
var dic = new Dictionary<string, string>
{
{ "AzureVault:BaseUrl", options.AzureVault }
};
config.AddInMemoryCollection(dic);
}
// use appsettings vault information
if (!string.IsNullOrWhiteSpace(appAzureVaultUrl.BaseUrl)
|| !string.IsNullOrWhiteSpace(options?.AzureVault))
{
config.AddAzureKeyVault(hostingEnviromentName: options.HostingEnviroment, options.UseAzureKeyPrefix);
}
if ((options.Verbose && options.Level == LogLevel.Debug)
|| options.Level == LogLevel.Trace)
{
config.Build().DebugConfigurations();
}
});
builder
.ConfigureLogging((_, configureBuilder) =>
{
if (options.Verbose)
{
configureBuilder.AddConsole();
configureBuilder.AddDebug();
}
});
builder
.ConfigureServices((context, services) =>
{
services.AddSingleton(options);
services.AddHostedService<HostStartupService>();
// disable hosting messages
services.Configure<ConsoleLifetimeOptions>(opt => opt.SuppressStatusMessages = true);
if (options.Verbose)
{
services.AddLogging(x => x.AddFilter((loglevel) =>
{
return loglevel == options.Level;
}));
}
services.AddSingleton<IClipboard, Clipboard>();
services.AddSingleton(PhysicalConsole.Singleton);
});
return builder;
}
}
}