tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
RootController.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq.Expressions;
4
5using Microsoft.AspNetCore.Authorization;
6using Microsoft.AspNetCore.Hosting;
7using Microsoft.AspNetCore.Mvc;
8using Microsoft.Extensions.Logging;
9using Microsoft.Extensions.Options;
10
16
18{
22 [Route("/")]
23 [ApiExplorerSettings(IgnoreApi = true)]
24 public sealed class RootController : Controller
25 {
29 const string LogoSvgWindowsName = "0176d5d8b7d307f158e0";
30
34 const string LogoSvgLinuxName = "b5616c99bf2052a6bbd7";
35
40
45
49 readonly IWebHostEnvironment hostEnvironment;
50
54 readonly ILogger<RootController> logger;
55
60
65
70
76 static Tuple<string, string?> GetControlPanelActionLink(Expression<Func<ControlPanelController, IActionResult>> actionExpression)
77 {
78 var memberSelectorExpression = (MethodCallExpression)actionExpression.Body;
79 var method = memberSelectorExpression.Method;
80
81 var controllerName = typeof(ControlPanelController).Name;
82
83 const string ControllerSuffix = nameof(Controller);
84 if (controllerName.EndsWith(ControllerSuffix, StringComparison.Ordinal))
85 controllerName = controllerName.Substring(0, controllerName.Length - ControllerSuffix.Length);
86
87 var actionName = method.Name;
88
89 return Tuple.Create<string, string?>(controllerName, actionName);
90 }
91
105 IWebHostEnvironment hostEnvironment,
106 ILogger<RootController> logger,
107 IOptions<GeneralConfiguration> generalConfigurationOptions,
108 IOptions<ControlPanelConfiguration> controlPanelConfigurationOptions,
109 IOptionsSnapshot<InternalConfiguration> internalConfigurationOptions)
110 {
111 this.assemblyInformationProvider = assemblyInformationProvider ?? throw new ArgumentNullException(nameof(assemblyInformationProvider));
112 this.platformIdentifier = platformIdentifier ?? throw new ArgumentNullException(nameof(platformIdentifier));
113 this.hostEnvironment = hostEnvironment ?? throw new ArgumentNullException(nameof(hostEnvironment));
114 this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
115 generalConfiguration = generalConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(generalConfigurationOptions));
116 controlPanelConfiguration = controlPanelConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(controlPanelConfigurationOptions));
117 internalConfiguration = internalConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(internalConfigurationOptions));
118 }
119
124 [HttpGet]
125 [AllowAnonymous]
126 public IActionResult Index()
127 {
128 var panelEnabled = controlPanelConfiguration.Enable;
129 var apiDocsEnabled = generalConfiguration.HostApiDocumentation;
130
131 var controlPanelRoute = $"{ControlPanelController.ControlPanelRoute.TrimStart('/')}/";
132 if (panelEnabled && !apiDocsEnabled)
133 return Redirect(controlPanelRoute);
134
135 Dictionary<string, string>? links = null;
136
137 if (panelEnabled || apiDocsEnabled)
138 {
139 links = new Dictionary<string, string>();
140
141 if (panelEnabled)
142 links.Add("Web Control Panel", controlPanelRoute);
143
144 if (apiDocsEnabled)
145 {
147 links.Add("GraphQL API Documentation", Routes.GraphQL);
148
149 links.Add("REST API Documentation", SwaggerConfiguration.DocumentationSiteRouteExtension);
150 }
151 }
152 else
153 links = null;
154
155 var model = new
156 {
157 Links = links,
159 };
160
161 return View(model);
162 }
163
168 [HttpGet("logo.svg")]
169 public IActionResult GetLogo()
170 {
171 // these are different because of motherfucking line endings -_-
173 {
174 VirtualFileResult? result = this.TryServeFile(hostEnvironment, logger, $"{LogoSvgWindowsName}.svg");
175 if (result != null)
176 return result;
177
178 // BUT THE UPDATE PACKAGES ARE BUILT ON LINUX RAAAAAGH
179 }
180
181 return (IActionResult?)this.TryServeFile(hostEnvironment, logger, $"{LogoSvgLinuxName}.svg") ?? NotFound();
182 }
183
189 public RedirectToActionResult WebpanelChannelRedirect()
190 {
191 var actionTuple = GetControlPanelActionLink(controller => controller.GetChannelJson());
192
193 return RedirectToActionPermanent(
194 actionTuple.Item2,
195 actionTuple.Item1);
196 }
197 }
198}
Routes to a server actions.
Definition Routes.cs:9
const string GraphQL
The GraphQL route.
Definition Routes.cs:18
bool HostApiDocumentation
If the swagger documentation and UI should be made avaiable.
Unstable configuration options used internally by TGS.
bool EnableGraphQL
Enables hosting the experimental GraphQL API in Release builds.
const string ChannelJsonRoute
The route to the control panel channel .json.
const string LogoSvgWindowsName
The name of the TGS logo .svg in the IWebHostEnvironment.WebRootPath on Windows.
readonly IPlatformIdentifier platformIdentifier
The IPlatformIdentifier for the RootController.
readonly InternalConfiguration internalConfiguration
The InternalConfiguration for the RootController.
readonly ILogger< RootController > logger
The ILogger for the RootController.
readonly GeneralConfiguration generalConfiguration
The GeneralConfiguration for the RootController.
RedirectToActionResult WebpanelChannelRedirect()
Workaround for the webpanel always expecting a trailing slash.
IActionResult Index()
Gets the server's homepage.
readonly IAssemblyInformationProvider assemblyInformationProvider
The IAssemblyInformationProvider for the RootController.
RootController(IAssemblyInformationProvider assemblyInformationProvider, IPlatformIdentifier platformIdentifier, IWebHostEnvironment hostEnvironment, ILogger< RootController > logger, IOptions< GeneralConfiguration > generalConfigurationOptions, IOptions< ControlPanelConfiguration > controlPanelConfigurationOptions, IOptionsSnapshot< InternalConfiguration > internalConfigurationOptions)
Initializes a new instance of the RootController class.
const string LogoSvgLinuxName
The name of the TGS logo .svg in the IWebHostEnvironment.WebRootPath on Linux.
readonly IWebHostEnvironment hostEnvironment
THe IWebHostEnvironment for the RootController.
static Tuple< string, string?> GetControlPanelActionLink(Expression< Func< ControlPanelController, IActionResult > > actionExpression)
Gets a Tuple<T1, T2> giving the ControlPanelController and action names for a given actionExpression ...
IActionResult GetLogo()
Retrieve the logo .svg for the webpanel.
readonly ControlPanelConfiguration controlPanelConfiguration
The ControlPanelConfiguration for the RootController.
Implements various filters for Swashbuckle.
const string DocumentationSiteRouteExtension
The path to the hosted documentation site.
For identifying the current platform.
bool IsWindows
If the current platform is a Windows platform.