tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
Public Member Functions | Static Public Attributes | Properties | Private Attributes | List of all members
Tgstation.Server.Host.ServerFactory Class Referencesealed

Implementation of IServerFactory. More...

Inheritance diagram for Tgstation.Server.Host.ServerFactory:
Inheritance graph
[legend]
Collaboration diagram for Tgstation.Server.Host.ServerFactory:
Collaboration graph
[legend]

Public Member Functions

async ValueTask< IServer?> CreateServer (string[] args, string? updatePath, CancellationToken cancellationToken)
 Create a IServer.
Parameters
argsThe arguments for the IServer.
updatePathThe directory in which to install server updates.
cancellationTokenThe CancellationToken for the operation.
Returns
A ValueTask<TResult> resulting in a new IServer if it should be run, null otherwise.

 

Static Public Attributes

const string AppSettings = "appsettings"
 Name of the appsettings file.
 

Properties

IIOManager IOManager [get]
 The IIOManager for the IServerFactory.
 
- Properties inherited from Tgstation.Server.Host.IServerFactory

Private Attributes

readonly IAssemblyInformationProvider assemblyInformationProvider
 The IAssemblyInformationProvider for the ServerFactory.
 

Detailed Description

Implementation of IServerFactory.

Definition at line 25 of file ServerFactory.cs.

Member Function Documentation

◆ CreateServer()

async ValueTask< IServer?> Tgstation.Server.Host.ServerFactory.CreateServer ( string[]  args,
string?  updatePath,
CancellationToken  cancellationToken 
)

Create a IServer.

Parameters
argsThe arguments for the IServer.
updatePathThe directory in which to install server updates.
cancellationTokenThe CancellationToken for the operation.
Returns
A ValueTask<TResult> resulting in a new IServer if it should be run, null otherwise.

Implements Tgstation.Server.Host.IServerFactory.

Definition at line 54 of file ServerFactory.cs.

55 {
56 ArgumentNullException.ThrowIfNull(args);
57
58 // need to shove this arg in to disable config reloading unless a user specifically overrides it
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
75 basePath = IOManager.ResolvePath();
76
77 // this is a massive bloody hack but I don't know a better way to do it
78 // It's needed for the setup wizard
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 // reorganize the builder so our yaml configs don't override the env/cmdline configs
90 // values obtained via debugger
91 var environmentJsonConfig = builder.Sources[2];
92 var envConfig = builder.Sources[3];
93
94 // CURSED
95 // https://github.com/dotnet/runtime/blob/30dc7e7aedb7aab085c7d9702afeae5bc5a43133/src/libraries/Microsoft.Extensions.Hosting/src/HostingHostBuilderExtensions.cs#L246-L249
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()
126 .UseSetupApplication(assemblyInformationProvider, IOManager);
127
128 IPostSetupServices postSetupServices;
129 using (var setupHost = setupWizardHostBuilder.Build())
130 {
131 ILogger<ServerFactory> logger = setupHost.Services.GetRequiredService<ILogger<ServerFactory>>();
132 postSetupServices = setupHost.Services.GetRequiredService<IPostSetupServices>();
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); // Can't use Http1And2 without TLS. Let the reverse proxy handle it
157
158 // with 515 we lost the ability to test this effectively. Just bump it slightly above the default and let the existing limit hold us back
159 kestrelOptions.Limits.MaxRequestLineSize = 8400;
160 })
161 .UseIIS()
162 .UseIISIntegration()
163 .UseApplication(assemblyInformationProvider, IOManager, postSetupServices)
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.

References Tgstation.Server.Host.ServerFactory.assemblyInformationProvider, Tgstation.Server.Host.Setup.IPostSetupServices.GeneralConfiguration, Tgstation.Server.Host.IO.IIOManager.GetDirectoryName(), Tgstation.Server.Host.ServerFactory.IOManager, Tgstation.Server.Host.System.IAssemblyInformationProvider.Path, Tgstation.Server.Host.IO.IIOManager.ResolvePath(), and Tgstation.Server.Host.Configuration.GeneralConfiguration.RestartTimeoutMinutes.

Here is the call graph for this function:

Member Data Documentation

◆ AppSettings

const string Tgstation.Server.Host.ServerFactory.AppSettings = "appsettings"
static

Name of the appsettings file.

Definition at line 30 of file ServerFactory.cs.

◆ assemblyInformationProvider

readonly IAssemblyInformationProvider Tgstation.Server.Host.ServerFactory.assemblyInformationProvider
private

The IAssemblyInformationProvider for the ServerFactory.

Definition at line 35 of file ServerFactory.cs.

Referenced by Tgstation.Server.Host.ServerFactory.CreateServer().

Property Documentation

◆ IOManager

IIOManager Tgstation.Server.Host.ServerFactory.IOManager
get

The IIOManager for the IServerFactory.

Implements Tgstation.Server.Host.IServerFactory.

Definition at line 38 of file ServerFactory.cs.

38{ get; }

Referenced by Tgstation.Server.Host.ServerFactory.CreateServer().


The documentation for this class was generated from the following file: