55 {
56 ArgumentNullException.ThrowIfNull(args);
57
58
59 if (!args.Any(arg => arg.Contains("hostBuilder:reloadConfigOnChange", StringComparison.OrdinalIgnoreCase))
60 && String.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("hostBuilder__reloadConfigOnChange")))
61 {
62 var oldArgs = args;
63 args = new string[oldArgs.Length + 1];
64 Array.Copy(oldArgs, args, oldArgs.Length);
65 args[oldArgs.Length] = "--hostBuilder:reloadConfigOnChange=false";
66 }
67
68 const string AppSettingsRelocationKey = $"--{AppSettings}-base-path=";
69
70 var appsettingsRelativeBasePathArgument = args.FirstOrDefault(arg => arg.StartsWith(AppSettingsRelocationKey, StringComparison.Ordinal));
71 string basePath;
72 if (appsettingsRelativeBasePathArgument != null)
73 basePath =
IOManager.
ResolvePath(appsettingsRelativeBasePathArgument[AppSettingsRelocationKey.Length..]);
74 else
76
77
78
79 Environment.SetEnvironmentVariable($"{InternalConfiguration.Section}__{nameof(InternalConfiguration.AppSettingsBasePath)}", basePath);
80
81 IHostBuilder CreateDefaultBuilder() => Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args)
82 .ConfigureAppConfiguration((context, builder) =>
83 {
84 builder.SetBasePath(basePath);
85
86 builder.AddYamlFile($"{AppSettings}.yml", optional: true, reloadOnChange: false)
87 .AddYamlFile($"{AppSettings}.{context.HostingEnvironment.EnvironmentName}.yml", optional: true, reloadOnChange: false);
88
89
90
91 var environmentJsonConfig = builder.Sources[2];
92 var envConfig = builder.Sources[3];
93
94
95
96#if !NET8_0
97#error Validate this monstrosity works on current .NET
98#endif
99 IConfigurationSource? cmdLineConfig;
100 IConfigurationSource baseYmlConfig, environmentYmlConfig;
101 if (args.Length == 0)
102 {
103 cmdLineConfig = null;
104 baseYmlConfig = builder.Sources[4];
105 environmentYmlConfig = builder.Sources[5];
106 }
107 else
108 {
109 cmdLineConfig = builder.Sources[4];
110 baseYmlConfig = builder.Sources[5];
111 environmentYmlConfig = builder.Sources[6];
112 }
113
114 builder.Sources[2] = baseYmlConfig;
115 builder.Sources[3] = environmentJsonConfig;
116 builder.Sources[4] = environmentYmlConfig;
117 builder.Sources[5] = envConfig;
118
119 if (cmdLineConfig != null)
120 {
121 builder.Sources[6] = cmdLineConfig;
122 }
123 });
124
125 var setupWizardHostBuilder = CreateDefaultBuilder()
127
129 using (var setupHost = setupWizardHostBuilder.Build())
130 {
131 ILogger<ServerFactory> logger = setupHost.Services.GetRequiredService<ILogger<ServerFactory>>();
133 await setupHost.RunAsync(cancellationToken);
134
135 if (postSetupServices.GeneralConfiguration.SetupWizardMode ==
SetupWizardMode.Only)
136 {
137 logger.LogInformation("Shutting down due to only running setup wizard.");
138 return null;
139 }
140
141 if (postSetupServices.ReloadRequired)
142 {
143 logger.LogInformation("TGS must restart to reload the updated configuration.");
144 return null;
145 }
146 }
147
148 var hostBuilder = CreateDefaultBuilder()
149 .ConfigureWebHost(webHostBuilder =>
150 webHostBuilder
151 .UseKestrel(kestrelOptions =>
152 {
153 var serverPortProvider = kestrelOptions.ApplicationServices.GetRequiredService<
IServerPortProvider>();
154 kestrelOptions.ListenAnyIP(
155 serverPortProvider.HttpApiPort,
156 listenOptions => listenOptions.Protocols = HttpProtocols.Http1);
157
158
159 kestrelOptions.Limits.MaxRequestLineSize = 8400;
160 })
161 .UseIIS()
162 .UseIISIntegration()
164 .SuppressStatusMessages(true)
165 .UseShutdownTimeout(
166 TimeSpan.FromMinutes(
167 postSetupServices.GeneralConfiguration.RestartTimeoutMinutes)));
168
169 if (updatePath != null)
170 hostBuilder.UseContentRoot(
173
174 return new Server(hostBuilder, updatePath);
175 }
IIOManager IOManager
The IIOManager for the IServerFactory.
readonly IAssemblyInformationProvider assemblyInformationProvider
The IAssemblyInformationProvider for the ServerFactory.
Provides access to the server's HttpApiPort.
string ResolvePath()
Retrieve the full path of the current working directory.
string GetDirectoryName(string path)
Gets the directory portion of a given path .
Set of objects needed to configure an Core.Application.
@ Server
Use server authentication.
SetupWizardMode
Determines if the SetupWizard will run.